[#120] 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 eeb754c327
commit d94b9c6d0d
13 changed files with 536 additions and 61 deletions

View file

@ -11,6 +11,7 @@ import (
easyproto "github.com/VictoriaMetrics/easyproto"
jlexer "github.com/mailru/easyjson/jlexer"
jwriter "github.com/mailru/easyjson/jwriter"
strconv "strconv"
)
type Decimal struct {
@ -170,13 +171,29 @@ func (x *Decimal) UnmarshalEasyJSON(in *jlexer.Lexer) {
case "value":
{
var f int64
f = in.Int64()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseInt(n, 10, 64)
if err != nil {
in.AddError(err)
return
}
pv := int64(v)
f = pv
x.Value = f
}
case "precision":
{
var f uint32
f = in.Uint32()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 32)
if err != nil {
in.AddError(err)
return
}
pv := uint32(v)
f = pv
x.Precision = f
}
}

View file

@ -1481,19 +1481,43 @@ func (x *BearerToken_Body_TokenLifetime) UnmarshalEasyJSON(in *jlexer.Lexer) {
case "exp":
{
var f uint64
f = in.Uint64()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 64)
if err != nil {
in.AddError(err)
return
}
pv := uint64(v)
f = pv
x.Exp = f
}
case "nbf":
{
var f uint64
f = in.Uint64()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 64)
if err != nil {
in.AddError(err)
return
}
pv := uint64(v)
f = pv
x.Nbf = f
}
case "iat":
{
var f uint64
f = in.Uint64()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 64)
if err != nil {
in.AddError(err)
return
}
pv := uint64(v)
f = pv
x.Iat = f
}
}

View file

@ -13,6 +13,7 @@ import (
easyproto "github.com/VictoriaMetrics/easyproto"
jlexer "github.com/mailru/easyjson/jlexer"
jwriter "github.com/mailru/easyjson/jwriter"
strconv "strconv"
)
type Container_Attribute struct {
@ -517,7 +518,15 @@ func (x *Container) UnmarshalEasyJSON(in *jlexer.Lexer) {
case "basicACL":
{
var f uint32
f = in.Uint32()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 32)
if err != nil {
in.AddError(err)
return
}
pv := uint32(v)
f = pv
x.BasicAcl = f
}
case "attributes":

View file

@ -703,7 +703,15 @@ func (x *Selector) UnmarshalEasyJSON(in *jlexer.Lexer) {
case "count":
{
var f uint32
f = in.Uint32()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 32)
if err != nil {
in.AddError(err)
return
}
pv := uint32(v)
f = pv
x.Count = f
}
case "clause":
@ -970,7 +978,15 @@ func (x *Replica) UnmarshalEasyJSON(in *jlexer.Lexer) {
case "count":
{
var f uint32
f = in.Uint32()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 32)
if err != nil {
in.AddError(err)
return
}
pv := uint32(v)
f = pv
x.Count = f
}
case "selector":
@ -982,13 +998,29 @@ func (x *Replica) UnmarshalEasyJSON(in *jlexer.Lexer) {
case "ecDataCount":
{
var f uint32
f = in.Uint32()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 32)
if err != nil {
in.AddError(err)
return
}
pv := uint32(v)
f = pv
x.EcDataCount = f
}
case "ecParityCount":
{
var f uint32
f = in.Uint32()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 32)
if err != nil {
in.AddError(err)
return
}
pv := uint32(v)
f = pv
x.EcParityCount = f
}
}
@ -1306,7 +1338,15 @@ func (x *PlacementPolicy) UnmarshalEasyJSON(in *jlexer.Lexer) {
case "containerBackupFactor":
{
var f uint32
f = in.Uint32()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 32)
if err != nil {
in.AddError(err)
return
}
pv := uint32(v)
f = pv
x.ContainerBackupFactor = f
}
case "selectors":
@ -2094,7 +2134,15 @@ func (x *Netmap) UnmarshalEasyJSON(in *jlexer.Lexer) {
case "epoch":
{
var f uint64
f = in.Uint64()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 64)
if err != nil {
in.AddError(err)
return
}
pv := uint64(v)
f = pv
x.Epoch = f
}
case "nodes":
@ -2677,19 +2725,43 @@ func (x *NetworkInfo) UnmarshalEasyJSON(in *jlexer.Lexer) {
case "currentEpoch":
{
var f uint64
f = in.Uint64()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 64)
if err != nil {
in.AddError(err)
return
}
pv := uint64(v)
f = pv
x.CurrentEpoch = f
}
case "magicNumber":
{
var f uint64
f = in.Uint64()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 64)
if err != nil {
in.AddError(err)
return
}
pv := uint64(v)
f = pv
x.MagicNumber = f
}
case "msPerBlock":
{
var f int64
f = in.Int64()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseInt(n, 10, 64)
if err != nil {
in.AddError(err)
return
}
pv := int64(v)
f = pv
x.MsPerBlock = f
}
case "networkConfig":

