From 6e2cb00008cbf09e556b00f87603797fcaa47e09 Mon Sep 17 00:00:00 2001 From: Christopher Speller Date: Mon, 16 Apr 2018 05:37:14 -0700 Subject: Depenancy upgrades and movign to dep. (#8630) --- vendor/github.com/mailru/easyjson/gen/decoder.go | 489 ------------------- vendor/github.com/mailru/easyjson/gen/encoder.go | 382 --------------- vendor/github.com/mailru/easyjson/gen/generator.go | 523 --------------------- .../mailru/easyjson/gen/generator_test.go | 87 ---- 4 files changed, 1481 deletions(-) delete mode 100644 vendor/github.com/mailru/easyjson/gen/decoder.go delete mode 100644 vendor/github.com/mailru/easyjson/gen/encoder.go delete mode 100644 vendor/github.com/mailru/easyjson/gen/generator.go delete mode 100644 vendor/github.com/mailru/easyjson/gen/generator_test.go (limited to 'vendor/github.com/mailru/easyjson/gen') diff --git a/vendor/github.com/mailru/easyjson/gen/decoder.go b/vendor/github.com/mailru/easyjson/gen/decoder.go deleted file mode 100644 index 021933ac8..000000000 --- a/vendor/github.com/mailru/easyjson/gen/decoder.go +++ /dev/null @@ -1,489 +0,0 @@ -package gen - -import ( - "encoding" - "encoding/json" - "fmt" - "reflect" - "strings" - "unicode" - - "github.com/mailru/easyjson" -) - -// Target this byte size for initial slice allocation to reduce garbage collection. -const minSliceBytes = 64 - -func (g *Generator) getDecoderName(t reflect.Type) string { - return g.functionName("decode", t) -} - -var primitiveDecoders = map[reflect.Kind]string{ - reflect.String: "in.String()", - reflect.Bool: "in.Bool()", - reflect.Int: "in.Int()", - reflect.Int8: "in.Int8()", - reflect.Int16: "in.Int16()", - reflect.Int32: "in.Int32()", - reflect.Int64: "in.Int64()", - reflect.Uint: "in.Uint()", - reflect.Uint8: "in.Uint8()", - reflect.Uint16: "in.Uint16()", - reflect.Uint32: "in.Uint32()", - reflect.Uint64: "in.Uint64()", - reflect.Float32: "in.Float32()", - reflect.Float64: "in.Float64()", -} - -var primitiveStringDecoders = map[reflect.Kind]string{ - 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{ - "json.Number": "in.JsonNumber()", -} - -// genTypeDecoder generates decoding code for the type t, but uses unmarshaler interface if implemented by t. -func (g *Generator) genTypeDecoder(t reflect.Type, out string, tags fieldTags, indent int) error { - ws := strings.Repeat(" ", indent) - - unmarshalerIface := reflect.TypeOf((*easyjson.Unmarshaler)(nil)).Elem() - if reflect.PtrTo(t).Implements(unmarshalerIface) { - fmt.Fprintln(g.out, ws+"("+out+").UnmarshalEasyJSON(in)") - return nil - } - - unmarshalerIface = reflect.TypeOf((*json.Unmarshaler)(nil)).Elem() - if reflect.PtrTo(t).Implements(unmarshalerIface) { - fmt.Fprintln(g.out, ws+"if data := in.Raw(); in.Ok() {") - fmt.Fprintln(g.out, ws+" in.AddError( ("+out+").UnmarshalJSON(data) )") - fmt.Fprintln(g.out, ws+"}") - return nil - } - - unmarshalerIface = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem() - if reflect.PtrTo(t).Implements(unmarshalerIface) { - fmt.Fprintln(g.out, ws+"if data := in.UnsafeBytes(); in.Ok() {") - fmt.Fprintln(g.out, ws+" in.AddError( ("+out+").UnmarshalText(data) )") - fmt.Fprintln(g.out, ws+"}") - return nil - } - - err := g.genTypeDecoderNoCheck(t, out, tags, indent) - return err -} - -// genTypeDecoderNoCheck generates decoding code for the type t. -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 != "" { - fmt.Fprintln(g.out, ws+out+" = "+dec) - return nil - } else if dec := primitiveStringDecoders[t.Kind()]; dec != "" && tags.asString { - fmt.Fprintln(g.out, ws+out+" = "+g.getType(t)+"("+dec+")") - return nil - } else if dec := primitiveDecoders[t.Kind()]; dec != "" { - fmt.Fprintln(g.out, ws+out+" = "+g.getType(t)+"("+dec+")") - return nil - } - - switch t.Kind() { - case reflect.Slice: - tmpVar := g.uniqueVarName() - elem := t.Elem() - - if elem.Kind() == reflect.Uint8 { - fmt.Fprintln(g.out, ws+"if in.IsNull() {") - 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()") - fmt.Fprintln(g.out, ws+"}") - - } else { - - capacity := minSliceBytes / elem.Size() - if capacity == 0 { - capacity = 1 - } - - fmt.Fprintln(g.out, ws+"if in.IsNull() {") - 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+" in.Delim('[')") - fmt.Fprintln(g.out, ws+" if "+out+" == nil {") - fmt.Fprintln(g.out, ws+" if !in.IsDelim(']') {") - fmt.Fprintln(g.out, ws+" "+out+" = make("+g.getType(t)+", 0, "+fmt.Sprint(capacity)+")") - fmt.Fprintln(g.out, ws+" } else {") - fmt.Fprintln(g.out, ws+" "+out+" = "+g.getType(t)+"{}") - fmt.Fprintln(g.out, ws+" }") - fmt.Fprintln(g.out, ws+" } else { ") - fmt.Fprintln(g.out, ws+" "+out+" = ("+out+")[:0]") - fmt.Fprintln(g.out, ws+" }") - fmt.Fprintln(g.out, ws+" for !in.IsDelim(']') {") - fmt.Fprintln(g.out, ws+" var "+tmpVar+" "+g.getType(elem)) - - if err := g.genTypeDecoder(elem, tmpVar, tags, indent+2); err != nil { - return err - } - - fmt.Fprintln(g.out, ws+" "+out+" = append("+out+", "+tmpVar+")") - fmt.Fprintln(g.out, ws+" in.WantComma()") - fmt.Fprintln(g.out, ws+" }") - fmt.Fprintln(g.out, ws+" in.Delim(']')") - fmt.Fprintln(g.out, ws+"}") - } - - case reflect.Array: - iterVar := g.uniqueVarName() - elem := t.Elem() - - if elem.Kind() == reflect.Uint8 { - fmt.Fprintln(g.out, ws+"if in.IsNull() {") - fmt.Fprintln(g.out, ws+" in.Skip()") - fmt.Fprintln(g.out, ws+"} else {") - fmt.Fprintln(g.out, ws+" copy("+out+"[:], in.Bytes())") - fmt.Fprintln(g.out, ws+"}") - - } else { - - length := t.Len() - - fmt.Fprintln(g.out, ws+"if in.IsNull() {") - fmt.Fprintln(g.out, ws+" in.Skip()") - fmt.Fprintln(g.out, ws+"} else {") - fmt.Fprintln(g.out, ws+" in.Delim('[')") - fmt.Fprintln(g.out, ws+" "+iterVar+" := 0") - fmt.Fprintln(g.out, ws+" for !in.IsDelim(']') {") - fmt.Fprintln(g.out, ws+" if "+iterVar+" < "+fmt.Sprint(length)+" {") - - if err := g.genTypeDecoder(elem, out+"["+iterVar+"]", tags, indent+3); err != nil { - return err - } - - fmt.Fprintln(g.out, ws+" "+iterVar+"++") - fmt.Fprintln(g.out, ws+" } else {") - fmt.Fprintln(g.out, ws+" in.SkipRecursive()") - fmt.Fprintln(g.out, ws+" }") - fmt.Fprintln(g.out, ws+" in.WantComma()") - fmt.Fprintln(g.out, ws+" }") - fmt.Fprintln(g.out, ws+" in.Delim(']')") - fmt.Fprintln(g.out, ws+"}") - } - - case reflect.Struct: - dec := g.getDecoderName(t) - g.addType(t) - - fmt.Fprintln(g.out, ws+dec+"(in, &"+out+")") - - case reflect.Ptr: - fmt.Fprintln(g.out, ws+"if in.IsNull() {") - 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+" if "+out+" == nil {") - fmt.Fprintln(g.out, ws+" "+out+" = new("+g.getType(t.Elem())+")") - fmt.Fprintln(g.out, ws+" }") - - if err := g.genTypeDecoder(t.Elem(), "*"+out, tags, indent+1); err != nil { - return err - } - - fmt.Fprintln(g.out, ws+"}") - - case reflect.Map: - key := t.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() - - fmt.Fprintln(g.out, ws+"if in.IsNull() {") - fmt.Fprintln(g.out, ws+" in.Skip()") - fmt.Fprintln(g.out, ws+"} else {") - fmt.Fprintln(g.out, ws+" in.Delim('{')") - fmt.Fprintln(g.out, ws+" if !in.IsDelim('}') {") - fmt.Fprintln(g.out, ws+" "+out+" = make("+g.getType(t)+")") - fmt.Fprintln(g.out, ws+" } else {") - fmt.Fprintln(g.out, ws+" "+out+" = nil") - fmt.Fprintln(g.out, ws+" }") - - fmt.Fprintln(g.out, ws+" for !in.IsDelim('}') {") - 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)) - - if err := g.genTypeDecoder(elem, tmpVar, tags, indent+2); err != nil { - return err - } - - fmt.Fprintln(g.out, ws+" ("+out+")[key] = "+tmpVar) - fmt.Fprintln(g.out, ws+" in.WantComma()") - fmt.Fprintln(g.out, ws+" }") - fmt.Fprintln(g.out, ws+" in.Delim('}')") - fmt.Fprintln(g.out, ws+"}") - - case reflect.Interface: - if t.NumMethod() != 0 { - return fmt.Errorf("interface type %v not supported: only interface{} is allowed", t) - } - 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) - } - return nil - -} - -func (g *Generator) genStructFieldDecoder(t reflect.Type, f reflect.StructField) error { - jsonName := g.fieldNamer.GetJSONFieldName(t, f) - tags := parseFieldTags(f) - - if tags.omit { - return nil - } - - fmt.Fprintf(g.out, " case %q:\n", jsonName) - if err := g.genTypeDecoder(f.Type, "out."+f.Name, tags, 3); err != nil { - return err - } - - if tags.required { - fmt.Fprintf(g.out, "%sSet = true\n", f.Name) - } - - return nil -} - -func (g *Generator) genRequiredFieldSet(t reflect.Type, f reflect.StructField) { - tags := parseFieldTags(f) - - if !tags.required { - return - } - - fmt.Fprintf(g.out, "var %sSet bool\n", f.Name) -} - -func (g *Generator) genRequiredFieldCheck(t reflect.Type, f reflect.StructField) { - jsonName := g.fieldNamer.GetJSONFieldName(t, f) - tags := parseFieldTags(f) - - if !tags.required { - return - } - - g.imports["fmt"] = "fmt" - - fmt.Fprintf(g.out, "if !%sSet {\n", f.Name) - fmt.Fprintf(g.out, " in.AddError(fmt.Errorf(\"key '%s' is required\"))\n", jsonName) - fmt.Fprintf(g.out, "}\n") -} - -func mergeStructFields(fields1, fields2 []reflect.StructField) (fields []reflect.StructField) { - used := map[string]bool{} - for _, f := range fields2 { - used[f.Name] = true - fields = append(fields, f) - } - - for _, f := range fields1 { - if !used[f.Name] { - fields = append(fields, f) - } - } - return -} - -func getStructFields(t reflect.Type) ([]reflect.StructField, error) { - if t.Kind() != reflect.Struct { - return nil, fmt.Errorf("got %v; expected a struct", t) - } - - var efields []reflect.StructField - for i := 0; i < t.NumField(); i++ { - f := t.Field(i) - if !f.Anonymous { - continue - } - - t1 := f.Type - if t1.Kind() == reflect.Ptr { - t1 = t1.Elem() - } - - fs, err := getStructFields(t1) - if err != nil { - return nil, fmt.Errorf("error processing embedded field: %v", err) - } - efields = mergeStructFields(efields, fs) - } - - var fields []reflect.StructField - for i := 0; i < t.NumField(); i++ { - f := t.Field(i) - if f.Anonymous { - continue - } - - c := []rune(f.Name)[0] - if unicode.IsUpper(c) { - fields = append(fields, f) - } - } - return mergeStructFields(efields, fields), nil -} - -func (g *Generator) genDecoder(t reflect.Type) error { - switch t.Kind() { - case reflect.Slice, reflect.Array, reflect.Map: - return g.genSliceArrayDecoder(t) - default: - return g.genStructDecoder(t) - } -} - -func (g *Generator) genSliceArrayDecoder(t reflect.Type) error { - switch t.Kind() { - case reflect.Slice, reflect.Array, reflect.Map: - default: - return fmt.Errorf("cannot generate encoder/decoder for %v, not a slice/array/map type", t) - } - - fname := g.getDecoderName(t) - typ := g.getType(t) - - fmt.Fprintln(g.out, "func "+fname+"(in *jlexer.Lexer, out *"+typ+") {") - fmt.Fprintln(g.out, " isTopLevel := in.IsStart()") - err := g.genTypeDecoderNoCheck(t, "*out", fieldTags{}, 1) - if err != nil { - return err - } - fmt.Fprintln(g.out, " if isTopLevel {") - fmt.Fprintln(g.out, " in.Consumed()") - fmt.Fprintln(g.out, " }") - fmt.Fprintln(g.out, "}") - - return nil -} - -func (g *Generator) genStructDecoder(t reflect.Type) error { - if t.Kind() != reflect.Struct { - return fmt.Errorf("cannot generate encoder/decoder for %v, not a struct type", t) - } - - fname := g.getDecoderName(t) - typ := g.getType(t) - - fmt.Fprintln(g.out, "func "+fname+"(in *jlexer.Lexer, out *"+typ+") {") - fmt.Fprintln(g.out, " isTopLevel := in.IsStart()") - fmt.Fprintln(g.out, " if in.IsNull() {") - fmt.Fprintln(g.out, " if isTopLevel {") - fmt.Fprintln(g.out, " in.Consumed()") - fmt.Fprintln(g.out, " }") - fmt.Fprintln(g.out, " in.Skip()") - fmt.Fprintln(g.out, " return") - fmt.Fprintln(g.out, " }") - - // Init embedded pointer fields. - for i := 0; i < t.NumField(); i++ { - f := t.Field(i) - if !f.Anonymous || f.Type.Kind() != reflect.Ptr { - continue - } - fmt.Fprintln(g.out, " out."+f.Name+" = new("+g.getType(f.Type.Elem())+")") - } - - fs, err := getStructFields(t) - if err != nil { - return fmt.Errorf("cannot generate decoder for %v: %v", t, err) - } - - for _, f := range fs { - g.genRequiredFieldSet(t, f) - } - - fmt.Fprintln(g.out, " in.Delim('{')") - fmt.Fprintln(g.out, " for !in.IsDelim('}') {") - fmt.Fprintln(g.out, " key := in.UnsafeString()") - fmt.Fprintln(g.out, " in.WantColon()") - fmt.Fprintln(g.out, " if in.IsNull() {") - fmt.Fprintln(g.out, " in.Skip()") - fmt.Fprintln(g.out, " in.WantComma()") - fmt.Fprintln(g.out, " continue") - fmt.Fprintln(g.out, " }") - - fmt.Fprintln(g.out, " switch key {") - for _, f := range fs { - if err := g.genStructFieldDecoder(t, f); err != nil { - return err - } - } - - fmt.Fprintln(g.out, " default:") - fmt.Fprintln(g.out, " in.SkipRecursive()") - fmt.Fprintln(g.out, " }") - fmt.Fprintln(g.out, " in.WantComma()") - fmt.Fprintln(g.out, " }") - fmt.Fprintln(g.out, " in.Delim('}')") - fmt.Fprintln(g.out, " if isTopLevel {") - fmt.Fprintln(g.out, " in.Consumed()") - fmt.Fprintln(g.out, " }") - - for _, f := range fs { - g.genRequiredFieldCheck(t, f) - } - - fmt.Fprintln(g.out, "}") - - return nil -} - -func (g *Generator) genStructUnmarshaler(t reflect.Type) error { - switch t.Kind() { - case reflect.Slice, reflect.Array, reflect.Map, reflect.Struct: - default: - return fmt.Errorf("cannot generate encoder/decoder for %v, not a struct/slice/array/map type", t) - } - - fname := g.getDecoderName(t) - typ := g.getType(t) - - if !g.noStdMarshalers { - fmt.Fprintln(g.out, "// UnmarshalJSON supports json.Unmarshaler interface") - fmt.Fprintln(g.out, "func (v *"+typ+") UnmarshalJSON(data []byte) error {") - fmt.Fprintln(g.out, " r := jlexer.Lexer{Data: data}") - fmt.Fprintln(g.out, " "+fname+"(&r, v)") - fmt.Fprintln(g.out, " return r.Error()") - fmt.Fprintln(g.out, "}") - } - - fmt.Fprintln(g.out, "// UnmarshalEasyJSON supports easyjson.Unmarshaler interface") - fmt.Fprintln(g.out, "func (v *"+typ+") UnmarshalEasyJSON(l *jlexer.Lexer) {") - fmt.Fprintln(g.out, " "+fname+"(l, v)") - fmt.Fprintln(g.out, "}") - - return nil -} diff --git a/vendor/github.com/mailru/easyjson/gen/encoder.go b/vendor/github.com/mailru/easyjson/gen/encoder.go deleted file mode 100644 index 48cba15d4..000000000 --- a/vendor/github.com/mailru/easyjson/gen/encoder.go +++ /dev/null @@ -1,382 +0,0 @@ -package gen - -import ( - "encoding" - "encoding/json" - "fmt" - "reflect" - "strconv" - "strings" - - "github.com/mailru/easyjson" -) - -func (g *Generator) getEncoderName(t reflect.Type) string { - return g.functionName("encode", t) -} - -var primitiveEncoders = map[reflect.Kind]string{ - reflect.String: "out.String(string(%v))", - reflect.Bool: "out.Bool(bool(%v))", - reflect.Int: "out.Int(int(%v))", - reflect.Int8: "out.Int8(int8(%v))", - reflect.Int16: "out.Int16(int16(%v))", - reflect.Int32: "out.Int32(int32(%v))", - reflect.Int64: "out.Int64(int64(%v))", - reflect.Uint: "out.Uint(uint(%v))", - reflect.Uint8: "out.Uint8(uint8(%v))", - reflect.Uint16: "out.Uint16(uint16(%v))", - reflect.Uint32: "out.Uint32(uint32(%v))", - reflect.Uint64: "out.Uint64(uint64(%v))", - reflect.Float32: "out.Float32(float32(%v))", - reflect.Float64: "out.Float64(float64(%v))", -} - -var primitiveStringEncoders = map[reflect.Kind]string{ - 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. -type fieldTags struct { - name string - - omit bool - omitEmpty bool - noOmitEmpty bool - asString bool - required bool -} - -// parseFieldTags parses the json field tag into a structure. -func parseFieldTags(f reflect.StructField) fieldTags { - var ret fieldTags - - for i, s := range strings.Split(f.Tag.Get("json"), ",") { - switch { - case i == 0 && s == "-": - ret.omit = true - case i == 0: - ret.name = s - case s == "omitempty": - ret.omitEmpty = true - case s == "!omitempty": - ret.noOmitEmpty = true - case s == "string": - ret.asString = true - case s == "required": - ret.required = true - } - } - - return ret -} - -// genTypeEncoder generates code that encodes in of type t into the writer, but uses marshaler interface if implemented by t. -func (g *Generator) genTypeEncoder(t reflect.Type, in string, tags fieldTags, indent int, assumeNonEmpty bool) error { - ws := strings.Repeat(" ", indent) - - marshalerIface := reflect.TypeOf((*easyjson.Marshaler)(nil)).Elem() - if reflect.PtrTo(t).Implements(marshalerIface) { - fmt.Fprintln(g.out, ws+"("+in+").MarshalEasyJSON(out)") - return nil - } - - marshalerIface = reflect.TypeOf((*json.Marshaler)(nil)).Elem() - if reflect.PtrTo(t).Implements(marshalerIface) { - fmt.Fprintln(g.out, ws+"out.Raw( ("+in+").MarshalJSON() )") - return nil - } - - marshalerIface = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem() - if reflect.PtrTo(t).Implements(marshalerIface) { - fmt.Fprintln(g.out, ws+"out.RawText( ("+in+").MarshalText() )") - return nil - } - - err := g.genTypeEncoderNoCheck(t, in, tags, indent, assumeNonEmpty) - return err -} - -// genTypeEncoderNoCheck generates code that encodes in of type t into the writer. -func (g *Generator) genTypeEncoderNoCheck(t reflect.Type, in string, tags fieldTags, indent int, assumeNonEmpty bool) error { - ws := strings.Repeat(" ", indent) - - // Check whether type is primitive, needs to be done after interface check. - if enc := primitiveStringEncoders[t.Kind()]; enc != "" && tags.asString { - fmt.Fprintf(g.out, ws+enc+"\n", in) - return nil - } else if enc := primitiveEncoders[t.Kind()]; enc != "" { - fmt.Fprintf(g.out, ws+enc+"\n", in) - return nil - } - - switch t.Kind() { - case reflect.Slice: - elem := t.Elem() - iVar := g.uniqueVarName() - vVar := g.uniqueVarName() - - if t.Elem().Kind() == reflect.Uint8 { - fmt.Fprintln(g.out, ws+"out.Base64Bytes("+in+")") - } else { - if !assumeNonEmpty { - fmt.Fprintln(g.out, ws+"if "+in+" == nil && (out.Flags & jwriter.NilSliceAsEmpty) == 0 {") - fmt.Fprintln(g.out, ws+` out.RawString("null")`) - fmt.Fprintln(g.out, ws+"} else {") - } else { - fmt.Fprintln(g.out, ws+"{") - } - fmt.Fprintln(g.out, ws+" out.RawByte('[')") - fmt.Fprintln(g.out, ws+" for "+iVar+", "+vVar+" := range "+in+" {") - fmt.Fprintln(g.out, ws+" if "+iVar+" > 0 {") - fmt.Fprintln(g.out, ws+" out.RawByte(',')") - fmt.Fprintln(g.out, ws+" }") - - if err := g.genTypeEncoder(elem, vVar, tags, indent+2, false); err != nil { - return err - } - - fmt.Fprintln(g.out, ws+" }") - fmt.Fprintln(g.out, ws+" out.RawByte(']')") - fmt.Fprintln(g.out, ws+"}") - } - - case reflect.Array: - elem := t.Elem() - iVar := g.uniqueVarName() - - if t.Elem().Kind() == reflect.Uint8 { - 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+" {") - fmt.Fprintln(g.out, ws+" if "+iVar+" > 0 {") - fmt.Fprintln(g.out, ws+" out.RawByte(',')") - fmt.Fprintln(g.out, ws+" }") - - if err := g.genTypeEncoder(elem, in+"["+iVar+"]", tags, indent+1, false); err != nil { - return err - } - - fmt.Fprintln(g.out, ws+"}") - fmt.Fprintln(g.out, ws+"out.RawByte(']')") - } - - case reflect.Struct: - enc := g.getEncoderName(t) - g.addType(t) - - fmt.Fprintln(g.out, ws+enc+"(out, "+in+")") - - case reflect.Ptr: - if !assumeNonEmpty { - fmt.Fprintln(g.out, ws+"if "+in+" == nil {") - fmt.Fprintln(g.out, ws+` out.RawString("null")`) - fmt.Fprintln(g.out, ws+"} else {") - } - - if err := g.genTypeEncoder(t.Elem(), "*"+in, tags, indent+1, false); err != nil { - return err - } - - if !assumeNonEmpty { - fmt.Fprintln(g.out, ws+"}") - } - - case reflect.Map: - key := t.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() - - if !assumeNonEmpty { - fmt.Fprintln(g.out, ws+"if "+in+" == nil && (out.Flags & jwriter.NilMapAsEmpty) == 0 {") - fmt.Fprintln(g.out, ws+" out.RawString(`null`)") - fmt.Fprintln(g.out, ws+"} else {") - } else { - fmt.Fprintln(g.out, ws+"{") - } - fmt.Fprintln(g.out, ws+" out.RawByte('{')") - 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+" "+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 { - return err - } - - fmt.Fprintln(g.out, ws+" }") - fmt.Fprintln(g.out, ws+" out.RawByte('}')") - fmt.Fprintln(g.out, ws+"}") - - case reflect.Interface: - if t.NumMethod() != 0 { - return fmt.Errorf("interface type %v not supported: only interface{} is allowed", t) - } - fmt.Fprintln(g.out, ws+"if m, ok := "+in+".(easyjson.Marshaler); ok {") - fmt.Fprintln(g.out, ws+" m.MarshalEasyJSON(out)") - fmt.Fprintln(g.out, ws+"} else if m, ok := "+in+".(json.Marshaler); ok {") - fmt.Fprintln(g.out, ws+" out.Raw(m.MarshalJSON())") - fmt.Fprintln(g.out, ws+"} else {") - fmt.Fprintln(g.out, ws+" out.Raw(json.Marshal("+in+"))") - fmt.Fprintln(g.out, ws+"}") - - default: - return fmt.Errorf("don't know how to encode %v", t) - } - return nil -} - -func (g *Generator) notEmptyCheck(t reflect.Type, v string) string { - optionalIface := reflect.TypeOf((*easyjson.Optional)(nil)).Elem() - if reflect.PtrTo(t).Implements(optionalIface) { - return "(" + v + ").IsDefined()" - } - - switch t.Kind() { - case reflect.Slice, reflect.Map: - return "len(" + v + ") != 0" - case reflect.Interface, reflect.Ptr: - return v + " != nil" - case reflect.Bool: - return v - case reflect.String: - return v + ` != ""` - case reflect.Float32, reflect.Float64, - reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, - reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - - return v + " != 0" - - default: - // note: Array types don't have a useful empty value - return "true" - } -} - -func (g *Generator) genStructFieldEncoder(t reflect.Type, f reflect.StructField) error { - jsonName := g.fieldNamer.GetJSONFieldName(t, f) - tags := parseFieldTags(f) - - if tags.omit { - return nil - } - noOmitEmpty := (!tags.omitEmpty && !g.omitEmpty) || tags.noOmitEmpty - if noOmitEmpty { - fmt.Fprintln(g.out, " {") - } else { - fmt.Fprintln(g.out, " if", g.notEmptyCheck(f.Type, "in."+f.Name), "{") - } - fmt.Fprintf(g.out, " const prefix string = %q\n", ","+strconv.Quote(jsonName)+":") - fmt.Fprintln(g.out, " if first {") - fmt.Fprintln(g.out, " first = false") - fmt.Fprintln(g.out, " out.RawString(prefix[1:])") - fmt.Fprintln(g.out, " } else {") - fmt.Fprintln(g.out, " out.RawString(prefix)") - fmt.Fprintln(g.out, " }") - - if err := g.genTypeEncoder(f.Type, "in."+f.Name, tags, 2, !noOmitEmpty); err != nil { - return err - } - fmt.Fprintln(g.out, " }") - return nil -} - -func (g *Generator) genEncoder(t reflect.Type) error { - switch t.Kind() { - case reflect.Slice, reflect.Array, reflect.Map: - return g.genSliceArrayMapEncoder(t) - default: - return g.genStructEncoder(t) - } -} - -func (g *Generator) genSliceArrayMapEncoder(t reflect.Type) error { - switch t.Kind() { - case reflect.Slice, reflect.Array, reflect.Map: - default: - return fmt.Errorf("cannot generate encoder/decoder for %v, not a slice/array/map type", t) - } - - fname := g.getEncoderName(t) - typ := g.getType(t) - - fmt.Fprintln(g.out, "func "+fname+"(out *jwriter.Writer, in "+typ+") {") - err := g.genTypeEncoderNoCheck(t, "in", fieldTags{}, 1, false) - if err != nil { - return err - } - fmt.Fprintln(g.out, "}") - return nil -} - -func (g *Generator) genStructEncoder(t reflect.Type) error { - if t.Kind() != reflect.Struct { - return fmt.Errorf("cannot generate encoder/decoder for %v, not a struct type", t) - } - - fname := g.getEncoderName(t) - typ := g.getType(t) - - fmt.Fprintln(g.out, "func "+fname+"(out *jwriter.Writer, in "+typ+") {") - fmt.Fprintln(g.out, " out.RawByte('{')") - fmt.Fprintln(g.out, " first := true") - fmt.Fprintln(g.out, " _ = first") - - fs, err := getStructFields(t) - if err != nil { - return fmt.Errorf("cannot generate encoder for %v: %v", t, err) - } - for _, f := range fs { - if err := g.genStructFieldEncoder(t, f); err != nil { - return err - } - } - - fmt.Fprintln(g.out, " out.RawByte('}')") - fmt.Fprintln(g.out, "}") - - return nil -} - -func (g *Generator) genStructMarshaler(t reflect.Type) error { - switch t.Kind() { - case reflect.Slice, reflect.Array, reflect.Map, reflect.Struct: - default: - return fmt.Errorf("cannot generate encoder/decoder for %v, not a struct/slice/array/map type", t) - } - - fname := g.getEncoderName(t) - typ := g.getType(t) - - if !g.noStdMarshalers { - fmt.Fprintln(g.out, "// MarshalJSON supports json.Marshaler interface") - fmt.Fprintln(g.out, "func (v "+typ+") MarshalJSON() ([]byte, error) {") - fmt.Fprintln(g.out, " w := jwriter.Writer{}") - fmt.Fprintln(g.out, " "+fname+"(&w, v)") - fmt.Fprintln(g.out, " return w.Buffer.BuildBytes(), w.Error") - fmt.Fprintln(g.out, "}") - } - - fmt.Fprintln(g.out, "// MarshalEasyJSON supports easyjson.Marshaler interface") - fmt.Fprintln(g.out, "func (v "+typ+") MarshalEasyJSON(w *jwriter.Writer) {") - fmt.Fprintln(g.out, " "+fname+"(w, v)") - fmt.Fprintln(g.out, "}") - - return nil -} diff --git a/vendor/github.com/mailru/easyjson/gen/generator.go b/vendor/github.com/mailru/easyjson/gen/generator.go deleted file mode 100644 index eb0d70ba2..000000000 --- a/vendor/github.com/mailru/easyjson/gen/generator.go +++ /dev/null @@ -1,523 +0,0 @@ -package gen - -import ( - "bytes" - "fmt" - "hash/fnv" - "io" - "path" - "reflect" - "sort" - "strconv" - "strings" - "unicode" -) - -const pkgWriter = "github.com/mailru/easyjson/jwriter" -const pkgLexer = "github.com/mailru/easyjson/jlexer" -const pkgEasyJSON = "github.com/mailru/easyjson" - -// FieldNamer defines a policy for generating names for struct fields. -type FieldNamer interface { - GetJSONFieldName(t reflect.Type, f reflect.StructField) string -} - -// Generator generates the requested marshaler/unmarshalers. -type Generator struct { - out *bytes.Buffer - - pkgName string - pkgPath string - buildTags string - hashString string - - varCounter int - - noStdMarshalers bool - omitEmpty bool - fieldNamer FieldNamer - - // package path to local alias map for tracking imports - imports map[string]string - - // types that marshalers were requested for by user - marshalers map[reflect.Type]bool - - // types that encoders were already generated for - typesSeen map[reflect.Type]bool - - // types that encoders were requested for (e.g. by encoders of other types) - typesUnseen []reflect.Type - - // function name to relevant type maps to track names of de-/encoders in - // case of a name clash or unnamed structs - functionNames map[string]reflect.Type -} - -// NewGenerator initializes and returns a Generator. -func NewGenerator(filename string) *Generator { - ret := &Generator{ - imports: map[string]string{ - pkgWriter: "jwriter", - pkgLexer: "jlexer", - pkgEasyJSON: "easyjson", - "encoding/json": "json", - }, - fieldNamer: DefaultFieldNamer{}, - marshalers: make(map[reflect.Type]bool), - typesSeen: make(map[reflect.Type]bool), - functionNames: make(map[string]reflect.Type), - } - - // Use a file-unique prefix on all auxiliary funcs to avoid - // name clashes. - hash := fnv.New32() - hash.Write([]byte(filename)) - ret.hashString = fmt.Sprintf("%x", hash.Sum32()) - - return ret -} - -// SetPkg sets the name and path of output package. -func (g *Generator) SetPkg(name, path string) { - g.pkgName = name - g.pkgPath = path -} - -// SetBuildTags sets build tags for the output file. -func (g *Generator) SetBuildTags(tags string) { - g.buildTags = tags -} - -// SetFieldNamer sets field naming strategy. -func (g *Generator) SetFieldNamer(n FieldNamer) { - g.fieldNamer = n -} - -// UseSnakeCase sets snake_case field naming strategy. -func (g *Generator) UseSnakeCase() { - g.fieldNamer = SnakeCaseFieldNamer{} -} - -// UseLowerCamelCase sets lowerCamelCase field naming strategy. -func (g *Generator) UseLowerCamelCase() { - g.fieldNamer = LowerCamelCaseFieldNamer{} -} - -// NoStdMarshalers instructs not to generate standard MarshalJSON/UnmarshalJSON -// methods (only the custom interface). -func (g *Generator) NoStdMarshalers() { - g.noStdMarshalers = true -} - -// OmitEmpty triggers `json=",omitempty"` behaviour by default. -func (g *Generator) OmitEmpty() { - g.omitEmpty = true -} - -// addTypes requests to generate encoding/decoding funcs for the given type. -func (g *Generator) addType(t reflect.Type) { - if g.typesSeen[t] { - return - } - for _, t1 := range g.typesUnseen { - if t1 == t { - return - } - } - g.typesUnseen = append(g.typesUnseen, t) -} - -// Add requests to generate marshaler/unmarshalers and encoding/decoding -// funcs for the type of given object. -func (g *Generator) Add(obj interface{}) { - t := reflect.TypeOf(obj) - if t.Kind() == reflect.Ptr { - t = t.Elem() - } - g.addType(t) - g.marshalers[t] = true -} - -// printHeader prints package declaration and imports. -func (g *Generator) printHeader() { - if g.buildTags != "" { - fmt.Println("// +build ", g.buildTags) - fmt.Println() - } - fmt.Println("// Code generated by easyjson for marshaling/unmarshaling. DO NOT EDIT.") - fmt.Println() - fmt.Println("package ", g.pkgName) - fmt.Println() - - byAlias := map[string]string{} - var aliases []string - for path, alias := range g.imports { - aliases = append(aliases, alias) - byAlias[alias] = path - } - - sort.Strings(aliases) - fmt.Println("import (") - for _, alias := range aliases { - fmt.Printf(" %s %q\n", alias, byAlias[alias]) - } - - fmt.Println(")") - fmt.Println("") - fmt.Println("// suppress unused package warning") - fmt.Println("var (") - fmt.Println(" _ *json.RawMessage") - fmt.Println(" _ *jlexer.Lexer") - fmt.Println(" _ *jwriter.Writer") - fmt.Println(" _ easyjson.Marshaler") - fmt.Println(")") - - fmt.Println() -} - -// Run runs the generator and outputs generated code to out. -func (g *Generator) Run(out io.Writer) error { - g.out = &bytes.Buffer{} - - for len(g.typesUnseen) > 0 { - t := g.typesUnseen[len(g.typesUnseen)-1] - g.typesUnseen = g.typesUnseen[:len(g.typesUnseen)-1] - g.typesSeen[t] = true - - if err := g.genDecoder(t); err != nil { - return err - } - if err := g.genEncoder(t); err != nil { - return err - } - - if !g.marshalers[t] { - continue - } - - if err := g.genStructMarshaler(t); err != nil { - return err - } - if err := g.genStructUnmarshaler(t); err != nil { - return err - } - } - g.printHeader() - _, err := out.Write(g.out.Bytes()) - return err -} - -// fixes vendored paths -func fixPkgPathVendoring(pkgPath string) string { - const vendor = "/vendor/" - if i := strings.LastIndex(pkgPath, vendor); i != -1 { - return pkgPath[i+len(vendor):] - } - return pkgPath -} - -func fixAliasName(alias string) string { - alias = strings.Replace( - strings.Replace(alias, ".", "_", -1), - "-", - "_", - -1, - ) - - if alias[0] == 'v' { // to void conflicting with var names, say v1 - alias = "_" + alias - } - return alias -} - -// pkgAlias creates and returns and import alias for a given package. -func (g *Generator) pkgAlias(pkgPath string) string { - pkgPath = fixPkgPathVendoring(pkgPath) - if alias := g.imports[pkgPath]; alias != "" { - return alias - } - - for i := 0; ; i++ { - alias := fixAliasName(path.Base(pkgPath)) - if i > 0 { - alias += fmt.Sprint(i) - } - - exists := false - for _, v := range g.imports { - if v == alias { - exists = true - break - } - } - - if !exists { - g.imports[pkgPath] = alias - return alias - } - } -} - -// getType return the textual type name of given type that can be used in generated code. -func (g *Generator) getType(t reflect.Type) string { - if t.Name() == "" { - switch t.Kind() { - case reflect.Ptr: - return "*" + g.getType(t.Elem()) - case reflect.Slice: - return "[]" + g.getType(t.Elem()) - case reflect.Array: - return "[" + strconv.Itoa(t.Len()) + "]" + g.getType(t.Elem()) - case reflect.Map: - return "map[" + g.getType(t.Key()) + "]" + g.getType(t.Elem()) - } - } - - if t.Name() == "" || t.PkgPath() == "" { - if t.Kind() == reflect.Struct { - // the fields of an anonymous struct can have named types, - // and t.String() will not be sufficient because it does not - // remove the package name when it matches g.pkgPath. - // so we convert by hand - nf := t.NumField() - lines := make([]string, 0, nf) - for i := 0; i < nf; i++ { - f := t.Field(i) - line := f.Name + " " + g.getType(f.Type) - t := f.Tag - if t != "" { - line += " " + escapeTag(t) - } - lines = append(lines, line) - } - return strings.Join([]string{"struct { ", strings.Join(lines, "; "), " }"}, "") - } - return t.String() - } else if t.PkgPath() == g.pkgPath { - return t.Name() - } - return g.pkgAlias(t.PkgPath()) + "." + t.Name() -} - -// escape a struct field tag string back to source code -func escapeTag(tag reflect.StructTag) string { - t := string(tag) - if strings.ContainsRune(t, '`') { - // there are ` in the string; we can't use ` to enclose the string - return strconv.Quote(t) - } - return "`" + t + "`" -} - -// uniqueVarName returns a file-unique name that can be used for generated variables. -func (g *Generator) uniqueVarName() string { - g.varCounter++ - return fmt.Sprint("v", g.varCounter) -} - -// safeName escapes unsafe characters in pkg/type name and returns a string that can be used -// in encoder/decoder names for the type. -func (g *Generator) safeName(t reflect.Type) string { - name := t.PkgPath() - if t.Name() == "" { - name += "anonymous" - } else { - name += "." + t.Name() - } - - parts := []string{} - part := []rune{} - for _, c := range name { - if unicode.IsLetter(c) || unicode.IsDigit(c) { - part = append(part, c) - } else if len(part) > 0 { - parts = append(parts, string(part)) - part = []rune{} - } - } - return joinFunctionNameParts(false, parts...) -} - -// functionName returns a function name for a given type with a given prefix. If a function -// with this prefix already exists for a type, it is returned. -// -// Method is used to track encoder/decoder names for the type. -func (g *Generator) functionName(prefix string, t reflect.Type) string { - prefix = joinFunctionNameParts(true, "easyjson", g.hashString, prefix) - name := joinFunctionNameParts(true, prefix, g.safeName(t)) - - // Most of the names will be unique, try a shortcut first. - if e, ok := g.functionNames[name]; !ok || e == t { - g.functionNames[name] = t - return name - } - - // Search if the function already exists. - for name1, t1 := range g.functionNames { - if t1 == t && strings.HasPrefix(name1, prefix) { - return name1 - } - } - - // Create a new name in the case of a clash. - for i := 1; ; i++ { - nm := fmt.Sprint(name, i) - if _, ok := g.functionNames[nm]; ok { - continue - } - g.functionNames[nm] = t - return nm - } -} - -// DefaultFieldsNamer implements trivial naming policy equivalent to encoding/json. -type DefaultFieldNamer struct{} - -func (DefaultFieldNamer) GetJSONFieldName(t reflect.Type, f reflect.StructField) string { - jsonName := strings.Split(f.Tag.Get("json"), ",")[0] - if jsonName != "" { - return jsonName - } else { - return f.Name - } -} - -// LowerCamelCaseFieldNamer -type LowerCamelCaseFieldNamer struct{} - -func isLower(b byte) bool { - return b <= 122 && b >= 97 -} - -func isUpper(b byte) bool { - return b >= 65 && b <= 90 -} - -// convert HTTPRestClient to httpRestClient -func lowerFirst(s string) string { - if s == "" { - return "" - } - - str := "" - strlen := len(s) - - /** - Loop each char - If is uppercase: - If is first char, LOWER it - If the following char is lower, LEAVE it - If the following char is upper OR numeric, LOWER it - If is the end of string, LEAVE it - Else lowercase - */ - - foundLower := false - for i := range s { - ch := s[i] - if isUpper(ch) { - if i == 0 { - str += string(ch + 32) - } else if !foundLower { // Currently just a stream of capitals, eg JSONRESTS[erver] - if strlen > (i+1) && isLower(s[i+1]) { - // Next char is lower, keep this a capital - str += string(ch) - } else { - // Either at end of string or next char is capital - str += string(ch + 32) - } - } else { - str += string(ch) - } - } else { - foundLower = true - str += string(ch) - } - } - - return str -} - -func (LowerCamelCaseFieldNamer) GetJSONFieldName(t reflect.Type, f reflect.StructField) string { - jsonName := strings.Split(f.Tag.Get("json"), ",")[0] - if jsonName != "" { - return jsonName - } else { - return lowerFirst(f.Name) - } -} - -// SnakeCaseFieldNamer implements CamelCase to snake_case conversion for fields names. -type SnakeCaseFieldNamer struct{} - -func camelToSnake(name string) string { - var ret bytes.Buffer - - multipleUpper := false - var lastUpper rune - var beforeUpper rune - - for _, c := range name { - // Non-lowercase character after uppercase is considered to be uppercase too. - isUpper := (unicode.IsUpper(c) || (lastUpper != 0 && !unicode.IsLower(c))) - - if lastUpper != 0 { - // Output a delimiter if last character was either the first uppercase character - // in a row, or the last one in a row (e.g. 'S' in "HTTPServer"). - // Do not output a delimiter at the beginning of the name. - - firstInRow := !multipleUpper - lastInRow := !isUpper - - if ret.Len() > 0 && (firstInRow || lastInRow) && beforeUpper != '_' { - ret.WriteByte('_') - } - ret.WriteRune(unicode.ToLower(lastUpper)) - } - - // Buffer uppercase char, do not output it yet as a delimiter may be required if the - // next character is lowercase. - if isUpper { - multipleUpper = (lastUpper != 0) - lastUpper = c - continue - } - - ret.WriteRune(c) - lastUpper = 0 - beforeUpper = c - multipleUpper = false - } - - if lastUpper != 0 { - ret.WriteRune(unicode.ToLower(lastUpper)) - } - return string(ret.Bytes()) -} - -func (SnakeCaseFieldNamer) GetJSONFieldName(t reflect.Type, f reflect.StructField) string { - jsonName := strings.Split(f.Tag.Get("json"), ",")[0] - if jsonName != "" { - return jsonName - } - - return camelToSnake(f.Name) -} - -func joinFunctionNameParts(keepFirst bool, parts ...string) string { - buf := bytes.NewBufferString("") - for i, part := range parts { - if i == 0 && keepFirst { - buf.WriteString(part) - } else { - if len(part) > 0 { - buf.WriteString(strings.ToUpper(string(part[0]))) - } - if len(part) > 1 { - buf.WriteString(part[1:]) - } - } - } - return buf.String() -} diff --git a/vendor/github.com/mailru/easyjson/gen/generator_test.go b/vendor/github.com/mailru/easyjson/gen/generator_test.go deleted file mode 100644 index 0c9d27845..000000000 --- a/vendor/github.com/mailru/easyjson/gen/generator_test.go +++ /dev/null @@ -1,87 +0,0 @@ -package gen - -import ( - "testing" -) - -func TestCamelToSnake(t *testing.T) { - for i, test := range []struct { - In, Out string - }{ - {"", ""}, - {"A", "a"}, - {"SimpleExample", "simple_example"}, - {"internalField", "internal_field"}, - - {"SomeHTTPStuff", "some_http_stuff"}, - {"WriteJSON", "write_json"}, - {"HTTP2Server", "http2_server"}, - {"Some_Mixed_Case", "some_mixed_case"}, - {"do_nothing", "do_nothing"}, - - {"JSONHTTPRPCServer", "jsonhttprpc_server"}, // nothing can be done here without a dictionary - } { - got := camelToSnake(test.In) - if got != test.Out { - t.Errorf("[%d] camelToSnake(%s) = %s; want %s", i, test.In, got, test.Out) - } - } -} - -func TestCamelToLowerCamel(t *testing.T) { - for i, test := range []struct { - In, Out string - }{ - {"", ""}, - {"A", "a"}, - {"SimpleExample", "simpleExample"}, - {"internalField", "internalField"}, - - {"SomeHTTPStuff", "someHTTPStuff"}, - {"WriteJSON", "writeJSON"}, - {"HTTP2Server", "http2Server"}, - - {"JSONHTTPRPCServer", "jsonhttprpcServer"}, // nothing can be done here without a dictionary - } { - got := lowerFirst(test.In) - if got != test.Out { - t.Errorf("[%d] lowerFirst(%s) = %s; want %s", i, test.In, got, test.Out) - } - } -} - -func TestJoinFunctionNameParts(t *testing.T) { - for i, test := range []struct { - keepFirst bool - parts []string - out string - }{ - {false, []string{}, ""}, - {false, []string{"a"}, "A"}, - {false, []string{"simple", "example"}, "SimpleExample"}, - {true, []string{"first", "example"}, "firstExample"}, - {false, []string{"some", "UPPER", "case"}, "SomeUPPERCase"}, - {false, []string{"number", "123"}, "Number123"}, - } { - got := joinFunctionNameParts(test.keepFirst, test.parts...) - if got != test.out { - t.Errorf("[%d] joinFunctionNameParts(%v) = %s; want %s", i, test.parts, got, test.out) - } - } -} - -func TestFixVendorPath(t *testing.T) { - for i, test := range []struct { - In, Out string - }{ - {"", ""}, - {"time", "time"}, - {"project/vendor/subpackage", "subpackage"}, - } { - got := fixPkgPathVendoring(test.In) - if got != test.Out { - t.Errorf("[%d] fixPkgPathVendoring(%s) = %s; want %s", i, test.In, got, test.Out) - } - } - -} -- cgit v1.2.3-1-g7c22