123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246openPpxlibopenAst_builder.Defaultletconst~locvalue=[%expr`Assoc["const",`String[%eestring~locvalue]]]lettype_ref~loctype_name=letname=estring~loc("#/$defs/"^type_name)in[%expr`Assoc["$ref",`String[%ename]]]lettype_def~loctype_name=[%expr`Assoc["type",`String[%eestring~loctype_name]]]letnull~loc=[%expr`Assoc["type",`String"null"]]letchar~loc=[%expr`Assoc["type",`String"string";"minLength",`Int1;"maxLength",`Int1]]letoneOf~locvalues=[%expr`Assoc["oneOf",`List[%eelist~locvalues]]]letanyOf~locvalues=[%expr`Assoc["anyOf",`List[%eelist~locvalues]]]letarray_~loc?min_items?max_itemselement_type=letfields=List.filter_map(funx->x)[Some[%expr"type",`String"array"];Some[%expr"items",[%eelement_type]];(matchmin_itemswith|Somemin->Some[%expr"minItems",`Int[%eeint~locmin]]|None->None);(matchmax_itemswith|Somemax->Some[%expr"maxItems",`Int[%eeint~locmax]]|None->None);]in[%expr`Assoc[%eelist~locfields]]lettuple~locelements=[%expr`Assoc["type",`String"array";"prefixItems",`List[%eelist~locelements];"unevaluatedItems",`Boolfalse;"minItems",`Int[%eeint~loc(List.lengthelements)];"maxItems",`Int[%eeint~loc(List.lengthelements)];]]letenum~loctypvalues=matchtypwith|Sometyp->[%expr`Assoc["type",`String[%eestring~loctyp];"enum",`List[%eelist~locvalues]]]|None->[%expr`Assoc["enum",`List[%eelist~locvalues]]]letenum_string~locvalues=letvalues=List.map(funname->[%expr`String[%eestring~locname]])valuesinenum~loc(Some"string")valuesletnullable~locschema=matchschemawith|[%expr`Assoc["type",`String[%e?t]]]->[%expr`Assoc["type",`List[`String[%et];`String"null"]]]|s->[%expr`Assoc["anyOf",`List[[%es];`Assoc["type",`String"null"]]]]letannotation~loc(name,value)schema=matchschemawith|[%expr`Assoc[%e?fields]]->[%expr`Assoc(([%eestring~locname],[%evalue])::[%efields])]|s->sletformat~locformat=annotation~loc("format",[%expr`String[%eestring~locformat]])letmaximum~locmaximum=annotation~loc("maximum",maximum)letminimum~locminimum=annotation~loc("minimum",minimum)letdefault~locvalue=annotation~loc("default",value)letdescription~locdescriptionschema_expr=annotation~loc("description",[%expr`String[%eestring~locdescription]])schema_exprletvariants~loc?(as_string=false)?(compact_variants=false)constrs=letopt_description~locdescschema=matchdescwith|Somed->description~locdschema|None->schemainanyOf~loc(List.map(function|`Tag(name,typs,desc)->letschema=ifas_stringthenconst~locnameelseifcompact_variants&&typs=[]thenconst~locnameelsetuple~loc(const~locname::typs)inopt_description~locdescschema|`Inherittyp->typ)constrs)moduleAnnotation=structletadd_schema_attr(attr,node)fschema=matchAttribute.getattrnodewith|Somev->fvschema|None->schemaletadd_format~locattrcore_type=add_schema_attrattr(funfmtschema->matchcore_typewith|[%type:string]|[%type:bytes]|[%type:stringoption]|[%type:bytesoption]->format~locfmt.txtschema|_->Location.raise_errorf~loc:core_type.ptyp_loc"[@jsonschema.format] can only be applied to string or bytes types")letadd_maximum~locattrcore_type=add_schema_attrattr(funexprschema->matchcore_type,expr.pexp_descwith|[%type:int],Pexp_constant(Pconst_integer_)|[%type:int32],Pexp_constant(Pconst_integer_)|[%type:nativeint],Pexp_constant(Pconst_integer_)->maximum~loc[%expr`Int[%eexpr]]schema|[%type:float],Pexp_constant(Pconst_float_)->maximum~loc[%expr`Float[%eexpr]]schema|_->Location.raise_errorf~loc:core_type.ptyp_loc"[@jsonschema.maximum] can only be applied to numeric types")letadd_minimum~locattrcore_type=add_schema_attrattr(funexprschema->matchcore_type,expr.pexp_descwith|[%type:int],Pexp_constant(Pconst_integer_)|[%type:int32],Pexp_constant(Pconst_integer_)|[%type:nativeint],Pexp_constant(Pconst_integer_)->minimum~loc[%expr`Int[%eexpr]]schema|[%type:float],Pexp_constant(Pconst_float_)->minimum~loc[%expr`Float[%eexpr]]schema|_->Location.raise_errorf~loc:core_type.ptyp_loc"[@jsonschema.minimum] can only be applied to numeric types")letrecserializer_of_core_type~locct=matchctwith|[%type:int]|[%type:int32]|[%type:nativeint]->[%exprfunx->`Intx]|[%type:float]->[%exprfunx->`Floatx]|[%type:string]|[%type:bytes]->[%exprfunx->`Stringx]|[%type:bool]->[%exprfunx->`Boolx]|[%type:[%t?t]option]->lets=serializer_of_core_type~loctin[%exprfunx->matchxwith|None->`Null|Somev->[%es]v]|[%type:[%t?t]list]->lets=serializer_of_core_type~loctin[%exprfunxs->`List(Stdlib.List.map[%es]xs)]|[%type:[%t?t]array]->lets=serializer_of_core_type~loctin[%exprfunxs->`List(Stdlib.Array.to_list(Stdlib.Array.map[%es]xs))]|{ptyp_desc=Ptyp_tupletypes;_}->letserializers=List.map(serializer_of_core_type~loc)typesinletvars=List.mapi(funi_->Printf.sprintf"ppx_tuple_%d"i)typesinletpats=List.map(funv->ppat_var~loc{txt=v;loc})varsinletexprs=List.map2(funsv->[%expr[%es][%eevar~locv]])serializersvarsin[%exprfun[%pppat_tuple~locpats]->`List[%eelist~locexprs]]|{ptyp_desc=Ptyp_varname;_}->evar~locname|{ptyp_desc=Ptyp_constr(id,args);_}->letarg_serializers=List.map(serializer_of_core_type~loc)argsinletexp=type_constr_conv~locid~f:(funs->ifString.equals"t"then"to_json"elses^"_to_json")arg_serializersin[%exprPpx_deriving_jsonschema_runtime.classify[%eexp]]|_->Location.raise_errorf~loc:ct.ptyp_loc"[@jsonschema.default] cannot serialize this type. For non-primitive types, ensure a '<type>_to_json' function \
is in scope (e.g., add [@@deriving json] to the type definition)"letadd_default~locattrcore_type=add_schema_attrattr(funexprschema->letjson_value=matchexpr.pexp_descwith|Pexp_construct({txt=Lident"[]";_},None)->[%expr`List[]]|Pexp_construct({txt=Lident"None";_},None)->[%expr`Null]|_->letbase_type=matchcore_typewith|[%type:[%t?t]option]->t|t->tinletserializer=serializer_of_core_type~locbase_typein[%expr[%eserializer][%eexpr]]inmatchschemawith|[%expr`Assoc[%e?fields]]->[%expr`Assoc(("default",[%ejson_value])::[%efields])]|s->[%exprmatch[%es]with|`Assocppx_fields->`Assoc(("default",[%ejson_value])::ppx_fields)|ppx_other->ppx_other])letadd_description~locdesc_optschema=matchdesc_optwith|Somedesc->description~locdesc.txtschema|None->schemaletadd_annotations~loc?core_typeattrsschema=letrequire_core_typefield=matchcore_typewith|Somet->t|None->Location.raise_errorf~loc"[@jsonschema.attrs] '%s' requires a type context (use the individual [@jsonschema.%s] attribute instead)"fieldfieldinmatchattrswith|None->schema|Someexpr->matchexpr.pexp_descwith|Pexp_record(fields,None)->List.fold_left(funschema({txt=label;loc=label_loc},value)->matchlabelwith|Lident"description"->(matchvalue.pexp_descwith|Pexp_constant(Pconst_string(s,_,_))->description~locsschema|_->Location.raise_errorf~loc:value.pexp_loc"[@jsonschema.attrs] 'description' must be a string literal")|Lident"format"->letct=require_core_type"format"in(matchvalue.pexp_descwith|Pexp_constant(Pconst_string(s,_,_))->(matchctwith|[%type:string]|[%type:bytes]|[%type:stringoption]|[%type:bytesoption]->format~locsschema|_->Location.raise_errorf~loc:ct.ptyp_loc"[@jsonschema.attrs] 'format' can only be applied to string types")|_->Location.raise_errorf~loc:value.pexp_loc"[@jsonschema.attrs] 'format' must be a string literal")|Lident"maximum"->letct=require_core_type"maximum"in(matchctwith|[%type:int]|[%type:int32]|[%type:nativeint]->maximum~loc[%expr`Int[%evalue]]schema|[%type:float]->maximum~loc[%expr`Float[%evalue]]schema|_->Location.raise_errorf~loc:ct.ptyp_loc"[@jsonschema.attrs] 'maximum' can only be applied to numeric types")|Lident"minimum"->letct=require_core_type"minimum"in(matchctwith|[%type:int]|[%type:int32]|[%type:nativeint]->minimum~loc[%expr`Int[%evalue]]schema|[%type:float]->minimum~loc[%expr`Float[%evalue]]schema|_->Location.raise_errorf~loc:ct.ptyp_loc"[@jsonschema.attrs] 'minimum' can only be applied to numeric types")|Lidentname->Location.raise_errorf~loc:label_loc"[@jsonschema.attrs] unknown field: '%s'"name|_->Location.raise_errorf~loc:label_loc"[@jsonschema.attrs] expected a simple field name")schemafields|_->Location.raise_errorf~loc:expr.pexp_loc"[@jsonschema.attrs] expects a record expression: { field = value; ... }"end