github.com/smugmug/godynamo@v0.0.0-20151122084750-7913028f6623/types/expected/expected.go (about) 1 // Package Expected implements the ExpectedAttributeValue data type. See: 2 // http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_ExpectedAttributeValue.html 3 // The documentation defines it in places as Expected and/or ExpectedAttributeValue. 4 package expected 5 6 import ( 7 "encoding/json" 8 "errors" 9 "github.com/smugmug/godynamo/types/attributevalue" 10 ) 11 12 type Expected map[string]*Constraints 13 14 func NewExpected() Expected { 15 e := make(Expected) 16 return e 17 } 18 19 type Constraints struct { 20 AttributeValueList []*attributevalue.AttributeValue 21 ComparisonOperator string 22 Value *attributevalue.AttributeValue 23 Exists *bool 24 } 25 26 func NewConstraints() *Constraints { 27 c := new(Constraints) 28 c.Exists = new(bool) 29 c.Value = attributevalue.NewAttributeValue() 30 c.AttributeValueList = make([]*attributevalue.AttributeValue, 0) 31 return c 32 } 33 34 type constraints Constraints 35 36 func (c *Constraints) UnmarshalJSON(data []byte) error { 37 if c == nil { 38 return errors.New("Expected.UnmarshalJSON: pointer receiver for unmarshal is nil") 39 } 40 var ci constraints 41 t_err := json.Unmarshal(data, &ci) 42 if t_err != nil { 43 return t_err 44 } 45 46 if c.Exists == nil { 47 c.Exists = new(bool) 48 } 49 50 if ci.Exists == nil { 51 c.Exists = nil 52 } else { 53 *c.Exists = *ci.Exists 54 } 55 56 if ci.ComparisonOperator != "" { 57 c.ComparisonOperator = ci.ComparisonOperator 58 } 59 60 if ci.Value != nil { 61 c.Value = attributevalue.NewAttributeValue() 62 cp_err := ci.Value.Copy(c.Value) 63 if cp_err != nil { 64 return cp_err 65 } 66 } 67 68 l_ci_avl := len(ci.AttributeValueList) 69 if l_ci_avl != 0 { 70 c.AttributeValueList = make([]*attributevalue.AttributeValue, l_ci_avl) 71 for i := range ci.AttributeValueList { 72 c.AttributeValueList[i] = attributevalue.NewAttributeValue() 73 cp_err := ci.AttributeValueList[i].Copy(c.AttributeValueList[i]) 74 if cp_err != nil { 75 return cp_err 76 } 77 } 78 } 79 return nil 80 } 81 82 func (c Constraints) MarshalJSON() ([]byte, error) { 83 var ci constraints 84 if c.Exists != nil { 85 ci.Exists = new(bool) 86 *ci.Exists = *c.Exists 87 } 88 ci.Value = c.Value 89 ci.AttributeValueList = c.AttributeValueList 90 ci.ComparisonOperator = c.ComparisonOperator 91 return json.Marshal(ci) 92 }