From d10c76469d40f13e27739fd363145e89bf74c3e0 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Sat, 19 Jan 2019 19:23:31 +0100 Subject: identity: somewhat getting closer ! --- identity/common.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 identity/common.go (limited to 'identity/common.go') diff --git a/identity/common.go b/identity/common.go new file mode 100644 index 00000000..32dd3d9e --- /dev/null +++ b/identity/common.go @@ -0,0 +1,53 @@ +package identity + +import ( + "encoding/json" + "errors" + "fmt" + "strings" +) + +var ErrIdentityNotExist = errors.New("identity doesn't exist") + +type ErrMultipleMatch struct { + Matching []string +} + +func (e ErrMultipleMatch) Error() string { + return fmt.Sprintf("Multiple matching identities found:\n%s", strings.Join(e.Matching, "\n")) +} + +// Custom unmarshaling function to allow package user to delegate +// the decoding of an Identity and distinguish between an Identity +// and a Bare. +// +// If the given message has a "id" field, it's considered being a proper Identity. +func UnmarshalJSON(raw json.RawMessage) (Interface, error) { + // First try to decode as a normal Identity + var i Identity + + err := json.Unmarshal(raw, &i) + if err == nil && i.id != "" { + return &i, nil + } + + // abort if we have an error other than the wrong type + if _, ok := err.(*json.UnmarshalTypeError); err != nil && !ok { + return nil, err + } + + // Fallback on a legacy Bare identity + var b Bare + + err = json.Unmarshal(raw, &b) + if err == nil && (b.name != "" || b.login != "") { + return &b, nil + } + + // abort if we have an error other than the wrong type + if _, ok := err.(*json.UnmarshalTypeError); err != nil && !ok { + return nil, err + } + + return nil, fmt.Errorf("unknown identity type") +} -- cgit From 14b240af8fef269d2c1d5dde2fff192b656c50f3 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Sun, 20 Jan 2019 15:41:27 +0100 Subject: identity: more cleaning and fixes after a code review --- identity/common.go | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'identity/common.go') diff --git a/identity/common.go b/identity/common.go index 32dd3d9e..5301471a 100644 --- a/identity/common.go +++ b/identity/common.go @@ -51,3 +51,7 @@ func UnmarshalJSON(raw json.RawMessage) (Interface, error) { return nil, fmt.Errorf("unknown identity type") } + +type Resolver interface { + ResolveIdentity(id string) (Interface, error) +} -- cgit From 56c6147eb6012252cf0b723b9eb6d1e841fc2f94 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Fri, 1 Feb 2019 12:22:00 +0100 Subject: identity: more refactoring progress --- identity/common.go | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) (limited to 'identity/common.go') diff --git a/identity/common.go b/identity/common.go index 5301471a..00feaa2d 100644 --- a/identity/common.go +++ b/identity/common.go @@ -23,12 +23,13 @@ func (e ErrMultipleMatch) Error() string { // // If the given message has a "id" field, it's considered being a proper Identity. func UnmarshalJSON(raw json.RawMessage) (Interface, error) { - // First try to decode as a normal Identity - var i Identity + aux := &IdentityStub{} - err := json.Unmarshal(raw, &i) - if err == nil && i.id != "" { - return &i, nil + // First try to decode and load as a normal Identity + err := json.Unmarshal(raw, &aux) + if err == nil && aux.Id() != "" { + return aux, nil + // return identityResolver.ResolveIdentity(aux.Id) } // abort if we have an error other than the wrong type @@ -51,7 +52,3 @@ func UnmarshalJSON(raw json.RawMessage) (Interface, error) { return nil, fmt.Errorf("unknown identity type") } - -type Resolver interface { - ResolveIdentity(id string) (Interface, error) -} -- cgit From d2483d83dd52365741f51eca106aa18c4e8d6420 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Sat, 16 Feb 2019 17:32:30 +0100 Subject: identity: I can compile again !! --- identity/common.go | 1 - 1 file changed, 1 deletion(-) (limited to 'identity/common.go') diff --git a/identity/common.go b/identity/common.go index 00feaa2d..2f2b9042 100644 --- a/identity/common.go +++ b/identity/common.go @@ -29,7 +29,6 @@ func UnmarshalJSON(raw json.RawMessage) (Interface, error) { err := json.Unmarshal(raw, &aux) if err == nil && aux.Id() != "" { return aux, nil - // return identityResolver.ResolveIdentity(aux.Id) } // abort if we have an error other than the wrong type -- cgit