(* Auto-generated from "type_aliases.atd" by atdml. *)

type tag_list = string list

val tag_list_of_yojson : Yojson.Safe.t -> tag_list
val tag_list_of_jsonlike : Atd_jsonlike.AST.t -> tag_list
val yojson_of_tag_list : tag_list -> Yojson.Safe.t
val tag_list_of_json : string -> tag_list
val json_of_tag_list : tag_list -> string

module Tag_list : sig
  type nonrec t = tag_list
  val of_yojson : Yojson.Safe.t -> t
  val of_jsonlike : Atd_jsonlike.AST.t -> t
  val to_yojson : t -> Yojson.Safe.t
  val of_json : string -> t
  val to_json : t -> string
end

type score = private float

val create_score : float -> score
val score_of_yojson : Yojson.Safe.t -> score
val score_of_jsonlike : Atd_jsonlike.AST.t -> score
val yojson_of_score : score -> Yojson.Safe.t
val score_of_json : string -> score
val json_of_score : score -> string

module Score : sig
  type nonrec t = score
  val create : float -> t
  val of_yojson : Yojson.Safe.t -> t
  val of_jsonlike : Atd_jsonlike.AST.t -> t
  val to_yojson : t -> Yojson.Safe.t
  val of_json : string -> t
  val to_json : t -> string
end

type opt_name = string option

val opt_name_of_yojson : Yojson.Safe.t -> opt_name
val opt_name_of_jsonlike : Atd_jsonlike.AST.t -> opt_name
val yojson_of_opt_name : opt_name -> Yojson.Safe.t
val opt_name_of_json : string -> opt_name
val json_of_opt_name : opt_name -> string

module Opt_name : sig
  type nonrec t = opt_name
  val of_yojson : Yojson.Safe.t -> t
  val of_jsonlike : Atd_jsonlike.AST.t -> t
  val to_yojson : t -> Yojson.Safe.t
  val of_json : string -> t
  val to_json : t -> string
end

type id = private string

val create_id : string -> id
val id_of_yojson : Yojson.Safe.t -> id
val id_of_jsonlike : Atd_jsonlike.AST.t -> id
val yojson_of_id : id -> Yojson.Safe.t
val id_of_json : string -> id
val json_of_id : id -> string

module Id : sig
  type nonrec t = id
  val create : string -> t
  val of_yojson : Yojson.Safe.t -> t
  val of_jsonlike : Atd_jsonlike.AST.t -> t
  val to_yojson : t -> Yojson.Safe.t
  val of_json : string -> t
  val to_json : t -> string
end

type all = {
  id: id;
  score: score;
  tags: tag_list;
  name: opt_name;
}

val create_all : id:id -> score:score -> tags:tag_list -> name:opt_name -> unit -> all
val all_of_yojson : Yojson.Safe.t -> all
val all_of_jsonlike : Atd_jsonlike.AST.t -> all
val yojson_of_all : all -> Yojson.Safe.t
val all_of_json : string -> all
val json_of_all : all -> string

module All : sig
  type nonrec t = all
  val create : id:id -> score:score -> tags:tag_list -> name:opt_name -> unit -> t
  val of_yojson : Yojson.Safe.t -> t
  val of_jsonlike : Atd_jsonlike.AST.t -> t
  val to_yojson : t -> Yojson.Safe.t
  val of_json : string -> t
  val to_json : t -> string
end

--- ml ---
(* Auto-generated from "type_aliases.atd" by atdml. *)
[@@@ocaml.warning "-27-32-33-35-39"]

