This repository has been archived by the owner on Oct 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcategories.go
84 lines (73 loc) · 1.95 KB
/
categories.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"errors"
"fmt"
"strings"
)
var CATEGORIES_VAL_SPLIT_CHAR = "+"
type CategoryLine struct {
org_id string
asset_name string
version string
field_key string
field_val string
}
func newCategoryLine(line map[string]string) (*CategoryLine, error) {
c := &CategoryLine{}
if val, ok := line["orgId"]; ok {
c.org_id = val
} else {
return nil, errors.New("\t[ERROR] orgId attribute is missing")
}
if val, ok := line["assetName"]; ok {
c.asset_name = val
} else {
return nil, errors.New("\t[ERROR] assetName attribute is missing")
}
if val, ok := line["version"]; ok {
c.version = val
} else {
return nil, errors.New("\t[ERROR] version attribute is missing")
}
if val, ok := line["fieldKey"]; ok {
c.field_key = val
} else {
return nil, errors.New("\t[ERROR] fieldKey attribute is missing")
}
if val, ok := line["fieldVal"]; ok {
c.field_val = val
} else {
return nil, errors.New("\t[ERROR] fieldVal attribute is missing")
}
return c, nil
}
func (c *ExchangeClient) handleCategories(file string) error {
categories, err := CSVFileToMap(file)
if err != nil {
return err
}
if len(categories) <= 0 {
fmt.Printf("\tcategory file is empty.\n")
return nil
}
for _, line := range categories {
cat, err := newCategoryLine(line)
if err != nil {
return err
}
if err := c.handleCategory(cat); err != nil {
return err
}
}
return nil
}
func (c *ExchangeClient) handleCategory(category *CategoryLine) error {
if category == nil {
return errors.New("\t[ERROR] category line is empty")
}
fmt.Printf("Processing Category \n\torg: %s\n\tasset: %s\n\tversion: %s\n\tkey: %s\n\tvalue: %s\n\n", category.org_id, category.asset_name, category.version, category.field_key, category.field_val)
if err := c.createCustomCategory(category.org_id, category.asset_name, category.version, category.field_key, strings.Split(category.field_val, CATEGORIES_VAL_SPLIT_CHAR)); err != nil {
return err
}
return nil
}