protogen: Unmarshal stringified integers from JSON

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2024-10-01 13:50:27 +03:00
parent b9545e771c
commit 6d81becff8
13 changed files with 19 additions and 4 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -74,6 +74,17 @@ func emitJSONFieldRead(g *protogen.GeneratedFile, f *protogen.Field, name string
g.P("var f ", fieldType(g, f))
}
parseInteger := func(ident string, method string, bitSize int, typ string) {
g.P("r := in.JsonNumber()")
g.P("n := r.String()")
g.P("v, err := ", strconvPackage.Ident(method), "(n, 10, ", bitSize, ")")
g.P("if err != nil {")
g.P(" in.AddError(err)")
g.P(" return")
g.P("}")
g.P(ident, " := ", typ, "(v)")
}
var template string
switch f.Desc.Kind() {
case protoreflect.BoolKind:
@ -100,13 +111,17 @@ func emitJSONFieldRead(g *protogen.GeneratedFile, f *protogen.Field, name string
}`)
template = "%s = parsedValue"
case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind:
template = "%s = in.Int32()"
parseInteger("pv", "ParseInt", 32, "int32")
template = "%s = pv"
case protoreflect.Uint32Kind, protoreflect.Fixed32Kind:
template = "%s = in.Uint32()"
parseInteger("pv", "ParseUint", 32, "uint32")
template = "%s = pv"
case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind:
template = "%s = in.Int64()"
parseInteger("pv", "ParseInt", 64, "int64")
template = "%s = pv"
case protoreflect.Uint64Kind, protoreflect.Fixed64Kind:
template = "%s = in.Uint64()"
parseInteger("pv", "ParseUint", 64, "uint64")
template = "%s = pv"
case protoreflect.FloatKind:
template = "%s = in.Float32()"
case protoreflect.DoubleKind: