Merge branch 'master' into opts

This commit is contained in:
Carlo Alberto Ferraris
2017-11-20 08:43:51 +09:00
committed by GitHub
30 changed files with 341 additions and 112 deletions
+1
View File
@@ -12,6 +12,7 @@ root: .root/src/$(PKG)
clean:
rm -rf .root
rm -rf tests/*_easyjson.go
build:
go build -i -o .root/bin/easyjson $(PKG)/easyjson
+2 -2
View File
@@ -25,12 +25,12 @@ var smallStructData = Entities{
type SearchMetadata struct {
CompletedIn float64 `json:"completed_in"`
Count int `json:"count"`
MaxID int `json:"max_id"`
MaxID int64 `json:"max_id"`
MaxIDStr string `json:"max_id_str"`
NextResults string `json:"next_results"`
Query string `json:"query"`
RefreshURL string `json:"refresh_url"`
SinceID int `json:"since_id"`
SinceID int64 `json:"since_id"`
SinceIDStr string `json:"since_id_str"`
}
+16 -13
View File
@@ -36,16 +36,18 @@ var primitiveDecoders = map[reflect.Kind]string{
}
var primitiveStringDecoders = map[reflect.Kind]string{
reflect.Int: "in.IntStr()",
reflect.Int8: "in.Int8Str()",
reflect.Int16: "in.Int16Str()",
reflect.Int32: "in.Int32Str()",
reflect.Int64: "in.Int64Str()",
reflect.Uint: "in.UintStr()",
reflect.Uint8: "in.Uint8Str()",
reflect.Uint16: "in.Uint16Str()",
reflect.Uint32: "in.Uint32Str()",
reflect.Uint64: "in.Uint64Str()",
reflect.String: "in.String()",
reflect.Int: "in.IntStr()",
reflect.Int8: "in.Int8Str()",
reflect.Int16: "in.Int16Str()",
reflect.Int32: "in.Int32Str()",
reflect.Int64: "in.Int64Str()",
reflect.Uint: "in.UintStr()",
reflect.Uint8: "in.Uint8Str()",
reflect.Uint16: "in.Uint16Str()",
reflect.Uint32: "in.Uint32Str()",
reflect.Uint64: "in.Uint64Str()",
reflect.Uintptr: "in.UintptrStr()",
}
var customDecoders = map[string]string{
@@ -205,8 +207,9 @@ func (g *Generator) genTypeDecoderNoCheck(t reflect.Type, out string, tags field
case reflect.Map:
key := t.Key()
if key.Kind() != reflect.String {
return fmt.Errorf("map type %v not supported: only string keys are allowed", key)
keyDec, ok := primitiveStringDecoders[key.Kind()]
if !ok {
return fmt.Errorf("map type %v not supported: only string and integer keys are allowed", key)
}
elem := t.Elem()
tmpVar := g.uniqueVarName()
@@ -222,7 +225,7 @@ func (g *Generator) genTypeDecoderNoCheck(t reflect.Type, out string, tags field
fmt.Fprintln(g.out, ws+" }")
fmt.Fprintln(g.out, ws+" for !in.IsDelim('}') {")
fmt.Fprintln(g.out, ws+" key := "+g.getType(t.Key())+"(in.String())")
fmt.Fprintln(g.out, ws+" key := "+g.getType(key)+"("+keyDec+")")
fmt.Fprintln(g.out, ws+" in.WantColon()")
fmt.Fprintln(g.out, ws+" var "+tmpVar+" "+g.getType(elem))
+16 -13
View File
@@ -33,16 +33,18 @@ var primitiveEncoders = map[reflect.Kind]string{
}
var primitiveStringEncoders = map[reflect.Kind]string{
reflect.Int: "out.IntStr(int(%v))",
reflect.Int8: "out.Int8Str(int8(%v))",
reflect.Int16: "out.Int16Str(int16(%v))",
reflect.Int32: "out.Int32Str(int32(%v))",
reflect.Int64: "out.Int64Str(int64(%v))",
reflect.Uint: "out.UintStr(uint(%v))",
reflect.Uint8: "out.Uint8Str(uint8(%v))",
reflect.Uint16: "out.Uint16Str(uint16(%v))",
reflect.Uint32: "out.Uint32Str(uint32(%v))",
reflect.Uint64: "out.Uint64Str(uint64(%v))",
reflect.String: "out.String(string(%v))",
reflect.Int: "out.IntStr(int(%v))",
reflect.Int8: "out.Int8Str(int8(%v))",
reflect.Int16: "out.Int16Str(int16(%v))",
reflect.Int32: "out.Int32Str(int32(%v))",
reflect.Int64: "out.Int64Str(int64(%v))",
reflect.Uint: "out.UintStr(uint(%v))",
reflect.Uint8: "out.Uint8Str(uint8(%v))",
reflect.Uint16: "out.Uint16Str(uint16(%v))",
reflect.Uint32: "out.Uint32Str(uint32(%v))",
reflect.Uint64: "out.Uint64Str(uint64(%v))",
reflect.Uintptr: "out.UintptrStr(uintptr(%v))",
}
// fieldTags contains parsed version of json struct field tags.
@@ -194,8 +196,9 @@ func (g *Generator) genTypeEncoderNoCheck(t reflect.Type, in string, tags fieldT
case reflect.Map:
key := t.Key()
if key.Kind() != reflect.String {
return fmt.Errorf("map type %v not supported: only string keys are allowed", key)
keyEnc, ok := primitiveStringEncoders[key.Kind()]
if !ok {
return fmt.Errorf("map key type %v not supported: only string and integer keys are allowed", key)
}
tmpVar := g.uniqueVarName()
@@ -210,7 +213,7 @@ func (g *Generator) genTypeEncoderNoCheck(t reflect.Type, in string, tags fieldT
fmt.Fprintln(g.out, ws+" "+tmpVar+"First := true")
fmt.Fprintln(g.out, ws+" for "+tmpVar+"Name, "+tmpVar+"Value := range "+in+" {")
fmt.Fprintln(g.out, ws+" if "+tmpVar+"First { "+tmpVar+"First = false } else { out.RawByte(',') }")
fmt.Fprintln(g.out, ws+" out.String(string("+tmpVar+"Name))")
fmt.Fprintln(g.out, ws+" "+fmt.Sprintf(keyEnc, tmpVar+"Name"))
fmt.Fprintln(g.out, ws+" out.RawByte(':')")
if err := g.genTypeEncoder(t.Elem(), tmpVar+"Value", tags, indent+2, false); err != nil {
+4
View File
@@ -904,6 +904,10 @@ func (r *Lexer) UintStr() uint {
return uint(r.Uint64Str())
}
func (r *Lexer) UintptrStr() uintptr {
return uintptr(r.Uint64Str())
}
func (r *Lexer) Int8Str() int8 {
s, b := r.unsafeString()
if !r.Ok() {
+53 -4
View File
@@ -2,7 +2,6 @@
package jwriter
import (
"encoding/base64"
"io"
"strconv"
"unicode/utf8"
@@ -105,9 +104,7 @@ func (w *Writer) Base64Bytes(data []byte) {
return
}
w.Buffer.AppendByte('"')
dst := make([]byte, base64.StdEncoding.EncodedLen(len(data)))
base64.StdEncoding.Encode(dst, data)
w.Buffer.AppendBytes(dst)
w.base64(data)
w.Buffer.AppendByte('"')
}
@@ -196,6 +193,13 @@ func (w *Writer) Uint64Str(n uint64) {
w.Buffer.Buf = append(w.Buffer.Buf, '"')
}
func (w *Writer) UintptrStr(n uintptr) {
w.Buffer.EnsureSpace(20)
w.Buffer.Buf = append(w.Buffer.Buf, '"')
w.Buffer.Buf = strconv.AppendUint(w.Buffer.Buf, uint64(n), 10)
w.Buffer.Buf = append(w.Buffer.Buf, '"')
}
func (w *Writer) Int8Str(n int8) {
w.Buffer.EnsureSpace(4)
w.Buffer.Buf = append(w.Buffer.Buf, '"')
@@ -326,3 +330,48 @@ func (w *Writer) String(s string) {
w.Buffer.AppendString(s[p:])
w.Buffer.AppendByte('"')
}
const encode = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
const padChar = '='
func (w *Writer) base64(in []byte) {
if len(in) == 0 {
return
}
w.Buffer.EnsureSpace(((len(in) - 1) / 3 + 1) * 4)
si := 0
n := (len(in) / 3) * 3
for si < n {
// Convert 3x 8bit source bytes into 4 bytes
val := uint(in[si+0])<<16 | uint(in[si+1])<<8 | uint(in[si+2])
w.Buffer.Buf = append(w.Buffer.Buf, encode[val>>18&0x3F], encode[val>>12&0x3F], encode[val>>6&0x3F], encode[val&0x3F])
si += 3
}
remain := len(in) - si
if remain == 0 {
return
}
// Add the remaining small block
val := uint(in[si+0]) << 16
if remain == 2 {
val |= uint(in[si+1]) << 8
}
w.Buffer.Buf = append(w.Buffer.Buf, encode[val>>18&0x3F], encode[val>>12&0x3F])
switch remain {
case 2:
w.Buffer.Buf = append(w.Buffer.Buf, encode[val>>6&0x3F], byte(padChar))
case 1:
w.Buffer.Buf = append(w.Buffer.Buf, byte(padChar), byte(padChar))
}
}
+2 -2
View File
@@ -51,7 +51,7 @@ func (v *Bool) UnmarshalEasyJSON(l *jlexer.Lexer) {
}
// MarshalJSON implements a standard json marshaler interface.
func (v *Bool) MarshalJSON() ([]byte, error) {
func (v Bool) MarshalJSON() ([]byte, error) {
w := jwriter.Writer{}
v.MarshalEasyJSON(&w)
return w.Buffer.BuildBytes(), w.Error
@@ -59,7 +59,7 @@ func (v *Bool) MarshalJSON() ([]byte, error) {
// UnmarshalJSON implements a standard json unmarshaler interface.
func (v *Bool) UnmarshalJSON(data []byte) error {
l := jlexer.Lexer{}
l := jlexer.Lexer{Data: data}
v.UnmarshalEasyJSON(&l)
return l.Error()
}
+2 -2
View File
@@ -51,7 +51,7 @@ func (v *Float32) UnmarshalEasyJSON(l *jlexer.Lexer) {
}
// MarshalJSON implements a standard json marshaler interface.
func (v *Float32) MarshalJSON() ([]byte, error) {
func (v Float32) MarshalJSON() ([]byte, error) {
w := jwriter.Writer{}
v.MarshalEasyJSON(&w)
return w.Buffer.BuildBytes(), w.Error
@@ -59,7 +59,7 @@ func (v *Float32) MarshalJSON() ([]byte, error) {
// UnmarshalJSON implements a standard json unmarshaler interface.
func (v *Float32) UnmarshalJSON(data []byte) error {
l := jlexer.Lexer{}
l := jlexer.Lexer{Data: data}
v.UnmarshalEasyJSON(&l)
return l.Error()
}
+2 -2
View File
@@ -51,7 +51,7 @@ func (v *Float64) UnmarshalEasyJSON(l *jlexer.Lexer) {
}
// MarshalJSON implements a standard json marshaler interface.
func (v *Float64) MarshalJSON() ([]byte, error) {
func (v Float64) MarshalJSON() ([]byte, error) {
w := jwriter.Writer{}
v.MarshalEasyJSON(&w)
return w.Buffer.BuildBytes(), w.Error
@@ -59,7 +59,7 @@ func (v *Float64) MarshalJSON() ([]byte, error) {
// UnmarshalJSON implements a standard json unmarshaler interface.
func (v *Float64) UnmarshalJSON(data []byte) error {
l := jlexer.Lexer{}
l := jlexer.Lexer{Data: data}
v.UnmarshalEasyJSON(&l)
return l.Error()
}
+2 -2
View File
@@ -51,7 +51,7 @@ func (v *Int) UnmarshalEasyJSON(l *jlexer.Lexer) {
}
// MarshalJSON implements a standard json marshaler interface.
func (v *Int) MarshalJSON() ([]byte, error) {
func (v Int) MarshalJSON() ([]byte, error) {
w := jwriter.Writer{}
v.MarshalEasyJSON(&w)
return w.Buffer.BuildBytes(), w.Error
@@ -59,7 +59,7 @@ func (v *Int) MarshalJSON() ([]byte, error) {
// UnmarshalJSON implements a standard json unmarshaler interface.
func (v *Int) UnmarshalJSON(data []byte) error {
l := jlexer.Lexer{}
l := jlexer.Lexer{Data: data}
v.UnmarshalEasyJSON(&l)
return l.Error()
}
+2 -2
View File
@@ -51,7 +51,7 @@ func (v *Int16) UnmarshalEasyJSON(l *jlexer.Lexer) {
}
// MarshalJSON implements a standard json marshaler interface.
func (v *Int16) MarshalJSON() ([]byte, error) {
func (v Int16) MarshalJSON() ([]byte, error) {
w := jwriter.Writer{}
v.MarshalEasyJSON(&w)
return w.Buffer.BuildBytes(), w.Error
@@ -59,7 +59,7 @@ func (v *Int16) MarshalJSON() ([]byte, error) {
// UnmarshalJSON implements a standard json unmarshaler interface.
func (v *Int16) UnmarshalJSON(data []byte) error {
l := jlexer.Lexer{}
l := jlexer.Lexer{Data: data}
v.UnmarshalEasyJSON(&l)
return l.Error()
}
+2 -2
View File
@@ -51,7 +51,7 @@ func (v *Int32) UnmarshalEasyJSON(l *jlexer.Lexer) {
}
// MarshalJSON implements a standard json marshaler interface.
func (v *Int32) MarshalJSON() ([]byte, error) {
func (v Int32) MarshalJSON() ([]byte, error) {
w := jwriter.Writer{}
v.MarshalEasyJSON(&w)
return w.Buffer.BuildBytes(), w.Error
@@ -59,7 +59,7 @@ func (v *Int32) MarshalJSON() ([]byte, error) {
// UnmarshalJSON implements a standard json unmarshaler interface.
func (v *Int32) UnmarshalJSON(data []byte) error {
l := jlexer.Lexer{}
l := jlexer.Lexer{Data: data}
v.UnmarshalEasyJSON(&l)
return l.Error()
}
+2 -2
View File
@@ -51,7 +51,7 @@ func (v *Int64) UnmarshalEasyJSON(l *jlexer.Lexer) {
}
// MarshalJSON implements a standard json marshaler interface.
func (v *Int64) MarshalJSON() ([]byte, error) {
func (v Int64) MarshalJSON() ([]byte, error) {
w := jwriter.Writer{}
v.MarshalEasyJSON(&w)
return w.Buffer.BuildBytes(), w.Error
@@ -59,7 +59,7 @@ func (v *Int64) MarshalJSON() ([]byte, error) {
// UnmarshalJSON implements a standard json unmarshaler interface.
func (v *Int64) UnmarshalJSON(data []byte) error {
l := jlexer.Lexer{}
l := jlexer.Lexer{Data: data}
v.UnmarshalEasyJSON(&l)
return l.Error()
}
+2 -2
View File
@@ -51,7 +51,7 @@ func (v *Int8) UnmarshalEasyJSON(l *jlexer.Lexer) {
}
// MarshalJSON implements a standard json marshaler interface.
func (v *Int8) MarshalJSON() ([]byte, error) {
func (v Int8) MarshalJSON() ([]byte, error) {
w := jwriter.Writer{}
v.MarshalEasyJSON(&w)
return w.Buffer.BuildBytes(), w.Error
@@ -59,7 +59,7 @@ func (v *Int8) MarshalJSON() ([]byte, error) {
// UnmarshalJSON implements a standard json unmarshaler interface.
func (v *Int8) UnmarshalJSON(data []byte) error {
l := jlexer.Lexer{}
l := jlexer.Lexer{Data: data}
v.UnmarshalEasyJSON(&l)
return l.Error()
}
+2 -2
View File
@@ -51,7 +51,7 @@ func (v *String) UnmarshalEasyJSON(l *jlexer.Lexer) {
}
// MarshalJSON implements a standard json marshaler interface.
func (v *String) MarshalJSON() ([]byte, error) {
func (v String) MarshalJSON() ([]byte, error) {
w := jwriter.Writer{}
v.MarshalEasyJSON(&w)
return w.Buffer.BuildBytes(), w.Error
@@ -59,7 +59,7 @@ func (v *String) MarshalJSON() ([]byte, error) {
// UnmarshalJSON implements a standard json unmarshaler interface.
func (v *String) UnmarshalJSON(data []byte) error {
l := jlexer.Lexer{}
l := jlexer.Lexer{Data: data}
v.UnmarshalEasyJSON(&l)
return l.Error()
}
+2 -2
View File
@@ -51,7 +51,7 @@ func (v *Uint) UnmarshalEasyJSON(l *jlexer.Lexer) {
}
// MarshalJSON implements a standard json marshaler interface.
func (v *Uint) MarshalJSON() ([]byte, error) {
func (v Uint) MarshalJSON() ([]byte, error) {
w := jwriter.Writer{}
v.MarshalEasyJSON(&w)
return w.Buffer.BuildBytes(), w.Error
@@ -59,7 +59,7 @@ func (v *Uint) MarshalJSON() ([]byte, error) {
// UnmarshalJSON implements a standard json unmarshaler interface.
func (v *Uint) UnmarshalJSON(data []byte) error {
l := jlexer.Lexer{}
l := jlexer.Lexer{Data: data}
v.UnmarshalEasyJSON(&l)
return l.Error()
}
+2 -2
View File
@@ -51,7 +51,7 @@ func (v *Uint16) UnmarshalEasyJSON(l *jlexer.Lexer) {
}
// MarshalJSON implements a standard json marshaler interface.
func (v *Uint16) MarshalJSON() ([]byte, error) {
func (v Uint16) MarshalJSON() ([]byte, error) {
w := jwriter.Writer{}
v.MarshalEasyJSON(&w)
return w.Buffer.BuildBytes(), w.Error
@@ -59,7 +59,7 @@ func (v *Uint16) MarshalJSON() ([]byte, error) {
// UnmarshalJSON implements a standard json unmarshaler interface.
func (v *Uint16) UnmarshalJSON(data []byte) error {
l := jlexer.Lexer{}
l := jlexer.Lexer{Data: data}
v.UnmarshalEasyJSON(&l)
return l.Error()
}
+2 -2
View File
@@ -51,7 +51,7 @@ func (v *Uint32) UnmarshalEasyJSON(l *jlexer.Lexer) {
}
// MarshalJSON implements a standard json marshaler interface.
func (v *Uint32) MarshalJSON() ([]byte, error) {
func (v Uint32) MarshalJSON() ([]byte, error) {
w := jwriter.Writer{}
v.MarshalEasyJSON(&w)
return w.Buffer.BuildBytes(), w.Error
@@ -59,7 +59,7 @@ func (v *Uint32) MarshalJSON() ([]byte, error) {
// UnmarshalJSON implements a standard json unmarshaler interface.
func (v *Uint32) UnmarshalJSON(data []byte) error {
l := jlexer.Lexer{}
l := jlexer.Lexer{Data: data}
v.UnmarshalEasyJSON(&l)
return l.Error()
}
+2 -2
View File
@@ -51,7 +51,7 @@ func (v *Uint64) UnmarshalEasyJSON(l *jlexer.Lexer) {
}
// MarshalJSON implements a standard json marshaler interface.
func (v *Uint64) MarshalJSON() ([]byte, error) {
func (v Uint64) MarshalJSON() ([]byte, error) {
w := jwriter.Writer{}
v.MarshalEasyJSON(&w)
return w.Buffer.BuildBytes(), w.Error
@@ -59,7 +59,7 @@ func (v *Uint64) MarshalJSON() ([]byte, error) {
// UnmarshalJSON implements a standard json unmarshaler interface.
func (v *Uint64) UnmarshalJSON(data []byte) error {
l := jlexer.Lexer{}
l := jlexer.Lexer{Data: data}
v.UnmarshalEasyJSON(&l)
return l.Error()
}
+2 -2
View File
@@ -51,7 +51,7 @@ func (v *Uint8) UnmarshalEasyJSON(l *jlexer.Lexer) {
}
// MarshalJSON implements a standard json marshaler interface.
func (v *Uint8) MarshalJSON() ([]byte, error) {
func (v Uint8) MarshalJSON() ([]byte, error) {
w := jwriter.Writer{}
v.MarshalEasyJSON(&w)
return w.Buffer.BuildBytes(), w.Error
@@ -59,7 +59,7 @@ func (v *Uint8) MarshalJSON() ([]byte, error) {
// UnmarshalJSON implements a standard json unmarshaler interface.
func (v *Uint8) UnmarshalJSON(data []byte) error {
l := jlexer.Lexer{}
l := jlexer.Lexer{Data: data}
v.UnmarshalEasyJSON(&l)
return l.Error()
}

Some files were not shown because too many files have changed in this diff Show More