plain-lunch-50969
08/03/2022, 6:51 PMinterface{}
from a Session when using the golang bindings?echoing-caravan-91564
08/03/2022, 6:54 PM[]string
from metadata
groups := func (i []interface{}) []string {
var roles []string
for _, role := range i {
if v, ok := role.(string); ok {
roles = append(roles, v)
}
}
return roles
}(sess.Identity.MetadataPublic["roles"].([]interface{}))
plain-lunch-50969
08/03/2022, 7:04 PM%+v
print the Traits, I get map[email:<mailto:ops@example.net|ops@example.net> username:ops]
. When I attempt to typecast that to a struct with the appropriate fields, it fails.
type IdentityTraits struct {
Email string `json:"email"`
Username string `json:"username"`
}
traits, ok := id.Traits.(IdentityTraits)
if !ok {
return nil, fmt.Errorf("Unknown identity traits type")
}
ok
always is false here. I've tried other typecasts as well like map[string]string
.plain-lunch-50969
08/03/2022, 8:26 PMmap[string]interface{}
. If you have a trait of "email":
traits, ok := id.Traits.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("Unknown identity traits type")
}
email := ""
if val, ok := traits["email"]; ok {
r, ok := val.(string)
if ok {
email = r
}
}
// email here is the value in Traits if it exists.
echoing-caravan-91564
08/03/2022, 8:27 PMmap[string]string
or map[string]interface{}
so you would need to do something like
traits, _ := id.Traits.(map[string]string)
email, _ := traits["email"]
or
email := id.Traits.(map[string]interface{})["email"].(string)
where you first convert id.Traits
from interface{}
to either map[string]interface{}/string
echoing-caravan-91564
08/03/2022, 8:27 PMplain-lunch-50969
08/03/2022, 8:27 PMplain-lunch-50969
08/03/2022, 8:27 PMplain-lunch-50969
08/03/2022, 8:28 PMplain-lunch-50969
08/03/2022, 8:28 PMechoing-caravan-91564
08/03/2022, 8:29 PM// TODO make radar model of user traits as defined in ory identity schema & metadata we define ourselves
// it would be helpful to have a fully capable go model if we want more control
// schema isn't expected to change, so we don't need to automate it for now
// easy way is to use json can just be parsed to Go <https://transform.tools/json-to-go>
// we could use @go:generate (<https://blog.carlmjohnson.net/post/2016-11-27-how-to-use-go-generate/>)
// use go toolchain to guarantee install of json-to-go <https://github.com/samit22/json-go>
//
// a good generator would
// - be checked on PR that it matches generated output similar to protos
// - have add ory to our IaaS and use ci to regenerate on change
//
// I don't know why I spent the 10 min writing this out
plain-lunch-50969
08/03/2022, 8:30 PMechoing-caravan-91564
08/03/2022, 8:30 PMplain-lunch-50969
08/03/2022, 8:30 PMIdentity.Traits
?plain-lunch-50969
08/03/2022, 8:31 PMplain-lunch-50969
08/03/2022, 8:39 PMory.AdminCreateIdentityBody
which makes sense.plain-lunch-50969
08/03/2022, 8:45 PMplain-lunch-50969
08/03/2022, 8:46 PMmap[string]interface{}
must come from the auto-generated api. So this is in the API spec somewhere I guess.plain-lunch-50969
08/03/2022, 8:48 PMsteep-lamp-91158
steep-lamp-91158
type Traits struct { Email string `json:"email"` }
enc, err := json.Marshal(id.Traits)
var traits Traits
err = json.Unmarshal(enc, &traits)
steep-lamp-91158
steep-lamp-91158
json.RawMessage
instead of map[string]interface{}
, but I don't know of a way to do thatsteep-lamp-91158
steep-lamp-91158
steep-lamp-91158
plain-lunch-50969
08/04/2022, 1:10 PM