Skip to content

Commit

Permalink
Validator: added manual tests, fixed bug with nil.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Witkowski committed Oct 21, 2015
1 parent a8774db commit e619e86
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 54 deletions.
9 changes: 8 additions & 1 deletion plugin/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,10 @@ func (p *plugin) generateProto3Message(file *generator.FileDescriptor, message *
fmt.Fprintf(os.Stderr, "WARNING: field %v.%v is a nullable=false, validator.msg_exists has no effect\n", ccTypeName, fieldName)
}
}
if !nullable {
if nullable {
p.P(`if `, variableName, ` != nil {`)
p.In()
} else {
// non-nullable fields in proto3 store actual structs, we need pointers to operate on interaces
variableName = "&(" + variableName + ")"
}
Expand All @@ -242,6 +245,10 @@ func (p *plugin) generateProto3Message(file *generator.FileDescriptor, message *
p.P(`return err`)
p.Out()
p.P(`}`)
if nullable {
p.Out()
p.P(`}`)
}
}
if repeated {
// end the repeated loop
Expand Down
32 changes: 16 additions & 16 deletions test/validator/validator_proto2.pb.go

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

16 changes: 8 additions & 8 deletions test/validator/validator_proto2.proto
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,24 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

syntax = "proto2";
package validator_test;
package validatortest;

import "github.com/gogo/protobuf/gogoproto/gogo.proto";

message ValidatorMessage {
message Embedded {
optional string Identifier = 1 [(gogoproto.validator) = {regex: "[a-z]{2,5}"}];
optional string Identifier = 1 [(gogoproto.validator) = {regex: "^[a-z]{2,5}$"}];
required int64 SomeValue = 2 [(gogoproto.validator) = {int_gt: 0, int_lt: 100}];
}

required string StringReq = 1 [(gogoproto.validator) = {regex: ".{2,5}"}];
required string StringReqNonNull = 2 [(gogoproto.validator) = {regex: ".{2,5}"}, (gogoproto.nullable) = false];
optional string StringOpt = 3 [(gogoproto.validator) = {regex: ".{2,5}"}];
optional string StringOptNonNull = 4 [(gogoproto.validator) = {regex: ".{2,5}"}, (gogoproto.nullable) = false];
required string StringReq = 1 [(gogoproto.validator) = {regex: "^.{2,5}$"}];
required string StringReqNonNull = 2 [(gogoproto.validator) = {regex: "^.{2,5}$"}, (gogoproto.nullable) = false];
optional string StringOpt = 3 [(gogoproto.validator) = {regex: "^.{2,5}$"}];
optional string StringOptNonNull = 4 [(gogoproto.validator) = {regex: "^.{2,5}$"}, (gogoproto.nullable) = false];

required uint32 IntReq = 6 [(gogoproto.validator) = {int_gt: 0}];
required uint32 IntReq = 6 [(gogoproto.validator) = {int_gt: 10}];
required uint32 IntReqNonNull = 7 [(gogoproto.validator) = {int_gt: 0}, (gogoproto.nullable) = false];
repeated uint32 IntRep = 8 [(gogoproto.validator) = {int_gt: 0}];
repeated uint32 IntRep = 8 [(gogoproto.validator) = {int_gt: 10}];
repeated uint32 IntRepNonNull = 9 [(gogoproto.validator) = {int_gt: 0}, (gogoproto.nullable) = false];


Expand Down
46 changes: 26 additions & 20 deletions test/validator/validator_proto3.pb.go

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

14 changes: 7 additions & 7 deletions test/validator/validator_proto3.proto
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,21 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

syntax = "proto3";
package validator_test;
package validatortest;

import "github.com/gogo/protobuf/gogoproto/gogo.proto";

message ValidatorMessage3 {
message Embedded {
string Identifier = 1 [(gogoproto.validator) = {regex: "[a-z]{2,5}"}];
string Identifier = 1 [(gogoproto.validator) = {regex: "^[a-z]{2,5}$"}];
int64 SomeValue = 2 [(gogoproto.validator) = {int_gt: 0, int_lt: 100}];
}
string SomeString = 1 [(gogoproto.validator) = {regex: ".{2,5}"}];
repeated string SomeStringRep = 2 [(gogoproto.validator) = {regex: ".{2,5}"}];
string SomeString = 1 [(gogoproto.validator) = {regex: "^.{2,5}$"}];
repeated string SomeStringRep = 2 [(gogoproto.validator) = {regex: "^.{2,5}$"}];

uint32 SomeInt = 6 [(gogoproto.validator) = {int_gt: 0}];
repeated uint32 SomeIntRep = 7 [(gogoproto.validator) = {int_gt: 0}];
repeated uint32 SomeIntRepNonNull = 8 [(gogoproto.validator) = {int_gt: 0}, (gogoproto.nullable) = false];
uint32 SomeInt = 6 [(gogoproto.validator) = {int_gt: 10}];
repeated uint32 SomeIntRep = 7 [(gogoproto.validator) = {int_gt: 10}];
repeated uint32 SomeIntRepNonNull = 8 [(gogoproto.validator) = {int_gt: 10}, (gogoproto.nullable) = false];

Embedded someEmbedded = 10;
Embedded someEmbeddedNonNullable = 11 [(gogoproto.nullable) = false];
Expand Down
Loading

0 comments on commit e619e86

Please sign in to comment.