def get_child_type_keys(self): if ConfigTypeKind.is_closed_generic(self.kind): return self.type_param_keys elif ConfigTypeKind.has_fields(self.kind): return [field.type_key for field in self.fields] else: return []
def _get_next_level_refs(ref): # if a generic type, get type params # if a type with fields, get refs of the fields if ConfigTypeKind.is_closed_generic(ref.kind): return ref.type_param_refs elif (ConfigTypeKind.has_fields(ref.kind) and ref.fields): # still check fields because permissive return [field_meta.type_ref for field_meta in ref.fields]
def to_dauphin_config_type(config_schema_snapshot, config_type_key): check.inst_param(config_schema_snapshot, "config_schema_snapshot", ConfigSchemaSnapshot) check.str_param(config_type_key, "config_type_key") config_type_snap = config_schema_snapshot.get_config_snap(config_type_key) kind = config_type_snap.kind if kind == ConfigTypeKind.ENUM: return DauphinEnumConfigType(config_schema_snapshot, config_type_snap) elif ConfigTypeKind.has_fields(kind): return DauphinCompositeConfigType(config_schema_snapshot, config_type_snap) elif kind == ConfigTypeKind.ARRAY: return DauphinArrayConfigType(config_schema_snapshot, config_type_snap) elif kind == ConfigTypeKind.NONEABLE: return DauphinNullableConfigType(config_schema_snapshot, config_type_snap) elif kind == ConfigTypeKind.ANY or kind == ConfigTypeKind.SCALAR: return DauphinRegularConfigType(config_schema_snapshot, config_type_snap) elif kind == ConfigTypeKind.SCALAR_UNION: return DauphinScalarUnionConfigType(config_schema_snapshot, config_type_snap) else: check.failed("Should never reach")
def scaffold_type(config_type, skip_non_required=True): check.inst_param(config_type, "config_type", ConfigType) check.bool_param(skip_non_required, "skip_non_required") # Right now selectors and composites have the same # scaffolding logic, which might not be wise. if ConfigTypeKind.has_fields(config_type.kind): default_dict = {} for field_name, field in config_type.fields.items(): if skip_non_required and not field.is_required: continue default_dict[field_name] = scaffold_type(field.config_type, skip_non_required) return default_dict elif config_type.kind == ConfigTypeKind.ANY: return "AnyType" elif config_type.kind == ConfigTypeKind.SCALAR: defaults = {"String": "", "Int": 0, "Bool": True} return defaults[config_type.given_name] elif config_type.kind == ConfigTypeKind.ARRAY: return [] elif config_type.kind == ConfigTypeKind.ENUM: return "|".join( sorted(map(lambda v: v.config_value, config_type.enum_values))) else: check.failed("Do not know how to scaffold {type_name}".format( type_name=config_type.given_name))
def _recurse_through_generics(ref): yield ref if isinstance(ref, ConfigTypeMeta) and ConfigTypeKind.is_closed_generic( ref.kind): for type_param_ref in ref.type_param_refs: for inner_ref in _recurse_through_generics(type_param_ref): yield inner_ref
def scaffold_type(config_type, skip_optional=True): check.inst_param(config_type, 'config_type', ConfigType) check.bool_param(skip_optional, 'skip_optional') # Right now selectors and composites have the same # scaffolding logic, which might not be wise. if ConfigTypeKind.has_fields(config_type.kind): default_dict = {} for field_name, field in config_type.fields.items(): if skip_optional and field.is_optional: continue default_dict[field_name] = scaffold_type(field.config_type, skip_optional) return default_dict elif config_type.kind == ConfigTypeKind.ANY: return 'AnyType' elif config_type.kind == ConfigTypeKind.SCALAR: defaults = { 'String': '', 'Path': 'path/to/something', 'Int': 0, 'Bool': True } return defaults[config_type.given_name] elif config_type.kind == ConfigTypeKind.ARRAY: return [] elif config_type.kind == ConfigTypeKind.ENUM: return '|'.join( sorted(map(lambda v: v.config_value, config_type.enum_values))) else: check.failed('Do not know how to scaffold {type_name}'.format( type_name=config_type.given_name))
def __init__(self, config_type): check.inst_param(config_type, 'config_type', ConfigType) check.param_invariant(ConfigTypeKind.has_fields(config_type.kind), 'config_type') self._config_type = config_type super(DauphinCompositeConfigType, self).__init__(**_ctor_kwargs(config_type))
def get_field(self, name): check.str_param(name, 'name') check.invariant(ConfigTypeKind.has_fields(self.kind)) for f in self.fields: if f.name == name: return f check.failed('Field {name} not found'.format(name=name))
def meta_from_config_type(config_type): check.inst_param(config_type, 'config_type', ConfigType) return ConfigTypeMeta( key=config_type.key, given_name=config_type.given_name, kind=config_type.kind, description=config_type.description, type_param_refs=type_refs_of(config_type.type_params), enum_values=[ ConfigEnumValueMeta(ev.config_value, ev.description) for ev in config_type.enum_values ] if config_type.kind == ConfigTypeKind.ENUM else None, fields=[ meta_from_field(name, field) for name, field in config_type.fields.items() ] if ConfigTypeKind.has_fields(config_type.kind) else None, )
def to_dauphin_config_type(config_type): check.inst_param(config_type, 'config_type', ConfigType) kind = config_type.kind if kind == ConfigTypeKind.ENUM: return DauphinEnumConfigType(config_type) elif ConfigTypeKind.has_fields(kind): return DauphinCompositeConfigType(config_type) elif kind == ConfigTypeKind.ARRAY: return DauphinArrayConfigType(config_type) elif kind == ConfigTypeKind.NONEABLE: return DauphinNullableConfigType(config_type) elif kind == ConfigTypeKind.ANY or kind == ConfigTypeKind.SCALAR: return DauphinRegularConfigType(config_type) elif kind == ConfigTypeKind.SCALAR_UNION: return DauphinScalarUnionConfigType(config_type) else: check.failed('Should never reach')
def snap_from_config_type(config_type): check.inst_param(config_type, 'config_type', ConfigType) return ConfigTypeSnap( key=config_type.key, given_name=config_type.given_name, kind=config_type.kind, description=config_type.description, type_param_keys=[ct.key for ct in config_type.type_params] if config_type.type_params # jam scalar union types into type_param_keys else [config_type.scalar_type.key, config_type.non_scalar_type.key] if config_type.kind == ConfigTypeKind.SCALAR_UNION else None, enum_values=[ ConfigEnumValueSnap(ev.config_value, ev.description) for ev in config_type.enum_values ] if config_type.kind == ConfigTypeKind.ENUM else None, fields=[ snap_from_field(name, field) for name, field in config_type.fields.items() ] if ConfigTypeKind.has_fields(config_type.kind) else None, )
def type_ref_of(config_type): check.inst_param(config_type, 'config_type', ConfigType) if ConfigTypeKind.is_closed_generic(config_type.kind): return meta_from_config_type(config_type) else: return NonGenericTypeRefMeta(key=config_type.key)