Skip to content

Commit

Permalink
Add support for setting the repeat count (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
rogchap committed Jul 19, 2019
1 parent 1acca46 commit bc7168f
Show file tree
Hide file tree
Showing 6 changed files with 463 additions and 479 deletions.
752 changes: 359 additions & 393 deletions protobuf/protoc-gen-gogrpcmock/example/complete.mock.go

Large diffs are not rendered by default.

104 changes: 52 additions & 52 deletions protobuf/protoc-gen-gogrpcmock/example/complete.pb.go

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

2 changes: 1 addition & 1 deletion protobuf/protoc-gen-gogrpcmock/example/complete.proto
Expand Up @@ -27,7 +27,7 @@ message Complete {
repeated string word = 7;
string url = 8;
int32 single_number = 9;
repeated int64 repeated_number = 10;
repeated int64 repeated_number = 10 [ (grpcmock.field) = {repeatn : 3} ];
int32 lat = 11;
int32 lng = 12;
string words = 13 [ (grpcmock.field) = {words : true} ];
Expand Down
21 changes: 15 additions & 6 deletions protobuf/protoc-gen-gogrpcmock/plugin/plugin.go
Expand Up @@ -213,7 +213,7 @@ func (g *grpcmock) generateMockString(fieldName, fieldType string, repeated bool
g.P(fieldName, `: `, fieldType, `{`)
g.In()

for i := 0; i < repeatCount; i++ {
for i := 0; i < getRepeatCount(field); i++ {
g.P(`"`, generateStringValue(fieldName, field), `",`)
}

Expand All @@ -231,7 +231,7 @@ func (g *grpcmock) generateMockInt(fieldName, fieldType string, repeated bool, f
g.P(fieldName, `: `, fieldType, `{`)
g.In()

for i := 0; i < repeatCount; i++ {
for i := 0; i < getRepeatCount(field); i++ {
g.P(generateIntValue(fieldName, field), `,`)
}

Expand All @@ -247,7 +247,7 @@ func (g *grpcmock) generateMockFloat(fieldName, fieldType string, repeated bool,
g.P(fieldName, `: `, fieldType, `{`)
g.In()

for i := 0; i < repeatCount; i++ {
for i := 0; i < getRepeatCount(field); i++ {
g.P(generateFloatValue(fieldName, field), `,`)
}

Expand Down Expand Up @@ -286,7 +286,7 @@ func (g *grpcmock) generateMockInnerMessage(fieldName, fieldType string, repeate
length := 1

if repeated {
length = repeatCount
length = getRepeatCount(field)
g.P(fieldName, `: `, fieldType, `{`)
g.In()
} else {
Expand All @@ -298,7 +298,9 @@ func (g *grpcmock) generateMockInnerMessage(fieldName, fieldType string, repeate
for i := 0; i < length; i++ {
switch fieldType {
case "time.Time":
g.P(`time.Now(),`)
g.P(`time.Now().Add(` + strconv.Itoa(r.Intn(1000)) + `),`)
case "time.Duration":
g.P(`time.Duration(` + strconv.Itoa(r.Intn(1000)) + `),`)
case "*time.Time", "*time.Duration":
// g.P(`&time.Time{},`)
g.P(`nil,`)
Expand Down Expand Up @@ -470,7 +472,7 @@ func generateIntValue(fieldName string, field *descriptor.FieldDescriptorProto)
}
}

return strconv.Itoa(int(r.Intn(n)))
return strconv.Itoa(r.Intn(n))
}

func generateFloatValue(fieldName string, field *descriptor.FieldDescriptorProto) string {
Expand Down Expand Up @@ -507,3 +509,10 @@ func boolFromPtr(b *bool) bool {
}
return *b
}

func getRepeatCount(field *descriptor.FieldDescriptorProto) int {
if mocks := getFieldMocksIfAny(field); mocks != nil && mocks.Repeatn != nil {
return int(mocks.GetRepeatn())
}
return repeatCount
}
62 changes: 35 additions & 27 deletions protobuf/s12proto/grpcmock.pb.go

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

1 change: 1 addition & 0 deletions protobuf/s12proto/grpcmock.proto
Expand Up @@ -33,4 +33,5 @@ message FieldMock {
optional bool hexcolor = 19;
repeated string prefix = 20;
optional int32 floatn = 21;
optional int32 repeatn = 22;
}

0 comments on commit bc7168f

Please sign in to comment.