Merge branch 'master' into master

This commit is contained in:
Nicolas S. Dade
2017-01-26 16:38:41 -08:00
committed by GitHub
10 changed files with 543 additions and 135 deletions
+1
View File
@@ -26,6 +26,7 @@ generate: root build
.root/bin/easyjson -all .root/src/$(PKG)/tests/data.go
.root/bin/easyjson -all .root/src/$(PKG)/tests/nothing.go
.root/bin/easyjson -all .root/src/$(PKG)/tests/errors.go
.root/bin/easyjson -snake_case .root/src/$(PKG)/tests/snake.go
.root/bin/easyjson -omit_empty .root/src/$(PKG)/tests/omitempty.go
.root/bin/easyjson -build_tags=use_easyjson .root/src/$(PKG)/benchmark/data.go
+7 -2
View File
@@ -212,8 +212,13 @@ func (g *Generator) genTypeDecoderNoCheck(t reflect.Type, out string, tags field
if t.NumMethod() != 0 {
return fmt.Errorf("interface type %v not supported: only interface{} is allowed", t)
}
fmt.Fprintln(g.out, ws+out+" = in.Interface()")
fmt.Fprintln(g.out, ws+"if m, ok := "+out+".(easyjson.Unmarshaler); ok {")
fmt.Fprintln(g.out, ws+"m.UnmarshalEasyJSON(in)")
fmt.Fprintln(g.out, ws+"} else if m, ok := "+out+".(json.Unmarshaler); ok {")
fmt.Fprintln(g.out, ws+"m.UnmarshalJSON(in.Raw())")
fmt.Fprintln(g.out, ws+"} else {")
fmt.Fprintln(g.out, ws+" "+out+" = in.Interface()")
fmt.Fprintln(g.out, ws+"}")
default:
return fmt.Errorf("don't know how to decode %v", t)
}
+2 -2
View File
@@ -15,7 +15,7 @@ import (
const pkgWriter = "github.com/mailru/easyjson/jwriter"
const pkgLexer = "github.com/mailru/easyjson/jlexer"
const pkgEasyjson = "github.com/mailru/easyjson"
const pkgEasyJSON = "github.com/mailru/easyjson"
// FieldNamer defines a policy for generating names for struct fields.
type FieldNamer interface {
@@ -60,7 +60,7 @@ func NewGenerator(filename string) *Generator {
imports: map[string]string{
pkgWriter: "jwriter",
pkgLexer: "jlexer",
pkgEasyjson: "easyjson",
pkgEasyJSON: "easyjson",
"encoding/json": "json",
},
fieldNamer: DefaultFieldNamer{},
+227 -125
View File
File diff suppressed because it is too large Load Diff
+4 -1
View File
@@ -18,12 +18,15 @@ func TestString(t *testing.T) {
{toParse: `"\u0020"`, want: " "},
{toParse: `"\u0020-\t"`, want: " -\t"},
{toParse: `"\ufffd\uFFFD"`, want: "\ufffd\ufffd"},
{toParse: `"\ud83d\ude00"`, want: "😀"},
{toParse: `"\ud83d\ude08"`, want: "😈"},
{toParse: `"\ud8"`, wantError: true},
{toParse: `"test"junk`, want: "test"},
{toParse: `5`, wantError: true}, // not a string
{toParse: `"\x"`, wantError: true}, // invalid escape
{toParse: `"\ud800"`, wantError: true}, // invalid utf-8 char
{toParse: `"\ud800"`, want: ""}, // invalid utf-8 char; return replacement char
} {
l := Lexer{Data: []byte(test.toParse)}
+10 -5
View File
@@ -23,8 +23,9 @@ const (
type Writer struct {
Flags Flags
Error error
Buffer buffer.Buffer
Error error
Buffer buffer.Buffer
NoEscapeHTML bool
}
// Size returns the size of the data that was written out.
@@ -225,10 +226,14 @@ func (w *Writer) Bool(v bool) {
const chars = "0123456789abcdef"
func isNotEscapedSingleChar(c byte) bool {
func isNotEscapedSingleChar(c byte, escapeHTML bool) bool {
// Note: might make sense to use a table if there are more chars to escape. With 4 chars
// it benchmarks the same.
return c != '<' && c != '\\' && c != '"' && c != '>' && c >= 0x20 && c < utf8.RuneSelf
if escapeHTML {
return c != '<' && c != '>' && c != '&' && c != '\\' && c != '"' && c >= 0x20 && c < utf8.RuneSelf
} else {
return c != '\\' && c != '"' && c >= 0x20 && c < utf8.RuneSelf
}
}
func (w *Writer) String(s string) {
@@ -242,7 +247,7 @@ func (w *Writer) String(s string) {
for i := 0; i < len(s); {
c := s[i]
if isNotEscapedSingleChar(c) {
if isNotEscapedSingleChar(c, !w.NoEscapeHTML) {
// single-width character, no escaping is required
i++
continue
+12
View File
@@ -207,3 +207,15 @@ func TestNestedEasyJsonMarshal(t *testing.T) {
}
}
}
func TestUnmarshalStructWithEmbeddedPtrStruct(t *testing.T) {
var s = StructWithInterface{Field2: &EmbeddedStruct{}}
var err error
err = easyjson.Unmarshal([]byte(structWithInterfaceString), &s)
if err != nil {
t.Errorf("easyjson.Unmarshal() error: %v", err)
}
if !reflect.DeepEqual(s, structWithInterfaceValueFilled) {
t.Errorf("easyjson.Unmarshal() = %#v; want %#v", s, structWithInterfaceValueFilled)
}
}
+14
View File
@@ -634,3 +634,17 @@ type EncodingFlagsTestMap struct {
type EncodingFlagsTestSlice struct {
F []string
}
type StructWithInterface struct {
Field1 int `json:"f1"`
Field2 interface{} `json:"f2"`
Field3 string `json:"f3"`
}
type EmbeddedStruct struct {
Field1 int `json:"f1"`
Field2 string `json:"f2"`
}
var structWithInterfaceString = `{"f1":1,"f2":{"f1":11,"f2":"22"},"f3":"3"}`
var structWithInterfaceValueFilled = StructWithInterface{1, &EmbeddedStruct{11, "22"}, "3"}
+23
View File
@@ -0,0 +1,23 @@
package tests
//easyjson:json
type ErrorIntSlice []int
//easyjson:json
type ErrorBoolSlice []bool
//easyjson:json
type ErrorUintSlice []uint
//easyjson:json
type ErrorStruct struct {
Int int `json:"int"`
String string `json:"string"`
Slice []int `json:"slice"`
IntSlice []int `json:"int_slice"`
}
type ErrorNestedStruct struct {
ErrorStruct ErrorStruct `json:"error_struct"`
Int int `json:"int"`
}
+243
View File
@@ -0,0 +1,243 @@
package tests
import (
"testing"
"github.com/mailru/easyjson/jlexer"
)
func TestMultipleErrorsInt(t *testing.T) {
for i, test := range []struct {
Data []byte
Offsets []int
}{
{
Data: []byte(`[1, 2, 3, "4", "5"]`),
Offsets: []int{10, 15},
},
{
Data: []byte(`[1, {"2":"3"}, 3, "4"]`),
Offsets: []int{4, 18},
},
{
Data: []byte(`[1, "2", "3", "4", "5", "6"]`),
Offsets: []int{4, 9, 14, 19, 24},
},
{
Data: []byte(`[1, 2, 3, 4, "5"]`),
Offsets: []int{13},
},
{
Data: []byte(`[{"1": "2"}]`),
Offsets: []int{1},
},
} {
l := jlexer.Lexer{
Data: test.Data,
UseMultipleErrors: true,
}
var v ErrorIntSlice
v.UnmarshalEasyJSON(&l)
errors := l.GetNonFatalErrors()
if len(errors) != len(test.Offsets) {
t.Errorf("[%d] TestMultipleErrorsInt(): errornum: want: %d, got %d", i, len(test.Offsets), len(errors))
return
}
for ii, e := range errors {
if e.Offset != test.Offsets[ii] {
t.Errorf("[%d] TestMultipleErrorsInt(): offset[%d]: want %d, got %d", i, ii, test.Offsets[ii], e.Offset)
}
}
}
}
func TestMultipleErrorsBool(t *testing.T) {
for i, test := range []struct {
Data []byte
Offsets []int
}{
{
Data: []byte(`[true, false, true, false]`),
},
{
Data: []byte(`["test", "value", "lol", "1"]`),
Offsets: []int{1, 9, 18, 25},
},
{
Data: []byte(`[true, 42, {"a":"b", "c":"d"}, false]`),
Offsets: []int{7, 11},
},
} {
l := jlexer.Lexer{
Data: test.Data,
UseMultipleErrors: true,
}
var v ErrorBoolSlice
v.UnmarshalEasyJSON(&l)
errors := l.GetNonFatalErrors()
if len(errors) != len(test.Offsets) {
t.Errorf("[%d] TestMultipleErrorsBool(): errornum: want: %d, got %d", i, len(test.Offsets), len(errors))
return
}
for ii, e := range errors {
if e.Offset != test.Offsets[ii] {
t.Errorf("[%d] TestMultipleErrorsBool(): offset[%d]: want %d, got %d", i, ii, test.Offsets[ii], e.Offset)
}
}
}
}
func TestMultipleErrorsUint(t *testing.T) {
for i, test := range []struct {
Data []byte
Offsets []int
}{
{
Data: []byte(`[42, 42, 42]`),
},
{
Data: []byte(`[17, "42", 32]`),
Offsets: []int{5},
},
{
Data: []byte(`["zz", "zz"]`),
Offsets: []int{1, 7},
},
{
Data: []byte(`[{}, 42]`),
Offsets: []int{1},
},
} {
l := jlexer.Lexer{
Data: test.Data,
UseMultipleErrors: true,
}
var v ErrorUintSlice
v.UnmarshalEasyJSON(&l)
errors := l.GetNonFatalErrors()
if len(errors) != len(test.Offsets) {
t.Errorf("[%d] TestMultipleErrorsUint(): errornum: want: %d, got %d", i, len(test.Offsets), len(errors))
return
}
for ii, e := range errors {
if e.Offset != test.Offsets[ii] {
t.Errorf("[%d] TestMultipleErrorsUint(): offset[%d]: want %d, got %d", i, ii, test.Offsets[ii], e.Offset)
}
}
}
}
func TestMultipleErrorsStruct(t *testing.T) {
for i, test := range []struct {
Data []byte
Offsets []int
}{
{
Data: []byte(`{"string": "test", "slice":[42, 42, 42], "int_slice":[1, 2, 3]}`),
},
{
Data: []byte(`{"string": {"test": "test"}, "slice":[42, 42, 42], "int_slice":["1", 2, 3]}`),
Offsets: []int{11, 64},
},
{
Data: []byte(`{"slice": [42, 42], "string": {"test": "test"}, "int_slice":["1", "2", 3]}`),
Offsets: []int{30, 61, 66},
},
{
Data: []byte(`{"string": "test", "slice": {}}`),
Offsets: []int{28},
},
{
Data: []byte(`{"slice":5, "string" : "test"}`),
Offsets: []int{9},
},
{
Data: []byte(`{"slice" : "test", "string" : "test"}`),
Offsets: []int{11},
},
{
Data: []byte(`{"slice": "", "string" : {}, "int":{}}`),
Offsets: []int{10, 25, 35},
},
} {
l := jlexer.Lexer{
Data: test.Data,
UseMultipleErrors: true,
}
var v ErrorStruct
v.UnmarshalEasyJSON(&l)
errors := l.GetNonFatalErrors()
if len(errors) != len(test.Offsets) {
t.Errorf("[%d] TestMultipleErrorsStruct(): errornum: want: %d, got %d", i, len(test.Offsets), len(errors))
return
}
for ii, e := range errors {
if e.Offset != test.Offsets[ii] {
t.Errorf("[%d] TestMultipleErrorsStruct(): offset[%d]: want %d, got %d", i, ii, test.Offsets[ii], e.Offset)
}
}
}
}
func TestMultipleErrorsNestedStruct(t *testing.T) {
for i, test := range []struct {
Data []byte
Offsets []int
}{
{
Data: []byte(`{"error_struct":{}}`),
},
{
Data: []byte(`{"error_struct":5}`),
Offsets: []int{16},
},
{
Data: []byte(`{"error_struct":[]}`),
Offsets: []int{16},
},
{
Data: []byte(`{"error_struct":{"int":{}}}`),
Offsets: []int{23},
},
{
Data: []byte(`{"error_struct":{"int_slice":{}}, "int":4}`),
Offsets: []int{29},
},
{
Data: []byte(`{"error_struct":{"int_slice":["1", 2, "3"]}, "int":[]}`),
Offsets: []int{30, 38, 51},
},
} {
l := jlexer.Lexer{
Data: test.Data,
UseMultipleErrors: true,
}
var v ErrorNestedStruct
v.UnmarshalEasyJSON(&l)
errors := l.GetNonFatalErrors()
if len(errors) != len(test.Offsets) {
t.Errorf("[%d] TestMultipleErrorsNestedStruct(): errornum: want: %d, got %d", i, len(test.Offsets), len(errors))
return
}
for ii, e := range errors {
if e.Offset != test.Offsets[ii] {
t.Errorf("[%d] TestMultipleErrorsNestedStruct(): offset[%d]: want %d, got %d", i, ii, test.Offsets[ii], e.Offset)
}
}
}
}