mirror of
https://github.com/netbirdio/easyjson.git
synced 2026-05-22 18:44:42 -07:00
added tag for generate slice byte instead of Base64Bytes
This commit is contained in:
@@ -33,6 +33,8 @@ type Generator struct {
|
||||
StubsOnly bool
|
||||
LeaveTemps bool
|
||||
NoFormat bool
|
||||
|
||||
SimpleBytes bool
|
||||
}
|
||||
|
||||
// writeStub outputs an initial stubs for marshalers/unmarshalers so that the package
|
||||
@@ -120,6 +122,9 @@ func (g *Generator) writeMain() (path string, err error) {
|
||||
if g.NoStdMarshalers {
|
||||
fmt.Fprintln(f, " g.NoStdMarshalers()")
|
||||
}
|
||||
if g.SimpleBytes {
|
||||
fmt.Fprintln(f, " g.SimpleBytes()")
|
||||
}
|
||||
|
||||
sort.Strings(g.Types)
|
||||
for _, v := range g.Types {
|
||||
|
||||
@@ -22,6 +22,7 @@ var lowerCamelCase = flag.Bool("lower_camel_case", false, "use lowerCamelCase na
|
||||
var noStdMarshalers = flag.Bool("no_std_marshalers", false, "don't generate MarshalJSON/UnmarshalJSON funcs")
|
||||
var omitEmpty = flag.Bool("omit_empty", false, "omit empty fields by default")
|
||||
var allStructs = flag.Bool("all", false, "generate marshaler/unmarshalers for all structs in a file")
|
||||
var simpleBytes = flag.Bool("byte", false, "use simple byte inseat base64 for slice of bytes")
|
||||
var leaveTemps = flag.Bool("leave_temps", false, "do not delete temporary files")
|
||||
var stubs = flag.Bool("stubs", false, "only generate stubs for marshaler/unmarshaler funcs")
|
||||
var noformat = flag.Bool("noformat", false, "do not run 'gofmt -w' on output file")
|
||||
@@ -72,6 +73,7 @@ func generate(fname string) (err error) {
|
||||
OutName: outName,
|
||||
StubsOnly: *stubs,
|
||||
NoFormat: *noformat,
|
||||
SimpleBytes: *simpleBytes,
|
||||
}
|
||||
|
||||
if err := g.Run(); err != nil {
|
||||
|
||||
+8
-3
@@ -51,7 +51,7 @@ var primitiveStringDecoders = map[reflect.Kind]string{
|
||||
}
|
||||
|
||||
var customDecoders = map[string]string{
|
||||
"json.Number": "in.JsonNumber()",
|
||||
"json.Number": "in.JsonNumber()",
|
||||
}
|
||||
|
||||
// genTypeDecoder generates decoding code for the type t, but uses unmarshaler interface if implemented by t.
|
||||
@@ -88,7 +88,7 @@ func (g *Generator) genTypeDecoder(t reflect.Type, out string, tags fieldTags, i
|
||||
func (g *Generator) genTypeDecoderNoCheck(t reflect.Type, out string, tags fieldTags, indent int) error {
|
||||
ws := strings.Repeat(" ", indent)
|
||||
// Check whether type is primitive, needs to be done after interface check.
|
||||
if dec := customDecoders[t.String()]; dec != "" {
|
||||
if dec := customDecoders[t.String()]; dec != "" {
|
||||
fmt.Fprintln(g.out, ws+out+" = "+dec)
|
||||
return nil
|
||||
} else if dec := primitiveStringDecoders[t.Kind()]; dec != "" && tags.asString {
|
||||
@@ -109,7 +109,12 @@ func (g *Generator) genTypeDecoderNoCheck(t reflect.Type, out string, tags field
|
||||
fmt.Fprintln(g.out, ws+" in.Skip()")
|
||||
fmt.Fprintln(g.out, ws+" "+out+" = nil")
|
||||
fmt.Fprintln(g.out, ws+"} else {")
|
||||
fmt.Fprintln(g.out, ws+" "+out+" = in.Bytes()")
|
||||
if g.simpleBytes {
|
||||
fmt.Fprintln(g.out, ws+" "+out+" = []byte(in.String())")
|
||||
} else {
|
||||
fmt.Fprintln(g.out, ws+" "+out+" = in.Bytes()")
|
||||
}
|
||||
|
||||
fmt.Fprintln(g.out, ws+"}")
|
||||
|
||||
} else {
|
||||
|
||||
+11
-2
@@ -128,7 +128,12 @@ func (g *Generator) genTypeEncoderNoCheck(t reflect.Type, in string, tags fieldT
|
||||
vVar := g.uniqueVarName()
|
||||
|
||||
if t.Elem().Kind() == reflect.Uint8 {
|
||||
fmt.Fprintln(g.out, ws+"out.Base64Bytes("+in+")")
|
||||
if g.simpleBytes {
|
||||
fmt.Fprintln(g.out, ws+"out.String(string("+in+"))")
|
||||
} else {
|
||||
fmt.Fprintln(g.out, ws+"out.Base64Bytes("+in+")")
|
||||
}
|
||||
|
||||
} else {
|
||||
if !assumeNonEmpty {
|
||||
fmt.Fprintln(g.out, ws+"if "+in+" == nil && (out.Flags & jwriter.NilSliceAsEmpty) == 0 {")
|
||||
@@ -157,7 +162,11 @@ func (g *Generator) genTypeEncoderNoCheck(t reflect.Type, in string, tags fieldT
|
||||
iVar := g.uniqueVarName()
|
||||
|
||||
if t.Elem().Kind() == reflect.Uint8 {
|
||||
fmt.Fprintln(g.out, ws+"out.Base64Bytes("+in+"[:])")
|
||||
if g.simpleBytes {
|
||||
fmt.Fprintln(g.out, ws+"out.String(string("+in+"[:]))")
|
||||
} else {
|
||||
fmt.Fprintln(g.out, ws+"out.Base64Bytes("+in+"[:])")
|
||||
}
|
||||
} else {
|
||||
fmt.Fprintln(g.out, ws+"out.RawByte('[')")
|
||||
fmt.Fprintln(g.out, ws+"for "+iVar+" := range "+in+" {")
|
||||
|
||||
@@ -35,6 +35,7 @@ type Generator struct {
|
||||
|
||||
noStdMarshalers bool
|
||||
omitEmpty bool
|
||||
simpleBytes bool
|
||||
fieldNamer FieldNamer
|
||||
|
||||
// package path to local alias map for tracking imports
|
||||
@@ -115,6 +116,11 @@ func (g *Generator) OmitEmpty() {
|
||||
g.omitEmpty = true
|
||||
}
|
||||
|
||||
// SimpleBytes triggers generate output bytes as slice byte
|
||||
func (g *Generator) SimpleBytes() {
|
||||
g.simpleBytes = true
|
||||
}
|
||||
|
||||
// addTypes requests to generate encoding/decoding funcs for the given type.
|
||||
func (g *Generator) addType(t reflect.Type) {
|
||||
if g.typesSeen[t] {
|
||||
|
||||
Reference in New Issue
Block a user