wit/bindgen: support JSON encoding of WIT types
@lxfontes ci sta già lavorando.
Dal 7/11/2024.
Valutazione
Questa issue non è ancora stata valutata.
Descrizione
This started in https://github.com/bytecodealliance/go-modules/pull/225
Goal: JSON Serialize / Deserialize WIT Records based on WIT field names.
Progress
-
record- #265 -
list- #266 -
enum -
tuple -
variant -
result -
option -
resourcehandle types (decide behavior) -
error-context -
stream -
future
Approach
Centralize JSON serialization in the cm package, keeping codegen changes to a minimum.
Given 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"}),
}
We should serialize it to JSON as:
{"headers":[["header-name",["value1","value2"]]],"http-code":200,"body":{"err":{"error":"failed"}}}
For comparison, this is what is produced today:
{"Headers":{},"Body":{"RequiredParam":"required","OptionalParam":{}}}
Type encoding
Whenever possible, reuse standard mappings. string -> string, u32 -> uint32, etc.
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]
Questions
Zero Value Variants
Today they end up with tag = 0, this impacts de-serialization of variants. We need the ability to distinct between:
var v Variantv := NewVariantWithZeroTagValue()v := Some(NewVariantWithZeroTagValue)
atm only the Some() path de-serializes correctly ( pointers ).
- Lingua principale
- Go
- Stelle
- 148
- Fork
- 20
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Nessun modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di bytecodealliance/go-modules
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 48/100
bytecodealliance/go-modules#428 · 1 reazione ·
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 45/100
bytecodealliance/go-modules#411 ·
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 45/100
bytecodealliance/go-modules#410 ·
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 48/100
bytecodealliance/go-modules#394 ·
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 38/100
bytecodealliance/go-modules#377 · 1 commento ·
Tutte le issue di bytecodealliance/go-modules
Issue simili
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 88/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 67/100
vanderheijden86/b9s#20 ·
-
go-battery needs an ndsctl on PATH: TestPurchaseSessionGuardHoldsThroughTheOutcomeUnknownWindow fails on bare hosts (passes with stub)Forse già presa Una pull request collegata a questa issue è aperta o già unita. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
OpenTollGate/tollgate-module-basic-go#726 ·
I maintainer di solito rispondono entro 1 giorno
-
ux waiting for feedback
Difficoltà 2/5 1-3 ore Idoneità per principianti 63/100
evcc-io/evcc#34527 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
phase:v3 type:harness
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
I maintainer di solito rispondono entro 1 giorno