View file

@ -1508,7 +1508,15 @@ func (x *PutRequest_Body_Init) UnmarshalEasyJSON(in *jlexer.Lexer) {
var list []uint32
in.Delim('[')
for !in.IsDelim(']') {
f = in.Uint32()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 32)
if err != nil {
in.AddError(err)
return
}
pv := uint32(v)
f = pv
list = append(list, f)
in.WantComma()
}
@ -4815,7 +4823,15 @@ func (x *SearchRequest_Body) UnmarshalEasyJSON(in *jlexer.Lexer) {
case "version":
{
var f uint32
f = in.Uint32()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 32)
if err != nil {
in.AddError(err)
return
}
pv := uint32(v)
f = pv
x.Version = f
}
case "filters":
@ -5642,13 +5658,29 @@ func (x *Range) UnmarshalEasyJSON(in *jlexer.Lexer) {
case "offset":
{
var f uint64
f = in.Uint64()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 64)
if err != nil {
in.AddError(err)
return
}
pv := uint64(v)
f = pv
x.Offset = f
}
case "length":
{
var f uint64
f = in.Uint64()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 64)
if err != nil {
in.AddError(err)
return
}
pv := uint64(v)
f = pv
x.Length = f
}
}
@ -7823,7 +7855,15 @@ func (x *PutSingleRequest_Body) UnmarshalEasyJSON(in *jlexer.Lexer) {
var list []uint32
in.Delim('[')
for !in.IsDelim(']') {
f = in.Uint32()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 32)
if err != nil {
in.AddError(err)
return
}
pv := uint32(v)
f = pv
list = append(list, f)
in.WantComma()
}

View file

