Commit 8a070abe23ba5bc8149d707a10744ff18f42c8a8
1 parent
44c82dbac6
Exists in
master
improved
Showing
1 changed file
with
45 additions
and
20 deletions
Show diff stats
payload.go
... | ... | @@ -93,6 +93,14 @@ type Payload struct { |
93 | 93 | Data interface{} `json:"data"` |
94 | 94 | } |
95 | 95 | |
96 | +func (p *Payload) addLang(code string, labels map[string]string) { | |
97 | + t := Translation{ | |
98 | + Language: code, | |
99 | + FieldsLabels: labels, | |
100 | + } | |
101 | + p.Lang = append(p.Lang, t) | |
102 | +} | |
103 | + | |
96 | 104 | func (p *Payload) SetData(data interface{}) { |
97 | 105 | p.Data = data |
98 | 106 | } |
... | ... | @@ -269,49 +277,58 @@ func initMetadata(project string) error { |
269 | 277 | return nil |
270 | 278 | } |
271 | 279 | |
280 | +// TODO(marko): | |
281 | +// | |
282 | +// Currently supports only one hardcoded language... | |
283 | +// | |
284 | +// | |
285 | +// | |
286 | +// | |
287 | +// | |
288 | +// Metadata file ecpected format: | |
289 | +// | |
290 | +// [ payload A identifier ] | |
291 | +// key1 : value1 | |
292 | +// key2 : value2 | |
293 | +// ... | |
294 | +// [ payload B identifier ] | |
295 | +// key1 : value1 | |
296 | +// key2 : value2 | |
297 | +// ... | |
272 | 298 | func LoadMetadataFromFile(path string) error { |
273 | 299 | lines, err := ReadFileLines(path) |
274 | 300 | if err != nil { |
275 | 301 | return err |
276 | 302 | } |
277 | 303 | |
278 | - if len(metadata) > 0 { | |
279 | - metadata = nil | |
280 | - } | |
281 | 304 | metadata = make(map[string]Payload) |
282 | 305 | |
283 | 306 | var name string |
284 | 307 | for i, l := range lines { |
285 | - fmt.Println(l) | |
286 | - l = strings.TrimSpace(l) | |
287 | - if len(l) == 0 { | |
308 | + // skip empty lines | |
309 | + if l = trimSpaces(l); len(l) == 0 { | |
288 | 310 | continue |
289 | 311 | } |
290 | 312 | |
291 | - if strings.HasPrefix(l, "[") { | |
313 | + if isWrappedWith(l, "[", "]") { | |
292 | 314 | name = strings.Trim(l, "[]") |
293 | - metadata[name] = Payload{ | |
294 | - Lang: []Translation{ | |
295 | - Translation{ | |
296 | - Language: "sr", | |
297 | - FieldsLabels: make(map[string]string), | |
298 | - }, | |
299 | - }, | |
300 | - } | |
315 | + p := Payload{} | |
316 | + p.addLang("sr", make(map[string]string)) | |
317 | + metadata[name] = p | |
301 | 318 | continue |
302 | 319 | } |
303 | 320 | |
304 | 321 | if name == "" { |
305 | - return fmt.Errorf("webutility: LoadMetadataFromFile error on line %d: [no header][%s]\n", i+1, l) | |
322 | + return fmt.Errorf("webutility: LoadMetadataFromFile: error on line %d: [no header] [%s]\n", i+1, l) | |
306 | 323 | } |
307 | 324 | |
308 | 325 | parts := strings.Split(l, ":") |
309 | - if len(parts) < 2 { | |
310 | - return fmt.Errorf("webutility: LoadMetadataFromFile error on line %d: [invalid format][%s]\n", i+1, l) | |
326 | + if len(parts) != 2 { | |
327 | + return fmt.Errorf("webutility: LoadMetadataFromFile: error on line %d: [invalid format] [%s]\n", i+1, l) | |
311 | 328 | } |
312 | 329 | |
313 | - k := strings.TrimSpace(parts[0]) | |
314 | - v := strings.TrimSpace(parts[1]) | |
330 | + k := trimSpaces(parts[0]) | |
331 | + v := trimSpaces(parts[1]) | |
315 | 332 | if v != "-" { |
316 | 333 | metadata[name].Lang[0].FieldsLabels[k] = v |
317 | 334 | } |
... | ... | @@ -320,6 +337,14 @@ func LoadMetadataFromFile(path string) error { |
320 | 337 | return nil |
321 | 338 | } |
322 | 339 | |
340 | +func isWrappedWith(src, begin, end string) bool { | |
341 | + return strings.HasPrefix(src, begin) && strings.HasSuffix(src, end) | |
342 | +} | |
343 | + | |
344 | +func trimSpaces(s string) string { | |
345 | + return strings.TrimSpace(s) | |
346 | +} | |
347 | + | |
323 | 348 | func ReadFileLines(path string) ([]string, error) { |
324 | 349 | f, err := os.Open(path) |
325 | 350 | if err != nil { | ... | ... |