from__future__importannotationsfromtypingimportAny,Optionalfrompydantic.fieldsimportFieldasPydanticFieldfromts_ids_core.annotationsimportForeignKeyfromts_ids_core.base.ids_undefined_typeimportIDS_UNDEFINEDJSON_SCHEMA_EXTRA="json_schema_extra"# PascalCase used in function name in order to imitate ``pydantic.Field``, which is# wrapped by ``IdsField``.# pylint: disable=invalid-name#: JSON Schema key that marks a field of untransformed data; see :func:`IdsField`.IS_RAW_DATA="is_raw_data"
[docs]defIdsField(default:Any=IDS_UNDEFINED,primary_key:Optional[str]=None,is_raw_data:bool=False,**kwargs:Any,)->Any:""" Wrap the :func:`pydantic.Field` function such that, fields default to a sentinel value for undefined (a.k.a. unknown or missing) values. As such, the field definition is compatible with schema defined using :class:`ts_ids_core.base.ids_element.IdsElement`. :param default: The default value to use for the field. Default set to a global instance of :class:`ts_ids_core.base.ids_undefined_type.IdsUndefinedType` that's intended to be used as a singleton. :param primary_key: A JSON pointer to the primary key which this foreign key links to. For example, `IdsField(primary_key="/properties/samples/items/properties/pk")` would be used to define a foreign key which points to `samples[*].pk`. This pointer is validated when the `IdsElement.schema` method is called, validation fails if the target primary key field is not in the schema. :param is_raw_data: Whether the field and everything under it record data as written in the primary data, so that conventions for standardized values do not apply. For example, a `datacubes_raw` list beside the standard `datacubes` keeps the instrument's own axes and values before any conversion, so its quantities use `ValueUnit` and do not add `raw_value` and `raw_unit`. When true, the field's JSON Schema has `"is_raw_data": true`, and the ts-ids-validator raw unit check skips the field and everything under it. Set it on the field, not inside the component the field uses, so that a shared component needs no change. :param kwargs: All other keyword arguments are passed to :func:`pydantic.Field`. :return: The resulting :class:`pydantic.fields.FieldInfo` produced by :func:`pydantic.Field`. """if"default_factory"inkwargsanddefaultisnotIDS_UNDEFINED:raiseValueError("Cannot specify both `default` and `default_factory`.")schema_extra=kwargs.get(JSON_SCHEMA_EXTRA)ifisinstance(schema_extra,dict)andForeignKey.pk_reference_fieldinschema_extra:raiseValueError(f"The '{JSON_SCHEMA_EXTRA}' field {ForeignKey.pk_reference_field} should not be set when defining"f" IdsField. Instead define the primary_key reference with"f" 'IdsField(primary_key=<primary key reference>'.")ifprimary_keyisnotNoneoris_raw_data:ifschema_extraisNone:kwargs[JSON_SCHEMA_EXTRA]={}elifnotisinstance(schema_extra,dict):raiseValueError(f"The `json_schema_extra` argument for IdsField must be None or of type {type(dict)} when"f" passing a value for `primary_key` or `is_raw_data`.")ifprimary_keyisnotNone:kwargs[JSON_SCHEMA_EXTRA].update({ForeignKey.pk_reference_field:primary_key})ifis_raw_data:kwargs[JSON_SCHEMA_EXTRA][IS_RAW_DATA]=Trueif"default_factory"inkwargs:# If the default value is set via default_factory, use `pydantic` behavior.returnPydanticField(**kwargs)returnPydanticField(default=default,**kwargs)