Skip to content

wit/bindgen,cm: marshal & unmarshal for record types#225

Closed
lxfontes wants to merge 1 commit intobytecodealliance:mainfrom
lxfontes:lxfontes/cm-json
Closed

wit/bindgen,cm: marshal & unmarshal for record types#225
lxfontes wants to merge 1 commit intobytecodealliance:mainfrom
lxfontes:lxfontes/cm-json

Conversation

@lxfontes
Copy link
Member

Adds json struct tags to Records, based on wit names.

Also adds MarshalJSON & UnmarshalJSON to:

  • cm.List
  • cm.Tuple ( all of them )
  • cm.Option
  • cm.Variant

Example, encoding a complex record type:

  record response {
      headers: list<tuple<string, list<string>>>,
      http-code: u16,
      body: response-body
  }
  variant response-body {
    ok(list<list-element>),
    err(function-error),
    platform-err(platform-error)
  }
  record list-element {
      optional-int: option<u64>,
      optional-bool: option<bool>,
  }
  record function-error {
      error: string
  }
  record platform-error {
    code: string,
    message: string
  }

and filling it up:

	hdrVals := cm.ToList([]string{"value1", "value2"})
	hdrTuple := cm.Tuple[string, cm.List[string]]{
		F0: "header-name",
		F1: hdrVals,
	}
	headers := cm.ToList([]cm.Tuple[string, cm.List[string]]{hdrTuple})
	v := somefunctioninterface.Response{
		Headers:  headers,
		HTTPCode: 200,
		Body:     somefunctioninterface.ResponseBodyErr(somefunctioninterface.FunctionError{Error: "failed"}),
	}

Without this Pull Request:

{"Headers":{},"Body":{"RequiredParam":"required","OptionalParam":{}}}

With this Pull Request:

{"headers":[["header-name",["value1","value2"]]],"http-code":200,"body":{"err":{"error":"failed"}}}

Tuple handling

Tuples are encoded as json arrays with explicit nulls.

Tuple[string,int] -> [ "some string", 42 ]
Tuple3[string, *string, int] -> [ "some string", null, 42 ]

Variant handling

Variants are encoded as json dictionaries, so they can carry the variant data.

 record somerecord {
   myvariant: somevar,
 }
  variant somevar {
    ok,
    err(string),
  }

// JSON
{ "myvariant": { "ok": true }}
{ "myvariant": { "error": "error value" }}

Option handling

Options rely on explicit null for the "None" case.

{ "myoptional": null } // cm.None
{ "myoptional": "this" } // cm.Option[string]

If this direction seems reasonable I will add:

  • Marshal/Unmarshal to cm.Result
  • Encoding/Decoding Tests

Signed-off-by: Lucas Fontes <lucas@cosmonic.com>
@ydnar
Copy link
Collaborator

ydnar commented Nov 1, 2024

Amazing. This looks great so far. Thanks!

var b strings.Builder
b.WriteString("struct {\n")
stringio.Write(&b, "_ ", file.Import(g.opts.cmPackage), ".HostLayout")
stringio.Write(&b, "_ ", file.Import(g.opts.cmPackage), ".HostLayout", "`json:\"-\"`")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget the space before the backtick, or just have a single string with HostLayout and the tag.

stringio.Write(&b, "return ", stringsName, "[v.Tag()]\n")
b.WriteString("}\n\n")

file.Import("encoding/json")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure to capture the name of the import. It might not be json.

file.Import("encoding/json")
b.WriteString(formatDocComments("MarshalJSON implements [json.Marshaler].", true))
stringio.Write(&b, "func (v ", goName, ") MarshalJSON() ([]byte, error) {\n")
stringio.Write(&b, "ret := make(map[string]any)\n")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there might be a better way to do this.

Possibly by putting MarshalJSON and UnmarshalJSON in package cm, and generate only the minimal mapping from case name to case type.

list[T]
}

func (l List[T]) MarshalJSON() ([]byte, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These methods should be on the inner list[T] struct.

}

// MarshalJSON implements the json.Marshaler interface for [Option].
func (o Option[T]) MarshalJSON() ([]byte, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These methods should be on the inner struct.


// UnmarshalJSON unmarshals the Option from JSON.
func (o *Option[T]) UnmarshalJSON(buf []byte) error {
if len(buf) == 0 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also check if buf == null.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 the null story here is interesting. will push it up as part of the breaking this PR into many

@lxfontes
Copy link
Member Author

lxfontes commented Nov 7, 2024

going to close this PR and move over to #239

@lxfontes lxfontes closed this Nov 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants