protogen: Treat bytes field as non-nullable #123
1 changed files with 19 additions and 15 deletions
|
@ -70,6 +70,24 @@ func emitJSONParseInteger(g *protogen.GeneratedFile, ident string, method string
|
||||||
g.P(ident, " := ", typ, "(v)")
|
g.P(ident, " := ", typ, "(v)")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func emitJSONReadEnum(g *protogen.GeneratedFile, name string, enumType string) {
|
||||||
|
g.P(`switch v := in.Interface().(type) {
|
||||||
|
case string:
|
||||||
|
if vv, ok := `+enumType+`_value[v]; ok {
|
||||||
|
`+name+` = `+enumType+`(vv)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
vv, err := `, strconvPackage.Ident("ParseInt"), `(v, 10, 32)
|
||||||
|
if err != nil {
|
||||||
|
in.AddError(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
`+name+` = `+enumType+`(vv)
|
||||||
|
case float64:
|
||||||
|
`+name+` = `+enumType+`(v)
|
||||||
|
}`)
|
||||||
|
}
|
||||||
|
|
||||||
func emitJSONFieldRead(g *protogen.GeneratedFile, f *protogen.Field, name string) {
|
func emitJSONFieldRead(g *protogen.GeneratedFile, f *protogen.Field, name string) {
|
||||||
g.P("{")
|
g.P("{")
|
||||||
defer g.P("}")
|
defer g.P("}")
|
||||||
|
@ -94,21 +112,7 @@ func emitJSONFieldRead(g *protogen.GeneratedFile, f *protogen.Field, name string
|
||||||
|
|
||||||
enumType := fieldType(g, f).String()
|
enumType := fieldType(g, f).String()
|
||||||
g.P("var parsedValue " + enumType)
|
g.P("var parsedValue " + enumType)
|
||||||
g.P(`switch v := in.Interface().(type) {
|
emitJSONReadEnum(g, "parsedValue", enumType)
|
||||||
case string:
|
|
||||||
if vv, ok := `+enumType+`_value[v]; ok {
|
|
||||||
parsedValue = `+enumType+`(vv)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
vv, err := `, strconvPackage.Ident("ParseInt"), `(v, 10, 32)
|
|
||||||
if err != nil {
|
|
||||||
in.AddError(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
parsedValue = `+enumType+`(vv)
|
|
||||||
case float64:
|
|
||||||
parsedValue = `+enumType+`(v)
|
|
||||||
}`)
|
|
||||||
template = "%s = parsedValue"
|
template = "%s = parsedValue"
|
||||||
case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind:
|
case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind:
|
||||||
emitJSONParseInteger(g, "pv", "ParseInt", 32, "int32")
|
emitJSONParseInteger(g, "pv", "ParseInt", 32, "int32")
|
||||||
|
|
Loading…
Reference in a new issue