diff --git a/payload.go b/payload.go index 69af8dc..5e2e2f2 100644 --- a/payload.go +++ b/payload.go @@ -93,6 +93,14 @@ type Payload struct { Data interface{} `json:"data"` } +func (p *Payload) addLang(code string, labels map[string]string) { + t := Translation{ + Language: code, + FieldsLabels: labels, + } + p.Lang = append(p.Lang, t) +} + func (p *Payload) SetData(data interface{}) { p.Data = data } @@ -269,49 +277,58 @@ func initMetadata(project string) error { return nil } +// TODO(marko): +// +// Currently supports only one hardcoded language... +// +// +// +// +// +// Metadata file ecpected format: +// +// [ payload A identifier ] +// key1 : value1 +// key2 : value2 +// ... +// [ payload B identifier ] +// key1 : value1 +// key2 : value2 +// ... func LoadMetadataFromFile(path string) error { lines, err := ReadFileLines(path) if err != nil { return err } - if len(metadata) > 0 { - metadata = nil - } metadata = make(map[string]Payload) var name string for i, l := range lines { - fmt.Println(l) - l = strings.TrimSpace(l) - if len(l) == 0 { + // skip empty lines + if l = trimSpaces(l); len(l) == 0 { continue } - if strings.HasPrefix(l, "[") { + if isWrappedWith(l, "[", "]") { name = strings.Trim(l, "[]") - metadata[name] = Payload{ - Lang: []Translation{ - Translation{ - Language: "sr", - FieldsLabels: make(map[string]string), - }, - }, - } + p := Payload{} + p.addLang("sr", make(map[string]string)) + metadata[name] = p continue } if name == "" { - return fmt.Errorf("webutility: LoadMetadataFromFile error on line %d: [no header][%s]\n", i+1, l) + return fmt.Errorf("webutility: LoadMetadataFromFile: error on line %d: [no header] [%s]\n", i+1, l) } parts := strings.Split(l, ":") - if len(parts) < 2 { - return fmt.Errorf("webutility: LoadMetadataFromFile error on line %d: [invalid format][%s]\n", i+1, l) + if len(parts) != 2 { + return fmt.Errorf("webutility: LoadMetadataFromFile: error on line %d: [invalid format] [%s]\n", i+1, l) } - k := strings.TrimSpace(parts[0]) - v := strings.TrimSpace(parts[1]) + k := trimSpaces(parts[0]) + v := trimSpaces(parts[1]) if v != "-" { metadata[name].Lang[0].FieldsLabels[k] = v } @@ -320,6 +337,14 @@ func LoadMetadataFromFile(path string) error { return nil } +func isWrappedWith(src, begin, end string) bool { + return strings.HasPrefix(src, begin) && strings.HasSuffix(src, end) +} + +func trimSpaces(s string) string { + return strings.TrimSpace(s) +} + func ReadFileLines(path string) ([]string, error) { f, err := os.Open(path) if err != nil {