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

Commit

Permalink
Adds Enum Supports
Browse files Browse the repository at this point in the history
  • Loading branch information
jackskj committed Oct 29, 2019
1 parent a8019cc commit db46653
Show file tree
Hide file tree
Showing 20 changed files with 752 additions and 194 deletions.
Empty file modified examples/generate.sh
100644 → 100755
Empty file.
158 changes: 97 additions & 61 deletions examples/query.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions examples/query.pb.map.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions examples/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ message Author {
string author_password = 3;
string author_email = 4;
string author_bio = 5;
string author_favourite_section = 6;
Section author_favourite_section = 6;
}

message Post {
uint32 post_id = 1;
uint32 post_blog_id = 2;
uint32 post_author_id = 3;
google.protobuf.Timestamp post_created_on = 4;
string post_section = 5;
Section post_section = 5;
string post_subject = 6;
string draft = 7;
string post_body = 8;
Expand All @@ -73,6 +73,13 @@ message Tag {
string tag_name = 2;
}

enum Section {
cooking = 0;
painting = 1;
woodworking = 2;
snowboarding = 3;
}

message InsertAuthorRequest{
uint32 id = 1;
string username = 2;
Expand Down
35 changes: 31 additions & 4 deletions mapper/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ var _ = fmt.Errorf
var _ = log.Fatal
var _ = reflect.Append

type mapName string // Map name corresponds to the proto message name
type columnName string // Name of the column returned by executing SQL
type mapType int
type (
mapName string // Map name corresponds to the proto message name
columnName string // Name of the column returned by executing SQL
mapType int
)

// Map can either be, a top level element (proto response)
// Association, (nested field) or
Expand All @@ -28,6 +30,10 @@ const (
Collection
)

var (
EnumVals map[string]map[string]int32
)

// Representation of SQL response mapping to a proto response message
// protoc-gen-map generates service server methods which implement interfaces generated by protoc-gen-go
// each RPC and therefore SQL has corresponding Mapper
Expand Down Expand Up @@ -201,15 +207,17 @@ func generateSqlMap(sqlMap *SqlMap, protoMsg interface{}, columns []string) {
strings.ToLower(field.Name): true,
}
if _, found := possibleFieldNames[c]; found {
sqlMapColumns[columnName(c)] = &ProtoField{
protoField := ProtoField{
field: &field,
index: i,
}
sqlMapColumns[columnName(c)] = &protoField
// remove claimed column
columns[j] = columns[len(columns)-1]
columns = columns[:len(columns)-1]
break
}

}
} else if subMapType, isSubMap, err := isSubMap(field); isSubMap == true {
if err != nil {
Expand Down Expand Up @@ -287,11 +295,20 @@ func isAlowedType(field reflect.StructField) bool {
return true
} else if kind == reflect.Ptr && allowedTypes[field.Type] {
return true
} else if isEnum(field) {
return true
} else {
return false
}
}

func isEnum(field reflect.StructField) bool {
if field.Type.Kind() == reflect.Int32 && field.Type.Name() != "int32" {
return true
}
return false
}

// Tests if the struct field is a submap, this is true for nester and repeated fields
// Repeated fields of primitive types or Timestamp/Empty types are not allowed
func isSubMap(field reflect.StructField) (string, bool, error) {
Expand Down Expand Up @@ -487,6 +504,16 @@ func newSubMapVals(sqlMap *SqlMap, sqlMapVals *SqlMapVals) {
}
}

func RegisterEnums(enums map[string]map[string]int32) {
if EnumVals == nil {
EnumVals = enums
} else {
for enumMapName, enumMapVals := range enums {
EnumVals[enumMapName] = enumMapVals
}
}
}

//If non-breaking issues are found, this function prints them
func (m *Mapper) Log() {
if len(m.Logs) != 0 {
Expand Down
15 changes: 15 additions & 0 deletions mapper/mapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,21 @@ func TestMappingService(t *testing.T) {
if isVerbose {
printResp(resp)
}
resp, err = testMappingClient.SimpleEnum(ctx, &td.EmptyRequest{})
if err != nil {
log.Printf("stream error: %s\n", err)
}
if isVerbose {
printResp(resp)
}
resp, err = testMappingClient.NestedEnum(ctx, &td.EmptyRequest{})
if err != nil {
log.Printf("stream error: %s\n", err)
}
if isVerbose {
log.Printf(" asdasdasdsa: %s\n", err)
printResp(resp)
}
posts, err = testMappingClient.NullResoultsForSubmaps(ctx, &td.EmptyRequest{})
if err != nil {
log.Fatalf("stream error: %s", err)
Expand Down
Loading

0 comments on commit db46653

Please sign in to comment.