Skip to content
This repository has been archived by the owner on Aug 31, 2020. It is now read-only.

Commit

Permalink
Adds support to protoc-gen-go 1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
jackskj committed May 11, 2020
1 parent c7a6073 commit ac70b1b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
18 changes: 16 additions & 2 deletions mapper/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"reflect"
"strings"
"unicode"

"github.com/golang/protobuf/ptypes/timestamp"
)
Expand Down Expand Up @@ -244,6 +245,7 @@ func generateSqlMap(sqlMap *SqlMap, protoMsg interface{}, columns []string) {
} else if subMapType == "collection" {
subMap.MapType = Collection
subMap.ProtoSliceElem = field.Type.Elem()
log.Println(protoStruct.Field(i))
generateSqlMap(&subMap, field.Type, columns)
if subMap.Error != nil {
sqlMap.Error = subMap.Error
Expand Down Expand Up @@ -313,11 +315,11 @@ func isEnum(field reflect.StructField) bool {
return false
}

// Tests if the struct field is a submap, this is true for nester and repeated fields
// Tests if the struct field is a submap, this is true for nested and repeated fields
// Repeated fields of primitive types or Timestamp/Empty types are not allowed
func isSubMap(field reflect.StructField) (string, bool, error) {
kind := field.Type.Kind()
if field.Name[0:3] == "XXX" {
if isProtoInternal(field.Name) == true {
return "proto_field", false, nil
} else if kind == reflect.Slice {
if allowedKinds[field.Type.Elem().Kind()] {
Expand Down Expand Up @@ -518,6 +520,18 @@ func RegisterEnums(enums map[string]map[string]int32) {
}
}

// tests whether the field name is internal to go protobuff
// any field name starting with XXX as well as unexported fields are considered specific to protos
// this is trucky as this defunition changes with different versions of golang proto generators
func isProtoInternal(fieldName string) bool {
if fieldName[0:3] == "XXX" {
return true
} else if unicode.IsLower(rune(fieldName[0])) { // unexported fields
return true
}
return false
}

//If non-breaking issues are found while generating sqlmap, this function prints them
func logSqlMap(sqlm *SqlMap, name string) {
if len(sqlm.Logs) != 0 {
Expand Down
30 changes: 17 additions & 13 deletions mapper/param_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,25 @@ import (
)

type ParamTest struct {
NilVal *string
ShortVal string
ComplexVal string
LongVal string
IntVal int
TimeVal timestamp.Timestamp
SliceVal []string
NilVal *string
ShortVal string
ComplexVal string
LongVal string
WhitespacVal string
IntVal int
TimeVal timestamp.Timestamp
SliceVal []string
}

func TestParametarizedQuery(t *testing.T) {
testingVal := ParamTest{
ShortVal: "a",
ComplexVal: "!#$%&()*+,-./:;<=>?@[\\]^_{|}~",
LongVal: strings.Repeat("a", 100),
IntVal: 6,
TimeVal: td.GetSampleTS(),
SliceVal: []string{"a", "b", "c", "d", "e"},
ShortVal: "a",
ComplexVal: "!#$%&()*+,-./:;<=>?@[\\]^_{|}~",
LongVal: strings.Repeat("a", 100),
WhitespacVal: "< >",
IntVal: 6,
TimeVal: td.GetSampleTS(),
SliceVal: []string{"a", "b", "c", "d", "e"},
}
paramBuff := &bytes.Buffer{}

Expand Down Expand Up @@ -73,6 +75,8 @@ select {{ param .TimeVal }}
select {{ param .SliceVal }}
---{{ .WhitespacVal }}---
select
{{- range .SliceVal }}
{{- param . -}} ,
Expand Down

0 comments on commit ac70b1b

Please sign in to comment.