mirror of
https://github.com/netbirdio/easyjson.git
synced 2026-05-22 18:44:42 -07:00
Merge pull request #155 from sah4ez/master
added tag for generate slice byte instead of Base64Bytes
This commit is contained in:
@@ -35,6 +35,8 @@ Usage of easyjson:
|
||||
generate marshaler/unmarshalers for all structs in a file
|
||||
-build_tags string
|
||||
build tags to add to generated file
|
||||
-byte
|
||||
use simple bytes instead of Base64Bytes for slice of bytes
|
||||
-leave_temps
|
||||
do not delete temporary files
|
||||
-no_std_marshalers
|
||||
|
||||
@@ -35,6 +35,7 @@ type Generator struct {
|
||||
StubsOnly bool
|
||||
LeaveTemps bool
|
||||
NoFormat bool
|
||||
SimpleBytes bool
|
||||
}
|
||||
|
||||
// writeStub outputs an initial stub for marshalers/unmarshalers so that the package
|
||||
@@ -125,6 +126,9 @@ func (g *Generator) writeMain() (path string, err error) {
|
||||
if g.DisallowUnknownFields {
|
||||
fmt.Fprintln(f, " g.DisallowUnknownFields()")
|
||||
}
|
||||
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 bytes instead of Base64Bytes 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")
|
||||
@@ -74,6 +75,7 @@ func generate(fname string) (err error) {
|
||||
OutName: outName,
|
||||
StubsOnly: *stubs,
|
||||
NoFormat: *noformat,
|
||||
SimpleBytes: *simpleBytes,
|
||||
}
|
||||
|
||||
if err := g.Run(); err != nil {
|
||||
|
||||
+6
-1
@@ -129,7 +129,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 {
|
||||
|
||||
+10
-2
@@ -140,7 +140,11 @@ func (g *Generator) genTypeEncoderNoCheck(t reflect.Type, in string, tags fieldT
|
||||
vVar := g.uniqueVarName()
|
||||
|
||||
if t.Elem().Kind() == reflect.Uint8 && elem.Name() == "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 {")
|
||||
@@ -169,7 +173,11 @@ func (g *Generator) genTypeEncoderNoCheck(t reflect.Type, in string, tags fieldT
|
||||
iVar := g.uniqueVarName()
|
||||
|
||||
if t.Elem().Kind() == reflect.Uint8 && elem.Name() == "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+" {")
|
||||
|
||||
@@ -37,6 +37,7 @@ type Generator struct {
|
||||
omitEmpty bool
|
||||
disallowUnknownFields bool
|
||||
fieldNamer FieldNamer
|
||||
simpleBytes bool
|
||||
|
||||
// package path to local alias map for tracking imports
|
||||
imports map[string]string
|
||||
@@ -121,6 +122,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