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