@ -433,7 +433,15 @@ func (x *ShortHeader) UnmarshalEasyJSON(in *jlexer.Lexer) {
case "creationEpoch":
{
var f uint64
f = in.Uint64()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 64)
if err != nil {
in.AddError(err)
return
}
pv := uint64(v)
f = pv
x.CreationEpoch = f
}
case "ownerID":
@ -468,7 +476,15 @@ func (x *ShortHeader) UnmarshalEasyJSON(in *jlexer.Lexer) {
case "payloadLength":
{
var f uint64
f = in.Uint64()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 64)
if err != nil {
in.AddError(err)
return
}
pv := uint64(v)
f = pv
x.PayloadLength = f
}
case "payloadHash":
@ -1408,19 +1424,43 @@ func (x *Header_EC) UnmarshalEasyJSON(in *jlexer.Lexer) {
case "index":
{
var f uint32
f = in.Uint32()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 32)
if err != nil {
in.AddError(err)
return
}
pv := uint32(v)
f = pv
x.Index = f
}
case "total":
{
var f uint32
f = in.Uint32()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 32)
if err != nil {
in.AddError(err)
return
}
pv := uint32(v)
f = pv
x.Total = f
}
case "headerLength":
{
var f uint32
f = in.Uint32()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 32)
if err != nil {
in.AddError(err)
return
}
pv := uint32(v)
f = pv
x.HeaderLength = f
}
case "header":
@ -2005,13 +2045,29 @@ func (x *Header) UnmarshalEasyJSON(in *jlexer.Lexer) {
case "creationEpoch":
{
var f uint64
f = in.Uint64()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 64)
if err != nil {
in.AddError(err)
return
}
pv := uint64(v)
f = pv
x.CreationEpoch = f
}
case "payloadLength":
{
var f uint64
f = in.Uint64()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 64)
if err != nil {
in.AddError(err)
return
}
pv := uint64(v)
f = pv
x.PayloadLength = f
}
case "payloadHash":
@ -2777,13 +2833,29 @@ func (x *ECInfo_Chunk) UnmarshalEasyJSON(in *jlexer.Lexer) {
case "index":
{
var f uint32
f = in.Uint32()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 32)
if err != nil {
in.AddError(err)
return
}
pv := uint32(v)
f = pv
x.Index = f
}
case "total":
{
var f uint32
f = in.Uint32()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 32)
if err != nil {
in.AddError(err)
return
}
pv := uint32(v)
f = pv
x.Total = f
}
}

View file

@ -835,13 +835,29 @@ func (x *Version) UnmarshalEasyJSON(in *jlexer.Lexer) {
case "major":
{
var f uint32
f = in.Uint32()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 32)
if err != nil {
in.AddError(err)
return
}
pv := uint32(v)
f = pv
x.Major = f
}
case "minor":
{
var f uint32
f = in.Uint32()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 32)
if err != nil {
in.AddError(err)
return
}
pv := uint32(v)
f = pv
x.Minor = f
}
}

View file

@ -12,6 +12,7 @@ import (
easyproto "github.com/VictoriaMetrics/easyproto"
jlexer "github.com/mailru/easyjson/jlexer"
jwriter "github.com/mailru/easyjson/jwriter"
strconv "strconv"
)
type CreateRequest_Body struct {
@ -181,7 +182,15 @@ func (x *CreateRequest_Body) UnmarshalEasyJSON(in *jlexer.Lexer) {
case "expiration":
{
var f uint64
f = in.Uint64()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 64)
if err != nil {
in.AddError(err)
return
}
pv := uint64(v)
f = pv
x.Expiration = f
}
}

View file

@ -935,19 +935,43 @@ func (x *SessionToken_Body_TokenLifetime) UnmarshalEasyJSON(in *jlexer.Lexer) {
case "exp":
{
var f uint64
f = in.Uint64()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 64)
if err != nil {
in.AddError(err)
return
}
pv := uint64(v)
f = pv
x.Exp = f
}
case "nbf":
{
var f uint64
f = in.Uint64()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 64)
if err != nil {
in.AddError(err)
return
}
pv := uint64(v)
f = pv
x.Nbf = f
}
case "iat":
{
var f uint64
f = in.Uint64()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 64)
if err != nil {
in.AddError(err)
return
}
pv := uint64(v)
f = pv
x.Iat = f
}
}
@ -2083,13 +2107,29 @@ func (x *RequestMetaHeader) UnmarshalEasyJSON(in *jlexer.Lexer) {
case "epoch":
{
var f uint64
f = in.Uint64()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 64)
if err != nil {
in.AddError(err)
return
}
pv := uint64(v)
f = pv
x.Epoch = f
}
case "ttl":
{
var f uint32
f = in.Uint32()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 32)
if err != nil {
in.AddError(err)
return
}
pv := uint32(v)
f = pv
x.Ttl = f
}
case "xHeaders":
@ -2130,7 +2170,15 @@ func (x *RequestMetaHeader) UnmarshalEasyJSON(in *jlexer.Lexer) {
case "magicNumber":
{
var f uint64
f = in.Uint64()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 64)
if err != nil {
in.AddError(err)
return
}
pv := uint64(v)
f = pv
x.MagicNumber = f
}
}
@ -2456,13 +2504,29 @@ func (x *ResponseMetaHeader) UnmarshalEasyJSON(in *jlexer.Lexer) {
case "epoch":
{
var f uint64
f = in.Uint64()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 64)
if err != nil {
in.AddError(err)
return
}
pv := uint64(v)
f = pv
x.Epoch = f
}
case "ttl":
{
var f uint32
f = in.Uint32()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 32)
if err != nil {
in.AddError(err)
return
}
pv := uint32(v)
f = pv
x.Ttl = f
}
case "xHeaders":