(* Inlined runtime — no external dependency needed. *)
module Atdml_runtime = struct
  (* Returns true iff the list has strictly more than [n] elements,
     without traversing past element n+1. *)
  let rec list_length_gt n = function
    | _ :: rest -> if n = 0 then true else list_length_gt (n - 1) rest
    | [] -> false

  module Yojson = struct
    let bad_type expected_type x =
      Printf.ksprintf failwith "expected %s, got: %s"
        expected_type (Yojson.Safe.to_string x)

    let bad_sum type_name x =
      Printf.ksprintf failwith "invalid variant for type '%s': %s"
        type_name (Yojson.Safe.to_string x)

    let missing_field type_name field_name =
      Printf.ksprintf failwith "missing field '%s' in object of type '%s'"
        field_name type_name

    let bool_of_yojson = function
      | `Bool b -> b
      | x -> bad_type "bool" x

    let yojson_of_bool b = `Bool b

    let int_of_yojson = function
      | `Int n -> n
      | x -> bad_type "int" x

    let yojson_of_int n = `Int n

    let float_of_yojson = function
      | `Float f -> f
      | `Int n -> Float.of_int n
      | x -> bad_type "float" x

    let yojson_of_float f = `Float f

    let string_of_yojson = function
      | `String s -> s
      | x -> bad_type "string" x

    let yojson_of_string s = `String s

    let unit_of_yojson = function
      | `Null -> ()
      | x -> bad_type "null" x

    let yojson_of_unit () = `Null

    let list_of_yojson f = function
      | `List xs -> List.map f xs
      | x -> bad_type "array" x

    let yojson_of_list f xs = `List (List.map f xs)

    let option_of_yojson f = function
      | `String "None" -> None
      | `List [`String "Some"; x] -> Some (f x)
      | x -> bad_type "option" x

    let yojson_of_option f = function
      | None -> `String "None"
      | Some x -> `List [`String "Some"; f x]

    let nullable_of_yojson f = function
      | `Null -> None
      | x -> Some (f x)

    let yojson_of_nullable f = function
      | None -> `Null
      | Some x -> f x

    let assoc_of_yojson f = function
      | `Assoc pairs -> List.map (fun (k, v) -> (k, f v)) pairs
      | x -> bad_type "object" x

    let yojson_of_assoc f xs =
      `Assoc (List.map (fun (k, v) -> (k, f v)) xs)
  end

  module Jsonlike = struct
    let bad_type expected_type x =
      Printf.ksprintf failwith "%sexpected %s"
        (Atd_jsonlike.AST.loc_msg x) expected_type

    let bad_sum type_name x =
      Printf.ksprintf failwith "%sinvalid variant for type '%s'"
        (Atd_jsonlike.AST.loc_msg x) type_name

    let missing_field node type_name field_name =
      Printf.ksprintf failwith "%smissing field '%s' in object of type '%s'"
        (Atd_jsonlike.AST.loc_msg node) field_name type_name

    let bool_of_jsonlike = function
      | Atd_jsonlike.AST.Bool (_, b) -> b
      | x -> bad_type "bool" x

    let int_of_jsonlike = function
      | Atd_jsonlike.AST.Number (_, n) as node ->
          (match n.Atd_jsonlike.Number.int with
          | Some i -> i
          | None -> bad_type "integer" node)
      | x -> bad_type "int" x

    let float_of_jsonlike = function
      | Atd_jsonlike.AST.Number (_, n) as node ->
          (match n.Atd_jsonlike.Number.float with
          | Some f -> f
          | None -> bad_type "float" node)
      | x -> bad_type "float" x

    let string_of_jsonlike = function
      | Atd_jsonlike.AST.String (_, s) -> s
      | x -> bad_type "string" x

    let unit_of_jsonlike = function
      | Atd_jsonlike.AST.Null _ -> ()
      | x -> bad_type "null" x

    let list_of_jsonlike f = function
      | Atd_jsonlike.AST.Array (_, xs) -> List.map f xs
      | x -> bad_type "array" x

    let option_of_jsonlike f = function
      | Atd_jsonlike.AST.String (_, "None") -> None
      | Atd_jsonlike.AST.Array (_, [Atd_jsonlike.AST.String (_, "Some"); x]) -> Some (f x)
      | x -> bad_type "option" x

    let nullable_of_jsonlike f = function
      | Atd_jsonlike.AST.Null _ -> None
      | x -> Some (f x)

    let assoc_of_jsonlike f = function
      | Atd_jsonlike.AST.Object (_, pairs) ->
          List.map (fun (_, k, v) -> (k, f v)) pairs
      | x -> bad_type "object" x
  end
end

type tag_list = string list

let tag_list_of_yojson (x : Yojson.Safe.t) : tag_list =
  (Atdml_runtime.Yojson.list_of_yojson Atdml_runtime.Yojson.string_of_yojson) x

let tag_list_of_jsonlike (x : Atd_jsonlike.AST.t) : tag_list =
  (Atdml_runtime.Jsonlike.list_of_jsonlike Atdml_runtime.Jsonlike.string_of_jsonlike) x

let yojson_of_tag_list (x : tag_list) : Yojson.Safe.t =
  (Atdml_runtime.Yojson.yojson_of_list Atdml_runtime.Yojson.yojson_of_string) x

let tag_list_of_json s =
  tag_list_of_yojson (Yojson.Safe.from_string s)

let json_of_tag_list x =
  Yojson.Safe.to_string (yojson_of_tag_list x)

module Tag_list = struct
  type nonrec t = tag_list
  let of_yojson = tag_list_of_yojson
  let of_jsonlike = tag_list_of_jsonlike
  let to_yojson = yojson_of_tag_list
  let of_json = tag_list_of_json
  let to_json = json_of_tag_list
end

type score = float

let create_score (x : float) : score = x


let score_of_yojson (x : Yojson.Safe.t) : score =
  Atdml_runtime.Yojson.float_of_yojson x

let score_of_jsonlike (x : Atd_jsonlike.AST.t) : score =
  Atdml_runtime.Jsonlike.float_of_jsonlike x

let yojson_of_score (x : score) : Yojson.Safe.t =
  Atdml_runtime.Yojson.yojson_of_float x

let score_of_json s =
  score_of_yojson (Yojson.Safe.from_string s)

let json_of_score x =
  Yojson.Safe.to_string (yojson_of_score x)

module Score = struct
  type nonrec t = score
  let create = create_score
  let of_yojson = score_of_yojson
  let of_jsonlike = score_of_jsonlike
  let to_yojson = yojson_of_score
  let of_json = score_of_json
  let to_json = json_of_score
end

type opt_name = string option

let opt_name_of_yojson (x : Yojson.Safe.t) : opt_name =
  (Atdml_runtime.Yojson.option_of_yojson Atdml_runtime.Yojson.string_of_yojson) x

let opt_name_of_jsonlike (x : Atd_jsonlike.AST.t) : opt_name =
  (Atdml_runtime.Jsonlike.option_of_jsonlike Atdml_runtime.Jsonlike.string_of_jsonlike) x

let yojson_of_opt_name (x : opt_name) : Yojson.Safe.t =
  (Atdml_runtime.Yojson.yojson_of_option Atdml_runtime.Yojson.yojson_of_string) x

let opt_name_of_json s =
  opt_name_of_yojson (Yojson.Safe.from_string s)

let json_of_opt_name x =
  Yojson.Safe.to_string (yojson_of_opt_name x)

module Opt_name = struct
  type nonrec t = opt_name
  let of_yojson = opt_name_of_yojson
  let of_jsonlike = opt_name_of_jsonlike
  let to_yojson = yojson_of_opt_name
  let of_json = opt_name_of_json
  let to_json = json_of_opt_name
end

type id = string

let create_id (x : string) : id = x


let id_of_yojson (x : Yojson.Safe.t) : id =
  Atdml_runtime.Yojson.string_of_yojson x

let id_of_jsonlike (x : Atd_jsonlike.AST.t) : id =
  Atdml_runtime.Jsonlike.string_of_jsonlike x

let yojson_of_id (x : id) : Yojson.Safe.t =
  Atdml_runtime.Yojson.yojson_of_string x

let id_of_json s =
  id_of_yojson (Yojson.Safe.from_string s)

let json_of_id x =
  Yojson.Safe.to_string (yojson_of_id x)

module Id = struct
  type nonrec t = id
  let create = create_id
  let of_yojson = id_of_yojson
  let of_jsonlike = id_of_jsonlike
  let to_yojson = yojson_of_id
  let of_json = id_of_json
  let to_json = json_of_id
end

type all = {
  id: id;
  score: score;
  tags: tag_list;
  name: opt_name;
}

let create_all ~id ~score ~tags ~name () : all =
  { id; score; tags; name }

let all_of_yojson (x : Yojson.Safe.t) : all =
  match x with
  | `Assoc fields ->
    (* Duplicate JSON keys: behavior is unspecified (RFC 8259 §4 says keys SHOULD
       be unique). Below the threshold, List.assoc_opt returns the first binding;
       above it, the hashtable returns the last. *)
    let assoc_ =
      if Atdml_runtime.list_length_gt 5 fields then
        let tbl = Hashtbl.create 16 in
        List.iter (fun (k, v) -> Hashtbl.add tbl k v) fields;
        (fun key -> Hashtbl.find_opt tbl key)
      else (fun key -> List.assoc_opt key fields)
    in
    let id =
      match assoc_ "id" with
      | Some v -> id_of_yojson v
      | None -> Atdml_runtime.Yojson.missing_field "all" "id"
    in
    let score =
      match assoc_ "score" with
      | Some v -> score_of_yojson v
      | None -> Atdml_runtime.Yojson.missing_field "all" "score"
    in
    let tags =
      match assoc_ "tags" with
      | Some v -> tag_list_of_yojson v
      | None -> Atdml_runtime.Yojson.missing_field "all" "tags"
    in
    let name =
      match assoc_ "name" with
      | Some v -> opt_name_of_yojson v
      | None -> Atdml_runtime.Yojson.missing_field "all" "name"
    in
    { id; score; tags; name }
  | _ -> Atdml_runtime.Yojson.bad_type "all" x

let all_of_jsonlike (x : Atd_jsonlike.AST.t) : all =
  match x with
  | Atd_jsonlike.AST.Object (_, fields) ->
    let atdml_node_ = x in
    let assoc_ =
      if Atdml_runtime.list_length_gt 5 fields then
        let tbl = Hashtbl.create 16 in
        List.iter (fun (_, k, v) -> Hashtbl.add tbl k v) fields;
        (fun key -> Hashtbl.find_opt tbl key)
      else
        (fun key ->
          match List.find_opt (fun (_, k, _) -> k = key) fields with
          | None -> None | Some (_, _, v) -> Some v)
    in
    let id =
      match assoc_ "id" with
      | Some v -> id_of_jsonlike v
      | None -> Atdml_runtime.Jsonlike.missing_field atdml_node_ "all" "id"
    in
    let score =
      match assoc_ "score" with
      | Some v -> score_of_jsonlike v
      | None -> Atdml_runtime.Jsonlike.missing_field atdml_node_ "all" "score"
    in
    let tags =
      match assoc_ "tags" with
      | Some v -> tag_list_of_jsonlike v
      | None -> Atdml_runtime.Jsonlike.missing_field atdml_node_ "all" "tags"
    in
    let name =
      match assoc_ "name" with
      | Some v -> opt_name_of_jsonlike v
      | None -> Atdml_runtime.Jsonlike.missing_field atdml_node_ "all" "name"
    in
    { id; score; tags; name }
  | _ -> Atdml_runtime.Jsonlike.bad_type "all" x

let yojson_of_all (x : all) : Yojson.Safe.t =
  `Assoc (List.concat [
    [("id", yojson_of_id x.id)];
    [("score", yojson_of_score x.score)];
    [("tags", yojson_of_tag_list x.tags)];
    [("name", yojson_of_opt_name x.name)];
  ])

let all_of_json s =
  all_of_yojson (Yojson.Safe.from_string s)

let json_of_all x =
  Yojson.Safe.to_string (yojson_of_all x)

module All = struct
  type nonrec t = all
  let create = create_all
  let of_yojson = all_of_yojson
  let of_jsonlike = all_of_jsonlike
  let to_yojson = yojson_of_all
  let of_json = all_of_json
  let to_json = json_of_all
end

--- Input:
{ "id": "abc", "score": 1.23, "tags": [ "a", "b" ], "name": [ "Some", "x" ] }
--- Output:
{ "id": "abc", "score": 1.23, "tags": [ "a", "b" ], "name": [ "Some", "x" ] }
