Skip to content

Commit

Permalink
Merge pull request #236 from seqsense/avoid-panic-on-get-accepted
Browse files Browse the repository at this point in the history
shadow: Avoid panic on updateAccepted
  • Loading branch information
kamatama41 authored Mar 15, 2021
2 parents 6b88880 + cfbf757 commit ec4e064
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
15 changes: 2 additions & 13 deletions shadow/shadow.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (s *shadow) handleResponse(r interface{}) {
}

func (s *shadow) getAccepted(msg *mqtt.Message) {
doc := &ThingDocument{}
doc := newThingDocument()
if err := json.Unmarshal(msg.Payload, doc); err != nil {
s.handleError(ioterr.New(err, "unmarshaling thing document"))
return
Expand Down Expand Up @@ -178,18 +178,7 @@ func New(ctx context.Context, cli awsiotdev.Device, opt ...Option) (Shadow, erro
cli: cli,
thingName: cli.ThingName(),
opts: &opts,
doc: &ThingDocument{
State: ThingState{
Desired: NestedState{},
Reported: NestedState{},
Delta: NestedState{},
},
Metadata: ThingStateMetadata{
Desired: NestedMetadata{},
Reported: NestedMetadata{},
Delta: NestedMetadata{},
},
},
doc: newThingDocument(),

chResps: make(map[string]chan interface{}),
}
Expand Down
13 changes: 11 additions & 2 deletions shadow/shadow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ func TestHandlers(t *testing.T) {
State: ThingState{
Desired: NestedState{"key": "value_init"},
},
Metadata: ThingStateMetadata{
Delta: NestedMetadata{},
},
Version: 2,
Timestamp: 12345,
},
Expand All @@ -159,8 +162,14 @@ func TestHandlers(t *testing.T) {
}
expectedDoc := &ThingDocument{
State: ThingState{
Desired: NestedState{"key2": "value2"},
Delta: NestedState{"key2": "value2"},
Desired: NestedState{"key2": "value2"},
Reported: NestedState{},
Delta: NestedState{"key2": "value2"},
},
Metadata: ThingStateMetadata{
Desired: NestedMetadata{},
Reported: NestedMetadata{},
Delta: NestedMetadata{},
},
Version: 3,
Timestamp: 12346,
Expand Down
15 changes: 15 additions & 0 deletions shadow/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,21 @@ type thingDelta struct {
Timestamp int `json:"timestamp,omitempty"`
}

func newThingDocument() *ThingDocument {
return &ThingDocument{
State: ThingState{
Desired: NestedState{},
Reported: NestedState{},
Delta: NestedState{},
},
Metadata: ThingStateMetadata{
Desired: NestedMetadata{},
Reported: NestedMetadata{},
Delta: NestedMetadata{},
},
}
}

func (s *ThingDocument) update(state *thingDocumentRaw) error {
if s.Version > state.Version {
// Received an old version; just ignore it.
Expand Down

0 comments on commit ec4e064

Please sign in to comment.