View file

@ -425,7 +425,15 @@ func (x *Status_Detail) UnmarshalEasyJSON(in *jlexer.Lexer) {
case "id":
{
var f uint32
f = in.Uint32()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 32)
if err != nil {
in.AddError(err)
return
}
pv := uint32(v)
f = pv
x.Id = f
}
case "value":
@ -645,7 +653,15 @@ func (x *Status) UnmarshalEasyJSON(in *jlexer.Lexer) {
case "code":
{
var f uint32
f = in.Uint32()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 32)
if err != nil {
in.AddError(err)
return
}
pv := uint32(v)
f = pv
x.Code = f
}
case "message":

View file

@ -12,6 +12,7 @@ import (
easyproto "github.com/VictoriaMetrics/easyproto"
jlexer "github.com/mailru/easyjson/jlexer"
jwriter "github.com/mailru/easyjson/jwriter"
strconv "strconv"
)
type Tombstone struct {
@ -216,7 +217,15 @@ func (x *Tombstone) UnmarshalEasyJSON(in *jlexer.Lexer) {
case "expirationEpoch":
{
var f uint64
f = in.Uint64()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 64)
if err != nil {
in.AddError(err)
return
}
pv := uint64(v)
f = pv
x.ExpirationEpoch = f
}
case "splitID":

View file

@ -175,7 +175,15 @@ func (x *Primitives_Aux) UnmarshalEasyJSON(in *jlexer.Lexer) {
case "innerField":
{
var f uint32
f = in.Uint32()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 32)
if err != nil {
in.AddError(err)
return
}
pv := uint32(v)
f = pv
x.InnerField = f
}
}
@ -797,31 +805,71 @@ func (x *Primitives) UnmarshalEasyJSON(in *jlexer.Lexer) {
case "fieldD":
{
var f int32
f = in.Int32()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseInt(n, 10, 32)
if err != nil {
in.AddError(err)
return
}
pv := int32(v)
f = pv
x.FieldD = f
}
case "fieldE":
{
var f uint32
f = in.Uint32()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 32)
if err != nil {
in.AddError(err)
return
}
pv := uint32(v)
f = pv
x.FieldE = f
}
case "fieldF":
{
var f int64
f = in.Int64()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseInt(n, 10, 64)
if err != nil {
in.AddError(err)
return
}
pv := int64(v)
f = pv
x.FieldF = f
}
case "fieldG":
{
var f uint64
f = in.Uint64()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 64)
if err != nil {
in.AddError(err)
return
}
pv := uint64(v)
f = pv
x.FieldG = f
}
case "fieldI":
{
var f uint64
f = in.Uint64()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 64)
if err != nil {
in.AddError(err)
return
}
pv := uint64(v)
f = pv
x.FieldI = f
}
case "fieldJ":
@ -833,7 +881,15 @@ func (x *Primitives) UnmarshalEasyJSON(in *jlexer.Lexer) {
case "fieldK":
{
var f uint32
f = in.Uint32()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 32)
if err != nil {
in.AddError(err)
return
}
pv := uint32(v)
f = pv
x.FieldK = f
}
case "fieldH":
@ -871,7 +927,15 @@ func (x *Primitives) UnmarshalEasyJSON(in *jlexer.Lexer) {
x.FieldM = xx
{
var f uint32
f = in.Uint32()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 32)
if err != nil {
in.AddError(err)
return
}
pv := uint32(v)
f = pv
xx.FieldMe = f
}
case "fieldAux":
@ -1039,7 +1103,15 @@ func (x *RepPrimitives_Aux) UnmarshalEasyJSON(in *jlexer.Lexer) {
case "innerField":
{
var f uint32
f = in.Uint32()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 32)
if err != nil {
in.AddError(err)
return
}
pv := uint32(v)
f = pv
x.InnerField = f
}
}
@ -1498,7 +1570,15 @@ func (x *RepPrimitives) UnmarshalEasyJSON(in *jlexer.Lexer) {
var list []int32
in.Delim('[')
for !in.IsDelim(']') {
f = in.Int32()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseInt(n, 10, 32)
if err != nil {
in.AddError(err)
return
}
pv := int32(v)
f = pv
list = append(list, f)
in.WantComma()
}
@ -1511,7 +1591,15 @@ func (x *RepPrimitives) UnmarshalEasyJSON(in *jlexer.Lexer) {
var list []uint32
in.Delim('[')
for !in.IsDelim(']') {
f = in.Uint32()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 32)
if err != nil {
in.AddError(err)
return
}
pv := uint32(v)
f = pv
list = append(list, f)
in.WantComma()
}
@ -1524,7 +1612,15 @@ func (x *RepPrimitives) UnmarshalEasyJSON(in *jlexer.Lexer) {
var list []int64
in.Delim('[')
for !in.IsDelim(']') {
f = in.Int64()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseInt(n, 10, 64)
if err != nil {
in.AddError(err)
return
}
pv := int64(v)
f = pv
list = append(list, f)
in.WantComma()
}
@ -1537,7 +1633,15 @@ func (x *RepPrimitives) UnmarshalEasyJSON(in *jlexer.Lexer) {
var list []uint64
in.Delim('[')
for !in.IsDelim(']') {
f = in.Uint64()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 64)
if err != nil {
in.AddError(err)
return
}
pv := uint64(v)
f = pv
list = append(list, f)
in.WantComma()
}
@ -1550,7 +1654,15 @@ func (x *RepPrimitives) UnmarshalEasyJSON(in *jlexer.Lexer) {
var list []uint64
in.Delim('[')
for !in.IsDelim(']') {
f = in.Uint64()
r := in.JsonNumber()
n := r.String()
v, err := strconv.ParseUint(n, 10, 64)
if err != nil {
in.AddError(err)
return
}
pv := uint64(v)
f = pv
list = append(list, f)
in.WantComma()
}

View file

@ -59,6 +59,17 @@ func emitJSONUnmarshal(g *protogen.GeneratedFile, msg *protogen.Message) {
g.P("}")
}
func emitJSONParseInteger(g *protogen.GeneratedFile, 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)")
}
func emitJSONFieldRead(g *protogen.GeneratedFile, f *protogen.Field, name string) {
g.P("{")
defer g.P("}")
@ -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()"
emitJSONParseInteger(g, "pv", "ParseInt", 32, "int32")
template = "%s = pv"
case protoreflect.Uint32Kind, protoreflect.Fixed32Kind:
template = "%s = in.Uint32()"
emitJSONParseInteger(g, "pv", "ParseUint", 32, "uint32")
template = "%s = pv"
case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind:
template = "%s = in.Int64()"
emitJSONParseInteger(g, "pv", "ParseInt", 64, "int64")
template = "%s = pv"
case protoreflect.Uint64Kind, protoreflect.Fixed64Kind:
template = "%s = in.Uint64()"
emitJSONParseInteger(g, "pv", "ParseUint", 64, "uint64")
template = "%s = pv"
case protoreflect.FloatKind:
template = "%s = in.Float32()"
case protoreflect.DoubleKind: