diff --git a/README.md b/README.md index 09a683f..970ae4f 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,26 @@ # feragstring -*feragstring* is a Go package for creating a Ferag string file from a JSON data structure. If you don't know what Ferag (the company) or a Ferag string is you probably don't need this package ;-) +*feragstring* is a Go package for creating a FERAG string file from a JSON data structure. If you don't know what Ferag (the company) or a Ferag string is you probably don't need this package ;-) + +## The shortest possible FERAG string + +According to FERAG's documentation this is the shortest possible FERAG string: + +``` +%2440+40DEMO2009! +%2401+11E1_ROUTE_100 ! +%2402+11E1_ROUTE_100 +590+91000000+20E1 ! +%2403+12R100RE001 +1300123! +%2406+11E1_ROUTE_100 ! +%2441+40DEMO2009! +``` + +The variable values are: + +- the title is 'DEMO2009' +- a route named 'E1_ROUTE_100' +- an edition called 'E1' +- a production drop 'R100RE001' with 123 copies ## Usage diff --git a/ferag.go b/ferag.go index c9b2c84..6337943 100644 --- a/ferag.go +++ b/ferag.go @@ -13,6 +13,9 @@ type FeragString struct { ProductReferencesNr int RouteListEntries []*RouteListEntry RouteListEntryCount int + RouteInfoEntries []*RouteInfo + ProductionDropEntries []*ProductionDrop + RouteEndEntries []*RouteEnd } func NewFeragString() *FeragString { @@ -39,6 +42,18 @@ func (fs *FeragString) PrintOut() string { info += rl.Message() } + for _, ri := range fs.RouteInfoEntries { + info += ri.Message() + } + + for _, pd := range fs.ProductionDropEntries { + info += pd.Message() + } + + for _, re := range fs.RouteEndEntries { + info += re.Message() + } + info += fs.TitleEnd.Message() return info } @@ -55,9 +70,24 @@ func (fs *FeragString) AddProductReference(pr *ProductReference) error { func (fs *FeragString) AddRouteListEntry(rl *RouteListEntry) error { fs.RouteListEntryCount++ - if rl.routeCode == 0 { - rl.SetRouteCode(fs.RouteListEntryCount) - } + //if rl.routeCode == 0 { + // rl.SetRouteCode(fs.RouteListEntryCount) + //} fs.RouteListEntries = append(fs.RouteListEntries, rl) return nil +} + +func (fs *FeragString) AddRouteInfo(ri *RouteInfo) error { + fs.RouteInfoEntries = append(fs.RouteInfoEntries, ri) + return nil +} + +func (fs *FeragString) AddProductionDrop(pd *ProductionDrop) error { + fs.ProductionDropEntries = append(fs.ProductionDropEntries, pd) + return nil +} + +func (fs *FeragString) AddRouteEnd(re *RouteEnd) error { + fs.RouteEndEntries = append(fs.RouteEndEntries, re) + return nil } \ No newline at end of file diff --git a/ferag_message.go b/ferag_message.go index c1acd56..2b52ac2 100644 --- a/ferag_message.go +++ b/ferag_message.go @@ -1,6 +1,9 @@ package feragstring -import "fmt" +import ( + "fmt" + "strings" +) type FeragMessage struct { messageStart string @@ -21,7 +24,7 @@ func (fm *FeragMessage) MessageTemplate() MessageTemplateFunc { return func(fm *FeragMessage, s string) string { message := fm.getMessageStart() message += s - message += fm.getMessageEnd() + linebreak - return message + message += fm.getMessageEnd() + return strings.TrimSpace(message) + linebreak } } diff --git a/ferag_test.go b/ferag_test.go index 7093632..0e4c8a9 100644 --- a/ferag_test.go +++ b/ferag_test.go @@ -3,9 +3,59 @@ package feragstring import ( "fmt" "io/ioutil" + "strings" "testing" ) +func TestShortestFeragString(t *testing.T) { + fs := NewFeragString() + fs.SetTitleName("DEMO2009") + + rl := NewRouteListEntry() + rl.SetRouteName("E1_ROUTE_100") + fs.AddRouteListEntry(rl) + + ri := NewRouteInfo() + ri.SetRouteName("E1_ROUTE_100") + ri.SetEditionName("E1") + fs.AddRouteInfo(ri) + + pd := NewProductionDrop() + pd.SetAgentName("R100RE001") + pd.SetNumberOfCopies(123) + fs.AddProductionDrop(pd) + + re := NewRouteEnd() + re.SetRouteName("E1_ROUTE_100") + fs.AddRouteEnd(re) + + producedContent := fs.PrintOut() + + want := getTestFileContent("D:\\TEMP\\Feragstring\\minimal_want.txt") + + fmt.Println("--WANT-----------------") + fmt.Println(want) + fmt.Println("-----------------------") + fmt.Println("--PRODUCED-------------") + fmt.Println(producedContent) + fmt.Println("-----------------------") + fmt.Println("=======================") + + filename := "D:\\TEMP\\Feragstring\\minimal_test.txt" + err := ioutil.WriteFile(filename, []byte(producedContent), 0644) + if err != nil { + panic(err) + } + + + fmt.Printf("COMPARE: %d", strings.Compare(producedContent, want)) + if strings.Compare(producedContent, want) != 0 { + //if strings.TrimSpace(producedContent) != strings.TrimSpace(want) { + t.Errorf("Produced result does not equal to minimal example") + } + +} + func TestNewFeragString(t *testing.T) { fs := NewFeragString() fs.SetTitleName("MOP10629") @@ -59,7 +109,7 @@ func TestNewFeragString(t *testing.T) { testcontent := getTestFileContent("D:\\TEMP\\Feragstring\\test.txt") fmt.Println("=======================") - fmt.Println("TEST:") + fmt.Println("WANT:") fmt.Println(testcontent) fmt.Println("-----------------------") fmt.Println("PRODUCED:") diff --git a/production_drop.go b/production_drop.go new file mode 100644 index 0000000..56cff7e --- /dev/null +++ b/production_drop.go @@ -0,0 +1,46 @@ +package feragstring + +import "fmt" + +type ProductionDrop struct { + FeragMessage + agentName string + numberOfCopies int +} + +func (pd *ProductionDrop) NumberOfCopies() string { + return fmt.Sprintf("+13%05d", pd.numberOfCopies) +} + +func (pd *ProductionDrop) SetNumberOfCopies(numberOfCopies int) { + pd.numberOfCopies = numberOfCopies +} + +func (pd *ProductionDrop) AgentName() string { + return fmt.Sprintf("+12%-10s", pd.agentName) +} + +func (pd *ProductionDrop) SetAgentName(agentName string) { + pd.agentName = agentName +} + +func NewProductionDrop() *ProductionDrop { + pd := ProductionDrop{ + FeragMessage: FeragMessage{ + messageStart: "2403", + messageEnd: "!", + }, + } + return &pd +} + +func (pd *ProductionDrop) Payload() string { + data := pd.AgentName() + data += pd.NumberOfCopies() + return data +} + +func (pd *ProductionDrop) Message() string { + message := pd.FeragMessage.MessageTemplate() + return message(&pd.FeragMessage, pd.Payload()) +} diff --git a/route_end.go b/route_end.go new file mode 100644 index 0000000..f4e43d3 --- /dev/null +++ b/route_end.go @@ -0,0 +1,36 @@ +package feragstring + +import "fmt" + +type RouteEnd struct { + FeragMessage + routeName string +} + +func (re *RouteEnd) RouteName() string { + return fmt.Sprintf("+11%-13s", re.routeName) +} + +func (re *RouteEnd) SetRouteName(routeName string) { + re.routeName = routeName +} + +func NewRouteEnd() *RouteEnd { + re := RouteEnd{ + FeragMessage: FeragMessage{ + messageStart: "2406", + messageEnd: "!", + }, + } + return &re +} + +func (re *RouteEnd) Payload() string { + data := re.RouteName() + return data +} + +func (re *RouteEnd) Message() string { + message := re.FeragMessage.MessageTemplate() + return message(&re.FeragMessage, re.Payload()) +} diff --git a/route_info.go b/route_info.go new file mode 100644 index 0000000..d7aceff --- /dev/null +++ b/route_info.go @@ -0,0 +1,66 @@ +package feragstring + +import "fmt" + +type RouteInfo struct { + FeragMessage + routeName string + topsheetMarker int + eaAddressDefinition int + editionName string +} + +func (ri *RouteInfo) EditionName() string { + return fmt.Sprintf("+20%-30s", ri.editionName) +} + +func (ri *RouteInfo) SetEditionName(editionName string) { + ri.editionName = editionName +} + +func (ri *RouteInfo) EaAddressDefinition() string { + return fmt.Sprintf("+91%06d", ri.eaAddressDefinition) +} + +func (ri *RouteInfo) SetEaAddressDefinition(eaAddressDefinition int) { + ri.eaAddressDefinition = eaAddressDefinition +} + +func (ri *RouteInfo) TopsheetMarker() string { + return fmt.Sprintf("+59%1d", ri.topsheetMarker) +} + +func (ri *RouteInfo) SetTopsheetMarker(topsheetMarker int) { + ri.topsheetMarker = topsheetMarker +} + +func (ri *RouteInfo) SetRouteName(routeName string) { + ri.routeName = routeName +} + +func (ri *RouteInfo) RouteName() string { + return fmt.Sprintf("+11%-13s", ri.routeName) +} + +func NewRouteInfo() *RouteInfo { + ri := RouteInfo{ + FeragMessage: FeragMessage{ + messageStart: "2402", + messageEnd: "!", + }, + } + return &ri +} + +func (ri *RouteInfo) Payload() string { + data := ri.RouteName() + data += ri.TopsheetMarker() + data += ri.EaAddressDefinition() + data += ri.EditionName() + return data +} + +func (ri *RouteInfo) Message() string { + message := ri.FeragMessage.MessageTemplate() + return message(&ri.FeragMessage, ri.Payload()) +} diff --git a/route_list.go b/route_list.go index ea35adb..4bc364a 100644 --- a/route_list.go +++ b/route_list.go @@ -11,6 +11,9 @@ type RouteListEntry struct { } func (r *RouteListEntry) CopiesInRoute() string { + if r.copiesInRoute == 0 { + return "" + } return fmt.Sprintf("+23%06d", r.copiesInRoute) } @@ -19,6 +22,9 @@ func (r *RouteListEntry) SetCopiesInRoute(copiesInRoute int) { } func (r *RouteListEntry) RampNumber() string { + if r.rampNumber == 0 { + return "" + } return fmt.Sprintf("+25%02d", r.rampNumber) } @@ -27,6 +33,9 @@ func (r *RouteListEntry) SetRampNumber(rampNumber int) { } func (r *RouteListEntry) RouteCode() string { + if r.routeCode == 0 { + return "" + } return fmt.Sprintf("+79%05d", r.routeCode) } diff --git a/title_info.go b/title_info.go index 8ecf1cb..22ff8c5 100644 --- a/title_info.go +++ b/title_info.go @@ -20,6 +20,9 @@ func (ti *TitleInfo) SetPrintObjectName(printObjectName string) { } func (ti *TitleInfo) AdditionalInfo() string { + if ti.additionalInfo == "" { + return "" + } return fmt.Sprintf("+08%-50s", ti.additionalInfo) } @@ -28,6 +31,9 @@ func (ti *TitleInfo) SetAdditionalInfo(additionalInfo string) { } func (ti *TitleInfo) PrintObjectColor() string { + if ti.printObjectColor == "" { + return "" + } return fmt.Sprintf("+94%-8s", ti.printObjectColor) } @@ -36,6 +42,9 @@ func (ti *TitleInfo) SetPrintObjectColor(printObjectColor string) { } func (ti *TitleInfo) CountryCode() string { + if ti.countryCode == "" { + return "" + } return fmt.Sprintf("+97%-2s", ti.countryCode) } @@ -52,6 +61,9 @@ func (ti *TitleInfo) SetPublicationDate(publicationDateString string) { } func (ti *TitleInfo) PublicationDate() string { + if ti.publicationDate.IsZero() { + return "" + } return fmt.Sprintf("+95%-6s", ti.publicationDate.Format(dateOutputFormat)) } @@ -72,6 +84,9 @@ func (ti *TitleInfo) Payload() string { } func (ti *TitleInfo) PrintObjectName() string { + if ti.printObjectName == "" { + return "" + } return fmt.Sprintf("+93%-12s", ti.printObjectName) }