Initial commit

This commit is contained in:
Oliver Jakoubek 2020-05-27 21:23:36 +02:00
commit 0f717cd2af
16 changed files with 574 additions and 0 deletions

8
.idea/.gitignore vendored Normal file
View file

@ -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/

View file

@ -0,0 +1,7 @@
<component name="ProjectDictionaryState">
<dictionary name="jakoubek">
<words>
<w>ferag</w>
</words>
</dictionary>
</component>

8
.idea/feragstring.iml Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/misc.xml Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
</project>

8
.idea/modules.xml Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/feragstring.iml" filepath="$PROJECT_DIR$/.idea/feragstring.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

9
README.md Normal file
View file

@ -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
```

63
ferag.go Normal file
View file

@ -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
}

27
ferag_message.go Normal file
View file

@ -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
}
}

79
ferag_test.go Normal file
View file

@ -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)
}

3
go.mod Normal file
View file

@ -0,0 +1,3 @@
module jakoubek.net/feragstring
go 1.14

151
product_reference.go Normal file
View file

@ -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())
}

66
route_list.go Normal file
View file

@ -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())
}

32
title_end.go Normal file
View file

@ -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)
}

92
title_info.go Normal file
View file

@ -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
}

9
title_info_test.go Normal file
View file

@ -0,0 +1,9 @@
package feragstring
import "testing"
func TestNewTitleInfo(t *testing.T) {
ti := NewTitleInfo()
ti.titleName = "MOP10629"
}