Closed katylava closed 9 months ago
Thank you for opening this issue. We do provide a method on Context
to create the context directly from a dictionary. For your example, I think you could do:
pre_existing_dict = {
'firstName': 'Sandy',
'lastName': 'Smith',
'email': 'sandy@example.com',
'groups': ['Google', 'Microsoft'],
}
pre_existing_dict['key'] = 'context-key-123abc'
context = Context.from_dict(pre_existing_dict)
Is that an acceptable alternative?
Is that an acceptable alternative?
Yeah that works great! Is that preferred over context = Context(kind='user', key='somekey', attributes=pre_existing_dict)
?
Yes. You should avoid instantiating contexts directly and instead prefer our builder methods where available. There are some subtle differences between the two.
Thank you very much!
FWIW I had to set kind
in addition to key
. Otherwise I got [WARNING] - Context was invalid for flag evaluation (invalid data type for "kind"); returning default value
Is your feature request related to a problem? Please describe.
I wish ContextBuilder would take a dict of attributes to set. I have a dict of about 20 attributes, converted from an API response, and I don't want to write a
.set()
line for each attribute.Describe the solution you'd like
Instead of:
I'd like to be able to:
Describe alternatives you've considered
context = Context(kind='user', key='somekey', attributes=pre_existing_dict)
-- I'm fine with this but it's not documented so is there some reason not to do this?.set()
line for each attribute -- please, no.Additional context
I'm not talking about setting
kind
,name
, oranonymous
this way. Only the custom attributes.