Skip to content

Commit

Permalink
Fixed bug in dealing with single result.
Browse files Browse the repository at this point in the history
  • Loading branch information
rhomber committed Aug 10, 2019
1 parent be15f63 commit 645fd9d
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion auspost/model/postcode.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
package model

import "encoding/json"

type PostcodeSearchResult struct {
Localities Localities `json:"localities"`
}

type Localities struct {
Locality []Locality `json:"locality"`
RawLocalityWrapper struct {
RawLocality json.RawMessage `json:"locality"`
}
Locality []Locality `json:"-"`
}

func (l *Localities) UnmarshalJSON(b []byte) error {
if err := json.Unmarshal(b, &l.RawLocalityWrapper); err != nil {
return err
}
if l.RawLocalityWrapper.RawLocality[0] == '[' {
return json.Unmarshal(l.RawLocalityWrapper.RawLocality, &l.Locality)
}

var loc Locality
if err := json.Unmarshal(l.RawLocalityWrapper.RawLocality, &loc); err != nil {
return err
}

l.Locality = []Locality{loc}

return nil
}

type Locality struct {
Expand Down

0 comments on commit 645fd9d

Please sign in to comment.