def field_to_strategy(field, env):
"""Generate strategy for field."""
if SCALAR_MAPPINGS.get(field.type) is not None:
return apply_modifier(
strategy=SCALAR_MAPPINGS[field.type],
field=field
)
if field.type is FieldDescriptor.TYPE_ENUM:
return apply_modifier(
strategy=find_strategy_in_env(field.enum_type, env),
field=field
)
if field.type is FieldDescriptor.TYPE_MESSAGE:
field_options = field.message_type.GetOptions()
if field_options.deprecated:
return st.none()
if field_options.map_entry:
k, v = field.message_type.fields
return st.dictionaries(
field_to_strategy(k, env).filter(non_null),
field_to_strategy(v, env).filter(non_null)
)
return apply_modifier(
strategy=find_strategy_in_env(field.message_type, env),
field=field
)
raise Exception("Unhandled field {}.".format(field))