Created
November 28, 2016 11:34
-
-
Save johanbrandhorst/3729f49bf430e6c6fc38a45e72a63612 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Install protoc, protoc-gen-govalidators | |
protoc my.proto --govalidators_out=gogoimport=true,Mgoogle/protobuf/empty.proto=github.com/gogo/protobuf/types,Mgoogle/protobuf/timestamp.proto=github.com/gogo/protobuf/types:./ -I/usr/local/include -I./ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Protocol Buffers for Go with Gadgets | |
// | |
// Copyright (c) 2013, The GoGo Authors. All rights reserved. | |
// http://github.com/gogo/protobuf | |
// | |
// Redistribution and use in source and binary forms, with or without | |
// modification, are permitted provided that the following conditions are | |
// met: | |
// | |
// * Redistributions of source code must retain the above copyright | |
// notice, this list of conditions and the following disclaimer. | |
// * Redistributions in binary form must reproduce the above | |
// copyright notice, this list of conditions and the following disclaimer | |
// in the documentation and/or other materials provided with the | |
// distribution. | |
// | |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
syntax = "proto2"; | |
package gogoproto; | |
import "google/protobuf/descriptor.proto"; | |
option java_package = "com.google.protobuf"; | |
option java_outer_classname = "GoGoProtos"; | |
extend google.protobuf.EnumOptions { | |
optional bool goproto_enum_prefix = 62001; | |
optional bool goproto_enum_stringer = 62021; | |
optional bool enum_stringer = 62022; | |
optional string enum_customname = 62023; | |
} | |
extend google.protobuf.EnumValueOptions { | |
optional string enumvalue_customname = 66001; | |
} | |
extend google.protobuf.FileOptions { | |
optional bool goproto_getters_all = 63001; | |
optional bool goproto_enum_prefix_all = 63002; | |
optional bool goproto_stringer_all = 63003; | |
optional bool verbose_equal_all = 63004; | |
optional bool face_all = 63005; | |
optional bool gostring_all = 63006; | |
optional bool populate_all = 63007; | |
optional bool stringer_all = 63008; | |
optional bool onlyone_all = 63009; | |
optional bool equal_all = 63013; | |
optional bool description_all = 63014; | |
optional bool testgen_all = 63015; | |
optional bool benchgen_all = 63016; | |
optional bool marshaler_all = 63017; | |
optional bool unmarshaler_all = 63018; | |
optional bool stable_marshaler_all = 63019; | |
optional bool sizer_all = 63020; | |
optional bool goproto_enum_stringer_all = 63021; | |
optional bool enum_stringer_all = 63022; | |
optional bool unsafe_marshaler_all = 63023; | |
optional bool unsafe_unmarshaler_all = 63024; | |
optional bool goproto_extensions_map_all = 63025; | |
optional bool goproto_unrecognized_all = 63026; | |
optional bool gogoproto_import = 63027; | |
optional bool protosizer_all = 63028; | |
optional bool compare_all = 63029; | |
} | |
extend google.protobuf.MessageOptions { | |
optional bool goproto_getters = 64001; | |
optional bool goproto_stringer = 64003; | |
optional bool verbose_equal = 64004; | |
optional bool face = 64005; | |
optional bool gostring = 64006; | |
optional bool populate = 64007; | |
optional bool stringer = 67008; | |
optional bool onlyone = 64009; | |
optional bool equal = 64013; | |
optional bool description = 64014; | |
optional bool testgen = 64015; | |
optional bool benchgen = 64016; | |
optional bool marshaler = 64017; | |
optional bool unmarshaler = 64018; | |
optional bool stable_marshaler = 64019; | |
optional bool sizer = 64020; | |
optional bool unsafe_marshaler = 64023; | |
optional bool unsafe_unmarshaler = 64024; | |
optional bool goproto_extensions_map = 64025; | |
optional bool goproto_unrecognized = 64026; | |
optional bool protosizer = 64028; | |
optional bool compare = 64029; | |
} | |
extend google.protobuf.FieldOptions { | |
optional bool nullable = 65001; | |
optional bool embed = 65002; | |
optional string customtype = 65003; | |
optional string customname = 65004; | |
optional string jsontag = 65005; | |
optional string moretags = 65006; | |
optional string casttype = 65007; | |
optional string castkey = 65008; | |
optional string castvalue = 65009; | |
optional bool stdtime = 65010; | |
optional bool stdduration = 65011; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
syntax = "proto3"; | |
package my_proto; | |
import "google/protobuf/timestamp.proto"; | |
import "google/protobuf/empty.proto"; | |
import "validator.proto"; | |
import "gogo.proto"; | |
service MyService { | |
rpc MyRPC(MyRequest) returns (google.protobuf.Empty) {} | |
} | |
message MyRequest { | |
google.protobuf.Timestamp data_import_date = 1 [ | |
(gogoproto.stdtime) = true | |
]; | |
uint32 my_value = 2 [ | |
(validator.field) = { | |
int_gt: 0, | |
} | |
]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Code generated by protoc-gen-gogo. | |
// source: my.proto | |
// DO NOT EDIT! | |
/* | |
Package my_proto is a generated protocol buffer package. | |
It is generated from these files: | |
my.proto | |
It has these top-level messages: | |
MyRequest | |
*/ | |
package my_proto | |
import fmt "fmt" | |
import github_com_mwitkow_go_proto_validators "github.com/mwitkow/go-proto-validators" | |
import proto "github.com/gogo/protobuf/proto" | |
import math "math" | |
import _ "github.com/gogo/protobuf/types" | |
import _ "github.com/gogo/protobuf/types" | |
import _ "." | |
import _ "." | |
import time "time" | |
// Reference imports to suppress errors if they are not otherwise used. | |
var _ = proto.Marshal | |
var _ = fmt.Errorf | |
var _ = math.Inf | |
func (this *MyRequest) Validate() error { | |
if this.DataImportDate != nil { | |
if err := github_com_mwitkow_go_proto_validators.CallValidatorIfExists(this.DataImportDate); err != nil { | |
return github_com_mwitkow_go_proto_validators.FieldError("DataImportDate", err) | |
} | |
} | |
if !(this.MyValue > 0) { | |
return github_com_mwitkow_go_proto_validators.FieldError("MyValue", fmt.Errorf(`value '%v' must must be greater than '0'`, this.MyValue)) | |
} | |
return nil | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2016 Michal Witkowski. All Rights Reserved. | |
// See LICENSE for licensing terms. | |
// Protocol Buffers extensions for defining auto-generateable validators for messages. | |
// TODO(mwitkow): Add example. | |
syntax = "proto2"; | |
package validator; | |
import "google/protobuf/descriptor.proto"; | |
option go_package = "validator"; | |
// TODO(mwitkow): Email [email protected] to get an extension ID. | |
extend google.protobuf.FieldOptions { | |
optional FieldValidator field = 65020; | |
} | |
message FieldValidator { | |
// Uses a Golang RE2-syntax regex to match the field contents. | |
optional string regex = 1; | |
// Field value of integer strict greater than this value. | |
optional int64 int_gt = 2; | |
// Field value of integer strict smaller than this value. | |
optional int64 int_lt = 3; | |
// Used for nested message types, requires that the message type exists. | |
optional bool msg_exists = 4; | |
// Human error specifies a user-customizable error that is visible to the user. | |
optional string human_error = 5; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment