From b7ec2f599e93d402130bc3edd48ee31724b2ee86 Mon Sep 17 00:00:00 2001 From: Ashlynne Mitchell Date: Wed, 11 Mar 2026 23:15:17 +1100 Subject: [PATCH] feat(deflexicon): wip --- CHANGELOG.md | 2 ++ lib/atex/lexicon.ex | 38 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 99112c9..4052c18 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,8 @@ and this project adheres to - Various improvements to `Atex.Did.Document` - Add `Atex.DID.Document.Service` and `Atex.DID.Document.VerificationMethod` sub-structs. - Add `to_json/1` methods and `JSON.Encoder` protocols for easy conversion to camelCase JSON. +- `deflexicon` now emits `content_type/0` functions (on `Input` submodules for typed JSON bodies, + otherwise on the root module) for procedures. ### Fixed diff --git a/lib/atex/lexicon.ex b/lib/atex/lexicon.ex index 6da755e..75e7e90 100644 --- a/lib/atex/lexicon.ex +++ b/lib/atex/lexicon.ex @@ -347,8 +347,26 @@ defmodule Atex.Lexicon do input = if def[:input] && def.input[:schema] do - [schema] = def_to_schema(nsid, "input", def.input.schema) - schema + encoding = def.input[:encoding] + + [{key, quoted_schema, quoted_type, quoted_struct}] = + def_to_schema(nsid, "input", def.input.schema) + + quoted_struct = + quote do + unquote(quoted_struct) + + @spec content_type() :: String.t() + def content_type, do: unquote(encoding) + end + + {key, quoted_schema, quoted_type, quoted_struct} + end + + # Add `content_type/0` to the root module if the lexicon defines a type without a schema. + raw_input_encoding = + if is_nil(input) && def[:input] do + def.input[:encoding] end # Root struct containing `input`, `raw_input`, and `params` @@ -388,6 +406,22 @@ defmodule Atex.Lexicon do defstruct input: nil end + params && raw_input_encoding -> + quote do + defstruct raw_input: nil, params: nil + + @spec content_type() :: String.t() + def content_type, do: unquote(raw_input_encoding) + end + + raw_input_encoding -> + quote do + defstruct raw_input: nil + + @spec content_type() :: String.t() + def content_type, do: unquote(raw_input_encoding) + end + params -> quote do defstruct raw_input: nil, params: nil -- 2.51.2