1package identity
2
3import (
4 "encoding/json"
5 "fmt"
6
7 bootstrap "github.com/MichaelMure/git-bug/entity/boostrap"
8)
9
10// Custom unmarshaling function to allow package user to delegate
11// the decoding of an Identity and distinguish between an Identity
12// and a Bare.
13//
14// If the given message has a "id" field, it's considered being a proper Identity.
15func UnmarshalJSON(raw json.RawMessage) (bootstrap.Identity, error) {
16 aux := &IdentityStub{}
17
18 // First try to decode and load as a normal Identity
19 err := json.Unmarshal(raw, &aux)
20 if err == nil && aux.Id() != "" {
21 return aux, nil
22 }
23
24 // abort if we have an error other than the wrong type
25 if _, ok := err.(*json.UnmarshalTypeError); err != nil && !ok {
26 return nil, err
27 }
28
29 return nil, fmt.Errorf("unknown identity type")
30}