From 0f717cd2af0052a5929809c400e340ebc530dac8 Mon Sep 17 00:00:00 2001 From: Oliver Jakoubek Date: Wed, 27 May 2020 21:23:36 +0200 Subject: [PATCH] Initial commit --- .idea/.gitignore | 8 ++ .idea/dictionaries/jakoubek.xml | 7 ++ .idea/feragstring.iml | 8 ++ .idea/misc.xml | 6 ++ .idea/modules.xml | 8 ++ .idea/vcs.xml | 6 ++ README.md | 9 ++ ferag.go | 63 +++++++++++++ ferag_message.go | 27 ++++++ ferag_test.go | 79 +++++++++++++++++ go.mod | 3 + product_reference.go | 151 ++++++++++++++++++++++++++++++++ route_list.go | 66 ++++++++++++++ title_end.go | 32 +++++++ title_info.go | 92 +++++++++++++++++++ title_info_test.go | 9 ++ 16 files changed, 574 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/dictionaries/jakoubek.xml create mode 100644 .idea/feragstring.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 README.md create mode 100644 ferag.go create mode 100644 ferag_message.go create mode 100644 ferag_test.go create mode 100644 go.mod create mode 100644 product_reference.go create mode 100644 route_list.go create mode 100644 title_end.go create mode 100644 title_info.go create mode 100644 title_info_test.go diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/dictionaries/jakoubek.xml b/.idea/dictionaries/jakoubek.xml new file mode 100644 index 0000000..d96a5df --- /dev/null +++ b/.idea/dictionaries/jakoubek.xml @@ -0,0 +1,7 @@ + + + + ferag + + + \ No newline at end of file diff --git a/.idea/feragstring.iml b/.idea/feragstring.iml new file mode 100644 index 0000000..c956989 --- /dev/null +++ b/.idea/feragstring.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..28a804d --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..31da237 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..f1afcdd --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# 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. + +## Installation + +```bash +go get -u github.com/jakoubek/feragstring +``` \ No newline at end of file diff --git a/ferag.go b/ferag.go new file mode 100644 index 0000000..c9b2c84 --- /dev/null +++ b/ferag.go @@ -0,0 +1,63 @@ +package feragstring + +const ( + dateInputFormatISO = "2006-01-02" + dateOutputFormat = "060102" + linebreak = "\r\n" +) + +type FeragString struct { + TitleInfo *TitleInfo + TitleEnd *TitleEnd + ProductReferences []*ProductReference + ProductReferencesNr int + RouteListEntries []*RouteListEntry + RouteListEntryCount int +} + +func NewFeragString() *FeragString { + fs := FeragString{ + TitleInfo: NewTitleInfo(), + TitleEnd: NewTitleEnd(), + } + return &fs +} + +func (fs *FeragString) SetTitleName(titleName string) { + fs.TitleInfo.SetTitleName(titleName) + fs.TitleEnd.SetTitleName(titleName) +} + +func (fs *FeragString) PrintOut() string { + info := fs.TitleInfo.Message() + + for _, pr := range fs.ProductReferences { + info += pr.Message() + } + + for _, rl := range fs.RouteListEntries { + info += rl.Message() + } + + info += fs.TitleEnd.Message() + return info +} + +func (fs *FeragString) AddProductReference(pr *ProductReference) error { + fs.ProductReferencesNr++ + pr.SetProductReferenceNumber(fs.ProductReferencesNr) + if pr.productReferenceNumber == 1 && pr.productUsageType == 0 { + pr.SetProductUsageType(1) + } + fs.ProductReferences = append(fs.ProductReferences, pr) + return nil +} + +func (fs *FeragString) AddRouteListEntry(rl *RouteListEntry) error { + fs.RouteListEntryCount++ + if rl.routeCode == 0 { + rl.SetRouteCode(fs.RouteListEntryCount) + } + fs.RouteListEntries = append(fs.RouteListEntries, rl) + return nil +} \ No newline at end of file diff --git a/ferag_message.go b/ferag_message.go new file mode 100644 index 0000000..c1acd56 --- /dev/null +++ b/ferag_message.go @@ -0,0 +1,27 @@ +package feragstring + +import "fmt" + +type FeragMessage struct { + messageStart string + messageEnd string +} + +func (fm *FeragMessage) getMessageStart() string { + return fmt.Sprintf("%%%s", fm.messageStart) +} + +func (fm *FeragMessage) getMessageEnd() string { + return fmt.Sprintf("%s", fm.messageEnd) +} + +type MessageTemplateFunc func(*FeragMessage, string) string + +func (fm *FeragMessage) MessageTemplate() MessageTemplateFunc { + return func(fm *FeragMessage, s string) string { + message := fm.getMessageStart() + message += s + message += fm.getMessageEnd() + linebreak + return message + } +} diff --git a/ferag_test.go b/ferag_test.go new file mode 100644 index 0000000..7093632 --- /dev/null +++ b/ferag_test.go @@ -0,0 +1,79 @@ +package feragstring + +import ( + "fmt" + "io/ioutil" + "testing" +) + +func TestNewFeragString(t *testing.T) { + fs := NewFeragString() + fs.SetTitleName("MOP10629") + + fs.TitleInfo.SetPrintObjectName("MOP1") + fs.TitleInfo.SetPublicationDate("2019-06-29") + fs.TitleInfo.SetCountryCode("13") + fs.TitleInfo.SetPrintObjectColor("03368448") + + // Produktreferenzen + pr1 := NewProductReference() + pr1.SetProductName("HP MOP1 KERN") + pr1.SetCopiesAssigned(56433) + pr1.SetSupervision(1) + pr1.SetOverlap(5) + mp := MissingParameter{ + missingRate: 1, + missingSize: 1, + } + pr1.SetMissingParameter(mp) + pr1.SetIssueReference("HPMOP1 K") + fs.AddProductReference(pr1) + // ------------------------------------------ + pr2 := NewProductReference() + pr2.SetProductName("Beilage Rossmann ET 29.06.19") + pr2.SetProductUsageType(3) + pr2.SetCopiesAssigned(36000) + pr2.SetProductWeight(180) + pr2.SetSupervision(1) + pr2.SetOverlap(1) + pr2.SetIssueReference("19004378") + fs.AddProductReference(pr2) + // Produktreferenzen - Ende + + // Route Lists + rl1 := NewRouteListEntry() + rl1.SetRouteName("UT002223") + rl1.SetCopiesInRoute(309) + fs.AddRouteListEntry(rl1) + rl2 := NewRouteListEntry() + rl2.SetRouteName("UT888888")// Route Lists - Ende + rl2.SetCopiesInRoute(80) + fs.AddRouteListEntry(rl2) + producedContent := fs.PrintOut() + + filename := "D:\\TEMP\\Feragstring\\ferag.txt" + err := ioutil.WriteFile(filename, []byte(producedContent), 0644) + if err != nil { + panic(err) + } + + testcontent := getTestFileContent("D:\\TEMP\\Feragstring\\test.txt") + fmt.Println("=======================") + fmt.Println("TEST:") + fmt.Println(testcontent) + fmt.Println("-----------------------") + fmt.Println("PRODUCED:") + fmt.Println(producedContent) + fmt.Println("=======================") + if testcontent != producedContent { + t.Errorf("Files don't match!") + } +} + +func getTestFileContent(filename string) string { + testcontent, err := ioutil.ReadFile(filename) + if err != nil { + panic(err) + } + return string(testcontent) +} \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..36ae5e0 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module jakoubek.net/feragstring + +go 1.14 diff --git a/product_reference.go b/product_reference.go new file mode 100644 index 0000000..e04593e --- /dev/null +++ b/product_reference.go @@ -0,0 +1,151 @@ +package feragstring + +import "fmt" + +type ProductReference struct { + FeragMessage + productReferenceNumber int + productName string + productUsageType int + sheetLayers int + copiesAssigned int + productWeight int + supervision int + overlap int + feedingMode int + scatter int + missingParameter MissingParameter + issueReference string +} + +type MissingParameter struct { + missingRate int + missingSize int +} + +func (pr *ProductReference) IssueReference() string { + return fmt.Sprintf("+99195%-8s", pr.issueReference) +} + +func (pr *ProductReference) SetIssueReference(issueReference string) { + pr.issueReference = issueReference +} + +func (pr *ProductReference) MissingParameter() string { + return fmt.Sprintf("+99105%04d%08d", pr.missingParameter.missingRate, pr.missingParameter.missingSize) +} + +func (pr *ProductReference) SetMissingParameter(missingParameter MissingParameter) { + pr.missingParameter = missingParameter +} + +func (pr *ProductReference) Scatter() string { + return fmt.Sprintf("+99102%06d", pr.scatter) +} + +func (pr *ProductReference) SetScatter(scatter int) { + pr.scatter = scatter +} + +func (pr *ProductReference) FeedingMode() string { + return fmt.Sprintf("+99101%02d", pr.feedingMode) +} + +func (pr *ProductReference) SetFeedingMode(feedingMode int) { + pr.feedingMode = feedingMode +} + +func (pr *ProductReference) Overlap() string { + return fmt.Sprintf("+45%02d", pr.overlap) +} + +func (pr *ProductReference) SetOverlap(overlap int) { + pr.overlap = overlap +} + +func (pr *ProductReference) Supervision() string { + return fmt.Sprintf("+44%02d", pr.supervision) +} + +func (pr *ProductReference) SetSupervision(supervision int) { + pr.supervision = supervision +} + +func (pr *ProductReference) ProductWeight() string { + return fmt.Sprintf("+36%05d", pr.productWeight) +} + +func (pr *ProductReference) SetProductWeight(productWeight int) { + pr.productWeight = productWeight +} + +func (pr *ProductReference) CopiesAssigned() string { + return fmt.Sprintf("+02%08d", pr.copiesAssigned) +} + +func (pr *ProductReference) SetCopiesAssigned(copiesAssigned int) { + pr.copiesAssigned = copiesAssigned +} + +func (pr *ProductReference) SheetLayers() string { + return fmt.Sprintf("+35%04d", pr.sheetLayers) +} + +func (pr *ProductReference) SetSheetLayers(sheetLayers int) { + pr.sheetLayers = sheetLayers +} + +func (pr *ProductReference) ProductUsageType() string { + return fmt.Sprintf("+55%02d", pr.productUsageType) +} + +func (pr *ProductReference) SetProductUsageType(productUsageType int) { + pr.productUsageType = productUsageType +} + +func (pr *ProductReference) ProductName() string { + return fmt.Sprintf("+42%-30s", pr.productName) +} + +func (pr *ProductReference) SetProductName(productName string) { + pr.productName = productName +} + +func (pr *ProductReference) ProductReferenceNumber() string { + return fmt.Sprintf("+41%02d", pr.productReferenceNumber) +} + +func (pr *ProductReference) SetProductReferenceNumber(productReferenceNumber int) { + pr.productReferenceNumber = productReferenceNumber +} + +func NewProductReference() *ProductReference { + pr := ProductReference{ + FeragMessage: FeragMessage{"2450", "!"}, + missingParameter: MissingParameter{ + missingRate: 0, + missingSize: 1, + }, + } + return &pr +} + +func (pr *ProductReference) Payload() string { + data := pr.ProductReferenceNumber() + data += pr.ProductName() + data += pr.ProductUsageType() + data += pr.SheetLayers() + data += pr.CopiesAssigned() + data += pr.ProductWeight() + data += pr.Supervision() + data += pr.Overlap() + data += pr.FeedingMode() + data += pr.MissingParameter() + data += pr.IssueReference() + return data +} + +func (pr *ProductReference) Message() string { + message := pr.FeragMessage.MessageTemplate() + return message(&pr.FeragMessage, pr.Payload()) +} \ No newline at end of file diff --git a/route_list.go b/route_list.go new file mode 100644 index 0000000..ea35adb --- /dev/null +++ b/route_list.go @@ -0,0 +1,66 @@ +package feragstring + +import "fmt" + +type RouteListEntry struct { + FeragMessage + routeName string + routeCode int + rampNumber int + copiesInRoute int +} + +func (r *RouteListEntry) CopiesInRoute() string { + return fmt.Sprintf("+23%06d", r.copiesInRoute) +} + +func (r *RouteListEntry) SetCopiesInRoute(copiesInRoute int) { + r.copiesInRoute = copiesInRoute +} + +func (r *RouteListEntry) RampNumber() string { + return fmt.Sprintf("+25%02d", r.rampNumber) +} + +func (r *RouteListEntry) SetRampNumber(rampNumber int) { + r.rampNumber = rampNumber +} + +func (r *RouteListEntry) RouteCode() string { + return fmt.Sprintf("+79%05d", r.routeCode) +} + +func (r *RouteListEntry) SetRouteCode(routeCode int) { + r.routeCode = routeCode +} + +func (r *RouteListEntry) RouteName() string { + return fmt.Sprintf("+11%-13s", r.routeName) +} + +func (r *RouteListEntry) SetRouteName(routeName string) { + r.routeName = routeName +} + +func NewRouteListEntry() *RouteListEntry { + rl := RouteListEntry{ + FeragMessage: FeragMessage{ + messageStart: "2401", + messageEnd: "!", + }, + } + return &rl +} + +func (rl *RouteListEntry) Payload() string { + data := rl.RouteName() + data += rl.RouteCode() + data += rl.RampNumber() + data += rl.CopiesInRoute() + return data +} + +func (rl *RouteListEntry) Message() string { + message := rl.FeragMessage.MessageTemplate() + return message(&rl.FeragMessage, rl.Payload()) +} diff --git a/title_end.go b/title_end.go new file mode 100644 index 0000000..fdb7a6e --- /dev/null +++ b/title_end.go @@ -0,0 +1,32 @@ +package feragstring + +import "fmt" + +type TitleEnd struct { + FeragMessage + titleName string +} + +func (ti *TitleEnd) PrintObjectName() string { + return fmt.Sprintf("+40%-8s", ti.titleName) +} + +func (ti *TitleEnd) SetTitleName(titleName string) { + ti.titleName = titleName +} + +func NewTitleEnd() *TitleEnd { + t := TitleEnd{ + FeragMessage: FeragMessage{ + messageStart: "2441", + messageEnd: "!", + }, + } + return &t +} + +func (ti *TitleEnd) Message() string { + message := ti.FeragMessage.MessageTemplate() + payload := fmt.Sprintf("+40%-8s", ti.titleName) + return message(&ti.FeragMessage, payload) +} \ No newline at end of file diff --git a/title_info.go b/title_info.go new file mode 100644 index 0000000..8ecf1cb --- /dev/null +++ b/title_info.go @@ -0,0 +1,92 @@ +package feragstring + +import ( + "fmt" + "time" +) + +type TitleInfo struct { + FeragMessage + printObjectName string + titleName string + publicationDate time.Time + countryCode string + printObjectColor string + additionalInfo string +} + +func (ti *TitleInfo) SetPrintObjectName(printObjectName string) { + ti.printObjectName = printObjectName +} + +func (ti *TitleInfo) AdditionalInfo() string { + return fmt.Sprintf("+08%-50s", ti.additionalInfo) +} + +func (ti *TitleInfo) SetAdditionalInfo(additionalInfo string) { + ti.additionalInfo = additionalInfo +} + +func (ti *TitleInfo) PrintObjectColor() string { + return fmt.Sprintf("+94%-8s", ti.printObjectColor) +} + +func (ti *TitleInfo) SetPrintObjectColor(printObjectColor string) { + ti.printObjectColor = printObjectColor +} + +func (ti *TitleInfo) CountryCode() string { + return fmt.Sprintf("+97%-2s", ti.countryCode) +} + +func (ti *TitleInfo) SetCountryCode(countryCode string) { + ti.countryCode = countryCode +} + +func (ti *TitleInfo) SetPublicationDate(publicationDateString string) { + parsedDate, err := time.Parse(dateInputFormatISO, publicationDateString) + if err != nil { + panic(err) + } + ti.publicationDate = parsedDate +} + +func (ti *TitleInfo) PublicationDate() string { + return fmt.Sprintf("+95%-6s", ti.publicationDate.Format(dateOutputFormat)) +} + + +func (ti *TitleInfo) Message() string { + message := ti.FeragMessage.MessageTemplate() + return message(&ti.FeragMessage, ti.Payload()) +} + +func (ti *TitleInfo) Payload() string { + data := ti.PrintObjectName() + data += ti.TitleName() + data += ti.PublicationDate() + data += ti.CountryCode() + data += ti.PrintObjectColor() + data += ti.AdditionalInfo() + return data +} + +func (ti *TitleInfo) PrintObjectName() string { + return fmt.Sprintf("+93%-12s", ti.printObjectName) +} + +func (ti *TitleInfo) TitleName() string { + return fmt.Sprintf("+40%-8s", ti.titleName) +} + +func (ti *TitleInfo) SetTitleName(titleName string) { + ti.titleName = titleName +} + +func NewTitleInfo() *TitleInfo { + t := TitleInfo{ + FeragMessage: FeragMessage{"2440", "!"}, + } + return &t +} + diff --git a/title_info_test.go b/title_info_test.go new file mode 100644 index 0000000..5a165e4 --- /dev/null +++ b/title_info_test.go @@ -0,0 +1,9 @@ +package feragstring + +import "testing" + +func TestNewTitleInfo(t *testing.T) { + ti := NewTitleInfo() + ti.titleName = "MOP10629" + +}