-
Notifications
You must be signed in to change notification settings - Fork 2
/
dynamo.go
90 lines (79 loc) · 2.25 KB
/
dynamo.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
85
86
87
88
89
90
package main
import (
"github.com/awslabs/aws-sdk-go/aws"
"github.com/awslabs/aws-sdk-go/aws/awsutil"
"github.com/awslabs/aws-sdk-go/service/dynamodb"
"log"
"reflect"
)
type DynamoGTINSource struct {
AccessKey string
SecretKey string
creds aws.CredentialsProvider
Client *dynamodb.DynamoDB
}
func (dyn *DynamoGTINSource) Start() error {
dyn.Client = dynamodb.New(&aws.Config{Region: "us-west-2"})
return nil
}
func (dyn *DynamoGTINSource) Get(gtinId string) (*GTIN, error) {
gtin := >IN{GTIN: gtinId}
// Get SKU from GTIN
req := &dynamodb.GetItemInput{
TableName: aws.String("gtins"),
Key: &map[string]*dynamodb.AttributeValue{
"GTIN": &dynamodb.AttributeValue{S: aws.String(gtinId)},
},
}
resp, err := dyn.Client.GetItem(req)
if err != nil {
return gtin, err
} else if resp.Item == nil {
return gtin, ErrGTINNotFound
}
sku := awsutil.StringValue(resp)
if sku == "" {
return new(GTIN), ErrGTINNotFound
}
log.Printf("SKU:%v\n", sku)
// Now get SKU product info
req = &dynamodb.GetItemInput{
TableName: aws.String("skus"),
Key: &map[string]*dynamodb.AttributeValue{
"SKU": &dynamodb.AttributeValue{S: aws.String(sku)},
},
}
resp, err = dyn.Client.GetItem(req)
if err != nil {
return gtin, err
} else if resp.Item == nil {
return gtin, ErrGTINNotFound
}
// We have our data, load up the GTIN
item := resp.Item
productInfo := buildProductInfo(item)
productInfo.GTIN = gtinId
return productInfo, nil
}
func buildProductInfo(item *map[string]*dynamodb.AttributeValue) *GTIN {
keys := []string{"SKU", "Size1", "Size2", "Style", "OriginalRetail", "RecommendRetail",
"DivisionCode", "SubdivisionName", "SubdivisionName", "DepartmentCode", "DepartmentName",
"ClassCode", "ClassName", "SubclassCode", "SubclassName", "Description", "LongDescription",
"Color", "ColorCode", "SupplierCode", "SupplierName", "VendorProductName"}
p := >IN{}
for _, key := range keys {
itm := *item
if _, ok := itm[key]; ok {
reflect.ValueOf(p).Elem().FieldByName(key).SetString(awsutil.StringValue(itm))
}
}
// StoreDetail: []GTINStoreDetail{GTINStoreDetail{
// StoreNumber: "3",
// Price: StoreDetailPrice{
// RegularRetail: "120.00",
// SpecialRetail: "15.00",
// IsClearancePriced: true,
// },
// }},
return p
}