Created
March 12, 2020 09:26
-
-
Save alexkappa/eb5a7c5f7bd70db3610ed7b4655dc686 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package management | |
import ( | |
"encoding/json" | |
) | |
type S struct { | |
// The discriminator used to undertsand which concrete type of Option we | |
// should use to unmarshal into. Can be either [foo, bar]. | |
Strategy *string `json:"strategy,omitempty"` | |
RawOptions json.RawMessage `json:"options,omitempty"` | |
Options interface{} `json:"-"` | |
} | |
func (s *S) MarshalJSON() ([]byte, error) { | |
type aS S | |
b, err := json.Marshal(s.Options) | |
if err != nil { | |
return nil, err | |
} | |
s.RawOptions = b | |
return json.Marshal((*aS)(s)) | |
} | |
func (s *S) UnmarshalJSON(b []byte) error { | |
type aS S | |
err := json.Unmarshal(b, (*aS)(s)) | |
if err != nil { | |
return err | |
} | |
if s.Strategy != nil { | |
var v interface{} | |
switch *s.Strategy { | |
case "foo": | |
v = &FooOptions{} | |
case "bar": | |
v = &BarOptions{} | |
} | |
err = json.Unmarshal(s.RawOptions, v) | |
if err != nil { | |
return err | |
} | |
s.Options = v | |
} | |
return nil | |
} | |
type FooOptions struct { | |
ClientID *string `json:"client_id,omitempty"` | |
ClientSecret *string `json:"client_secret,omitempty"` | |
Domain *string `json:"domain,omitempty"` | |
TenantDomain *string `json:"tenant_domain,omitempty"` | |
DomainAliases []interface{} `json:"domain_aliases,omitempty"` | |
} | |
type BarOptions struct { | |
ClientID *string `json:"client_id,omitempty"` | |
ClientSecret *string `json:"client_secret,omitempty"` | |
Contacts *bool `json:"contacts,omitempty"` | |
Calendar *bool `json:"calendar,omitempty"` | |
GooglePlus *bool `json:"google_plus,omitempty"` | |
PicasaWeb *bool `json:"picasa_web,omitempty"` | |
Youtube *bool `json:"youtube,omitempty"` | |
GoogleAffiliateNetwork *bool `json:"google_affiliate_network,omitempty"` | |
GoogleBooks *bool `json:"google_books,omitempty"` | |
Email *bool `json:"email,omitempty"` | |
Profile *bool `json:"profile,omitempty"` | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package management | |
import ( | |
"encoding/json" | |
"testing" | |
"gopkg.in/auth0.v3" | |
) | |
func TestJSONUnmarshal(t *testing.T) { | |
for _, j := range []string{ | |
`{"strategy": "foo", "options": {"domain": "example.com"}}`, | |
`{"strategy": "bar", "options": {"email": true, "profile": true}}`, | |
`{"options": {"baz": "true"}}`, | |
} { | |
var s S | |
err := json.Unmarshal([]byte(j), &s) | |
if err != nil { | |
t.Error(err) | |
} | |
if s.Strategy != nil { | |
switch *s.Strategy { | |
case "foo": | |
t.Logf("%s %s %s", *s.Strategy, *s.Options.(*FooOptions).Domain, s.RawOptions) | |
case "bar": | |
t.Logf("%s %t %t %s", *s.Strategy, *s.Options.(*BarOptions).Email, *s.Options.(*BarOptions).Profile, s.RawOptions) | |
default: | |
t.Logf("%s %s", s.Options.(map[string]interface{}), s.RawOptions) | |
} | |
} else { | |
t.Logf("%s", s.RawOptions) | |
} | |
} | |
} | |
func TestJSONMarshal(t *testing.T) { | |
for _, s := range []S{ | |
{ | |
Strategy: auth0.String("foo"), | |
Options: &FooOptions{ | |
Domain: auth0.String("example.com"), | |
}, | |
}, | |
{ | |
Strategy: auth0.String("bar"), | |
Options: &BarOptions{ | |
Email: auth0.Bool(true), | |
Profile: auth0.Bool(true), | |
}, | |
}, | |
{ | |
Options: map[string]interface{}{ | |
"baz": auth0.Bool(true), | |
}, | |
}, | |
} { | |
b, err := json.Marshal(&s) | |
if err != nil { | |
t.Error(err) | |
} | |
t.Logf("%s\n", b) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment