├── AUTHORS ├── CONTRIBUTORS ├── .gitignore ├── LICENSE ├── protoc-gen-go ├── Makefile ├── link_grpc.go ├── testdata │ ├── imp3.proto │ ├── extension_extra.proto │ ├── multi │ │ ├── multi3.proto │ │ ├── multi2.proto │ │ └── multi1.proto │ ├── imp2.proto │ ├── extension_base.proto │ ├── proto3.proto │ ├── main_test.go │ ├── grpc.proto │ ├── imp.proto │ ├── Makefile │ ├── extension_user.proto │ ├── imp.pb.go.golden │ ├── my_test │ │ └── test.proto │ └── extension_test.go ├── generator │ ├── Makefile │ └── name_test.go ├── descriptor │ └── Makefile ├── plugin │ ├── Makefile │ └── plugin.pb.golden ├── doc.go └── main.go ├── ptypes ├── doc.go ├── regen.sh ├── empty │ ├── empty.proto │ └── empty.pb.go ├── struct │ └── struct.proto ├── duration.go ├── wrappers │ └── wrappers.proto ├── any_test.go ├── duration │ ├── duration.proto │ └── duration.pb.go ├── timestamp │ ├── timestamp.proto │ └── timestamp.pb.go ├── timestamp.go ├── any.go ├── duration_test.go ├── any │ ├── any.proto │ └── any.pb.go └── timestamp_test.go ├── proto ├── Makefile ├── testdata │ ├── Makefile │ └── golden_test.go ├── size2_test.go ├── proto3_proto │ └── proto3.proto ├── message_set_test.go ├── proto3_test.go ├── clone.go ├── size_test.go └── equal_test.go ├── Make.protobuf ├── jsonpb └── jsonpb_test_proto │ ├── Makefile │ ├── more_test_objects.proto │ ├── test_objects.proto │ └── more_test_objects.pb.go ├── Makefile └── README.md /AUTHORS: -------------------------------------------------------------------------------- 1 | # This source code refers to The Go Authors for copyright purposes. 2 | # The master list of authors is in the main Go distribution, 3 | # visible at http://tip.golang.org/AUTHORS. 4 | -------------------------------------------------------------------------------- /CONTRIBUTORS: -------------------------------------------------------------------------------- 1 | # This source code was written by the Go contributors. 2 | # The master list of contributors is in the main Go distribution, 3 | # visible at http://tip.golang.org/CONTRIBUTORS. 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | *.[568ao] 3 | *.ao 4 | *.so 5 | *.pyc 6 | ._* 7 | .nfs.* 8 | [568a].out 9 | *~ 10 | *.orig 11 | core 12 | _obj 13 | _test 14 | _testmain.go 15 | protoc-gen-go/testdata/multi/*.pb.go 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Go support for Protocol Buffers - Google's data interchange format 2 | 3 | Copyright 2010 The Go Authors. All rights reserved. 4 | https://github.com/golang/protobuf 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are 8 | met: 9 | 10 | * Redistributions of source code must retain the above copyright 11 | notice, this list of conditions and the following disclaimer. 12 | * Redistributions in binary form must reproduce the above 13 | copyright notice, this list of conditions and the following disclaimer 14 | in the documentation and/or other materials provided with the 15 | distribution. 16 | * Neither the name of Google Inc. nor the names of its 17 | contributors may be used to endorse or promote products derived from 18 | this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | -------------------------------------------------------------------------------- /protoc-gen-go/Makefile: -------------------------------------------------------------------------------- 1 | # Go support for Protocol Buffers - Google's data interchange format 2 | # 3 | # Copyright 2010 The Go Authors. All rights reserved. 4 | # https://github.com/golang/protobuf 5 | # 6 | # Redistribution and use in source and binary forms, with or without 7 | # modification, are permitted provided that the following conditions are 8 | # met: 9 | # 10 | # * Redistributions of source code must retain the above copyright 11 | # notice, this list of conditions and the following disclaimer. 12 | # * Redistributions in binary form must reproduce the above 13 | # copyright notice, this list of conditions and the following disclaimer 14 | # in the documentation and/or other materials provided with the 15 | # distribution. 16 | # * Neither the name of Google Inc. nor the names of its 17 | # contributors may be used to endorse or promote products derived from 18 | # this software without specific prior written permission. 19 | # 20 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | test: 33 | cd testdata && make test 34 | -------------------------------------------------------------------------------- /protoc-gen-go/link_grpc.go: -------------------------------------------------------------------------------- 1 | // Go support for Protocol Buffers - Google's data interchange format 2 | // 3 | // Copyright 2015 The Go Authors. All rights reserved. 4 | // https://github.com/golang/protobuf 5 | // 6 | // Redistribution and use in source and binary forms, with or without 7 | // modification, are permitted provided that the following conditions are 8 | // met: 9 | // 10 | // * Redistributions of source code must retain the above copyright 11 | // notice, this list of conditions and the following disclaimer. 12 | // * Redistributions in binary form must reproduce the above 13 | // copyright notice, this list of conditions and the following disclaimer 14 | // in the documentation and/or other materials provided with the 15 | // distribution. 16 | // * Neither the name of Google Inc. nor the names of its 17 | // contributors may be used to endorse or promote products derived from 18 | // this software without specific prior written permission. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | package main 33 | 34 | import _ "github.com/golang/protobuf/protoc-gen-go/grpc" 35 | -------------------------------------------------------------------------------- /ptypes/doc.go: -------------------------------------------------------------------------------- 1 | // Go support for Protocol Buffers - Google's data interchange format 2 | // 3 | // Copyright 2016 The Go Authors. All rights reserved. 4 | // https://github.com/golang/protobuf 5 | // 6 | // Redistribution and use in source and binary forms, with or without 7 | // modification, are permitted provided that the following conditions are 8 | // met: 9 | // 10 | // * Redistributions of source code must retain the above copyright 11 | // notice, this list of conditions and the following disclaimer. 12 | // * Redistributions in binary form must reproduce the above 13 | // copyright notice, this list of conditions and the following disclaimer 14 | // in the documentation and/or other materials provided with the 15 | // distribution. 16 | // * Neither the name of Google Inc. nor the names of its 17 | // contributors may be used to endorse or promote products derived from 18 | // this software without specific prior written permission. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | /* 33 | Package ptypes contains code for interacting with well-known types. 34 | */ 35 | package ptypes 36 | -------------------------------------------------------------------------------- /protoc-gen-go/testdata/imp3.proto: -------------------------------------------------------------------------------- 1 | // Go support for Protocol Buffers - Google's data interchange format 2 | // 3 | // Copyright 2012 The Go Authors. All rights reserved. 4 | // https://github.com/golang/protobuf 5 | // 6 | // Redistribution and use in source and binary forms, with or without 7 | // modification, are permitted provided that the following conditions are 8 | // met: 9 | // 10 | // * Redistributions of source code must retain the above copyright 11 | // notice, this list of conditions and the following disclaimer. 12 | // * Redistributions in binary form must reproduce the above 13 | // copyright notice, this list of conditions and the following disclaimer 14 | // in the documentation and/or other materials provided with the 15 | // distribution. 16 | // * Neither the name of Google Inc. nor the names of its 17 | // contributors may be used to endorse or promote products derived from 18 | // this software without specific prior written permission. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | syntax = "proto2"; 33 | 34 | package imp; 35 | 36 | message ForeignImportedMessage { 37 | optional string tuber = 1; 38 | } 39 | -------------------------------------------------------------------------------- /protoc-gen-go/testdata/extension_extra.proto: -------------------------------------------------------------------------------- 1 | // Go support for Protocol Buffers - Google's data interchange format 2 | // 3 | // Copyright 2011 The Go Authors. All rights reserved. 4 | // https://github.com/golang/protobuf 5 | // 6 | // Redistribution and use in source and binary forms, with or without 7 | // modification, are permitted provided that the following conditions are 8 | // met: 9 | // 10 | // * Redistributions of source code must retain the above copyright 11 | // notice, this list of conditions and the following disclaimer. 12 | // * Redistributions in binary form must reproduce the above 13 | // copyright notice, this list of conditions and the following disclaimer 14 | // in the documentation and/or other materials provided with the 15 | // distribution. 16 | // * Neither the name of Google Inc. nor the names of its 17 | // contributors may be used to endorse or promote products derived from 18 | // this software without specific prior written permission. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | syntax = "proto2"; 33 | 34 | package extension_extra; 35 | 36 | message ExtraMessage { 37 | optional int32 width = 1; 38 | } 39 | -------------------------------------------------------------------------------- /protoc-gen-go/generator/Makefile: -------------------------------------------------------------------------------- 1 | # Go support for Protocol Buffers - Google's data interchange format 2 | # 3 | # Copyright 2010 The Go Authors. All rights reserved. 4 | # https://github.com/golang/protobuf 5 | # 6 | # Redistribution and use in source and binary forms, with or without 7 | # modification, are permitted provided that the following conditions are 8 | # met: 9 | # 10 | # * Redistributions of source code must retain the above copyright 11 | # notice, this list of conditions and the following disclaimer. 12 | # * Redistributions in binary form must reproduce the above 13 | # copyright notice, this list of conditions and the following disclaimer 14 | # in the documentation and/or other materials provided with the 15 | # distribution. 16 | # * Neither the name of Google Inc. nor the names of its 17 | # contributors may be used to endorse or promote products derived from 18 | # this software without specific prior written permission. 19 | # 20 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | include $(GOROOT)/src/Make.inc 33 | 34 | TARG=github.com/golang/protobuf/compiler/generator 35 | GOFILES=\ 36 | generator.go\ 37 | 38 | DEPS=../descriptor ../plugin ../../proto 39 | 40 | include $(GOROOT)/src/Make.pkg 41 | -------------------------------------------------------------------------------- /protoc-gen-go/testdata/multi/multi3.proto: -------------------------------------------------------------------------------- 1 | // Go support for Protocol Buffers - Google's data interchange format 2 | // 3 | // Copyright 2010 The Go Authors. All rights reserved. 4 | // https://github.com/golang/protobuf 5 | // 6 | // Redistribution and use in source and binary forms, with or without 7 | // modification, are permitted provided that the following conditions are 8 | // met: 9 | // 10 | // * Redistributions of source code must retain the above copyright 11 | // notice, this list of conditions and the following disclaimer. 12 | // * Redistributions in binary form must reproduce the above 13 | // copyright notice, this list of conditions and the following disclaimer 14 | // in the documentation and/or other materials provided with the 15 | // distribution. 16 | // * Neither the name of Google Inc. nor the names of its 17 | // contributors may be used to endorse or promote products derived from 18 | // this software without specific prior written permission. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | syntax = "proto2"; 33 | 34 | package multitest; 35 | 36 | message Multi3 { 37 | enum HatType { 38 | FEDORA = 1; 39 | FEZ = 2; 40 | }; 41 | optional HatType hat_type = 1; 42 | } 43 | 44 | -------------------------------------------------------------------------------- /protoc-gen-go/testdata/imp2.proto: -------------------------------------------------------------------------------- 1 | // Go support for Protocol Buffers - Google's data interchange format 2 | // 3 | // Copyright 2011 The Go Authors. All rights reserved. 4 | // https://github.com/golang/protobuf 5 | // 6 | // Redistribution and use in source and binary forms, with or without 7 | // modification, are permitted provided that the following conditions are 8 | // met: 9 | // 10 | // * Redistributions of source code must retain the above copyright 11 | // notice, this list of conditions and the following disclaimer. 12 | // * Redistributions in binary form must reproduce the above 13 | // copyright notice, this list of conditions and the following disclaimer 14 | // in the documentation and/or other materials provided with the 15 | // distribution. 16 | // * Neither the name of Google Inc. nor the names of its 17 | // contributors may be used to endorse or promote products derived from 18 | // this software without specific prior written permission. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | syntax = "proto2"; 33 | 34 | package imp; 35 | 36 | message PubliclyImportedMessage { 37 | optional int64 field = 1; 38 | } 39 | 40 | enum PubliclyImportedEnum { 41 | GLASSES = 1; 42 | HAIR = 2; 43 | } 44 | -------------------------------------------------------------------------------- /ptypes/regen.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | # 3 | # This script fetches and rebuilds the "well-known types" protocol buffers. 4 | # To run this you will need protoc and goprotobuf installed; 5 | # see https://github.com/golang/protobuf for instructions. 6 | # You also need Go and Git installed. 7 | 8 | PKG=github.com/golang/protobuf/ptypes 9 | UPSTREAM=https://github.com/google/protobuf 10 | UPSTREAM_SUBDIR=src/google/protobuf 11 | PROTO_FILES=' 12 | any.proto 13 | duration.proto 14 | empty.proto 15 | struct.proto 16 | timestamp.proto 17 | wrappers.proto 18 | ' 19 | 20 | function die() { 21 | echo 1>&2 $* 22 | exit 1 23 | } 24 | 25 | # Sanity check that the right tools are accessible. 26 | for tool in go git protoc protoc-gen-go; do 27 | q=$(which $tool) || die "didn't find $tool" 28 | echo 1>&2 "$tool: $q" 29 | done 30 | 31 | tmpdir=$(mktemp -d -t regen-wkt.XXXXXX) 32 | trap 'rm -rf $tmpdir' EXIT 33 | 34 | echo -n 1>&2 "finding package dir... " 35 | pkgdir=$(go list -f '{{.Dir}}' $PKG) 36 | echo 1>&2 $pkgdir 37 | base=$(echo $pkgdir | sed "s,/$PKG\$,,") 38 | echo 1>&2 "base: $base" 39 | cd $base 40 | 41 | echo 1>&2 "fetching latest protos... " 42 | git clone -q $UPSTREAM $tmpdir 43 | # Pass 1: build mapping from upstream filename to our filename. 44 | declare -A filename_map 45 | for f in $(cd $PKG && find * -name '*.proto'); do 46 | echo -n 1>&2 "looking for latest version of $f... " 47 | up=$(cd $tmpdir/$UPSTREAM_SUBDIR && find * -name $(basename $f) | grep -v /testdata/) 48 | echo 1>&2 $up 49 | if [ $(echo $up | wc -w) != "1" ]; then 50 | die "not exactly one match" 51 | fi 52 | filename_map[$up]=$f 53 | done 54 | # Pass 2: copy files 55 | for up in "${!filename_map[@]}"; do 56 | f=${filename_map[$up]} 57 | shortname=$(basename $f | sed 's,\.proto$,,') 58 | cp $tmpdir/$UPSTREAM_SUBDIR/$up $PKG/$f 59 | done 60 | 61 | # Run protoc once per package. 62 | for dir in $(find $PKG -name '*.proto' | xargs dirname | sort | uniq); do 63 | echo 1>&2 "* $dir" 64 | protoc --go_out=. $dir/*.proto 65 | done 66 | echo 1>&2 "All OK" 67 | -------------------------------------------------------------------------------- /protoc-gen-go/testdata/multi/multi2.proto: -------------------------------------------------------------------------------- 1 | // Go support for Protocol Buffers - Google's data interchange format 2 | // 3 | // Copyright 2010 The Go Authors. All rights reserved. 4 | // https://github.com/golang/protobuf 5 | // 6 | // Redistribution and use in source and binary forms, with or without 7 | // modification, are permitted provided that the following conditions are 8 | // met: 9 | // 10 | // * Redistributions of source code must retain the above copyright 11 | // notice, this list of conditions and the following disclaimer. 12 | // * Redistributions in binary form must reproduce the above 13 | // copyright notice, this list of conditions and the following disclaimer 14 | // in the documentation and/or other materials provided with the 15 | // distribution. 16 | // * Neither the name of Google Inc. nor the names of its 17 | // contributors may be used to endorse or promote products derived from 18 | // this software without specific prior written permission. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | syntax = "proto2"; 33 | 34 | package multitest; 35 | 36 | message Multi2 { 37 | required int32 required_value = 1; 38 | 39 | enum Color { 40 | BLUE = 1; 41 | GREEN = 2; 42 | RED = 3; 43 | }; 44 | optional Color color = 2; 45 | } 46 | 47 | -------------------------------------------------------------------------------- /protoc-gen-go/testdata/multi/multi1.proto: -------------------------------------------------------------------------------- 1 | // Go support for Protocol Buffers - Google's data interchange format 2 | // 3 | // Copyright 2010 The Go Authors. All rights reserved. 4 | // https://github.com/golang/protobuf 5 | // 6 | // Redistribution and use in source and binary forms, with or without 7 | // modification, are permitted provided that the following conditions are 8 | // met: 9 | // 10 | // * Redistributions of source code must retain the above copyright 11 | // notice, this list of conditions and the following disclaimer. 12 | // * Redistributions in binary form must reproduce the above 13 | // copyright notice, this list of conditions and the following disclaimer 14 | // in the documentation and/or other materials provided with the 15 | // distribution. 16 | // * Neither the name of Google Inc. nor the names of its 17 | // contributors may be used to endorse or promote products derived from 18 | // this software without specific prior written permission. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | syntax = "proto2"; 33 | 34 | import "multi/multi2.proto"; 35 | import "multi/multi3.proto"; 36 | 37 | package multitest; 38 | 39 | message Multi1 { 40 | required Multi2 multi2 = 1; 41 | optional Multi2.Color color = 2; 42 | optional Multi3.HatType hat_type = 3; 43 | } 44 | 45 | -------------------------------------------------------------------------------- /proto/Makefile: -------------------------------------------------------------------------------- 1 | # Go support for Protocol Buffers - Google's data interchange format 2 | # 3 | # Copyright 2010 The Go Authors. All rights reserved. 4 | # https://github.com/golang/protobuf 5 | # 6 | # Redistribution and use in source and binary forms, with or without 7 | # modification, are permitted provided that the following conditions are 8 | # met: 9 | # 10 | # * Redistributions of source code must retain the above copyright 11 | # notice, this list of conditions and the following disclaimer. 12 | # * Redistributions in binary form must reproduce the above 13 | # copyright notice, this list of conditions and the following disclaimer 14 | # in the documentation and/or other materials provided with the 15 | # distribution. 16 | # * Neither the name of Google Inc. nor the names of its 17 | # contributors may be used to endorse or promote products derived from 18 | # this software without specific prior written permission. 19 | # 20 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | install: 33 | go install 34 | 35 | test: install generate-test-pbs 36 | go test 37 | 38 | 39 | generate-test-pbs: 40 | make install 41 | make -C testdata 42 | protoc --go_out=Mtestdata/test.proto=github.com/golang/protobuf/proto/testdata,Mgoogle/protobuf/any.proto=github.com/golang/protobuf/ptypes/any:. proto3_proto/proto3.proto 43 | make 44 | -------------------------------------------------------------------------------- /Make.protobuf: -------------------------------------------------------------------------------- 1 | # Go support for Protocol Buffers - Google's data interchange format 2 | # 3 | # Copyright 2010 The Go Authors. All rights reserved. 4 | # https://github.com/golang/protobuf 5 | # 6 | # Redistribution and use in source and binary forms, with or without 7 | # modification, are permitted provided that the following conditions are 8 | # met: 9 | # 10 | # * Redistributions of source code must retain the above copyright 11 | # notice, this list of conditions and the following disclaimer. 12 | # * Redistributions in binary form must reproduce the above 13 | # copyright notice, this list of conditions and the following disclaimer 14 | # in the documentation and/or other materials provided with the 15 | # distribution. 16 | # * Neither the name of Google Inc. nor the names of its 17 | # contributors may be used to endorse or promote products derived from 18 | # this software without specific prior written permission. 19 | # 20 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | # Includable Makefile to add a rule for generating .pb.go files from .proto files 33 | # (Google protocol buffer descriptions). 34 | # Typical use if myproto.proto is a file in package mypackage in this directory: 35 | # 36 | # include $(GOROOT)/src/pkg/github.com/golang/protobuf/Make.protobuf 37 | 38 | %.pb.go: %.proto 39 | protoc --go_out=. $< 40 | 41 | -------------------------------------------------------------------------------- /proto/testdata/Makefile: -------------------------------------------------------------------------------- 1 | # Go support for Protocol Buffers - Google's data interchange format 2 | # 3 | # Copyright 2010 The Go Authors. All rights reserved. 4 | # https://github.com/golang/protobuf 5 | # 6 | # Redistribution and use in source and binary forms, with or without 7 | # modification, are permitted provided that the following conditions are 8 | # met: 9 | # 10 | # * Redistributions of source code must retain the above copyright 11 | # notice, this list of conditions and the following disclaimer. 12 | # * Redistributions in binary form must reproduce the above 13 | # copyright notice, this list of conditions and the following disclaimer 14 | # in the documentation and/or other materials provided with the 15 | # distribution. 16 | # * Neither the name of Google Inc. nor the names of its 17 | # contributors may be used to endorse or promote products derived from 18 | # this software without specific prior written permission. 19 | # 20 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | 33 | include ../../Make.protobuf 34 | 35 | all: regenerate 36 | 37 | regenerate: 38 | rm -f test.pb.go 39 | make test.pb.go 40 | 41 | # The following rules are just aids to development. Not needed for typical testing. 42 | 43 | diff: regenerate 44 | git diff test.pb.go 45 | 46 | restore: 47 | cp test.pb.go.golden test.pb.go 48 | 49 | preserve: 50 | cp test.pb.go test.pb.go.golden 51 | -------------------------------------------------------------------------------- /jsonpb/jsonpb_test_proto/Makefile: -------------------------------------------------------------------------------- 1 | # Go support for Protocol Buffers - Google's data interchange format 2 | # 3 | # Copyright 2015 The Go Authors. All rights reserved. 4 | # https://github.com/golang/protobuf 5 | # 6 | # Redistribution and use in source and binary forms, with or without 7 | # modification, are permitted provided that the following conditions are 8 | # met: 9 | # 10 | # * Redistributions of source code must retain the above copyright 11 | # notice, this list of conditions and the following disclaimer. 12 | # * Redistributions in binary form must reproduce the above 13 | # copyright notice, this list of conditions and the following disclaimer 14 | # in the documentation and/or other materials provided with the 15 | # distribution. 16 | # * Neither the name of Google Inc. nor the names of its 17 | # contributors may be used to endorse or promote products derived from 18 | # this software without specific prior written permission. 19 | # 20 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | regenerate: 33 | protoc --go_out=Mgoogle/protobuf/any.proto=github.com/golang/protobuf/ptypes/any,Mgoogle/protobuf/duration.proto=github.com/golang/protobuf/ptypes/duration,Mgoogle/protobuf/struct.proto=github.com/golang/protobuf/ptypes/struct,Mgoogle/protobuf/timestamp.proto=github.com/golang/protobuf/ptypes/timestamp,Mgoogle/protobuf/wrappers.proto=github.com/golang/protobuf/ptypes/wrappers:. *.proto 34 | -------------------------------------------------------------------------------- /protoc-gen-go/testdata/extension_base.proto: -------------------------------------------------------------------------------- 1 | // Go support for Protocol Buffers - Google's data interchange format 2 | // 3 | // Copyright 2010 The Go Authors. All rights reserved. 4 | // https://github.com/golang/protobuf 5 | // 6 | // Redistribution and use in source and binary forms, with or without 7 | // modification, are permitted provided that the following conditions are 8 | // met: 9 | // 10 | // * Redistributions of source code must retain the above copyright 11 | // notice, this list of conditions and the following disclaimer. 12 | // * Redistributions in binary form must reproduce the above 13 | // copyright notice, this list of conditions and the following disclaimer 14 | // in the documentation and/or other materials provided with the 15 | // distribution. 16 | // * Neither the name of Google Inc. nor the names of its 17 | // contributors may be used to endorse or promote products derived from 18 | // this software without specific prior written permission. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | syntax = "proto2"; 33 | 34 | package extension_base; 35 | 36 | message BaseMessage { 37 | optional int32 height = 1; 38 | extensions 4 to 9; 39 | extensions 16 to max; 40 | } 41 | 42 | // Another message that may be extended, using message_set_wire_format. 43 | message OldStyleMessage { 44 | option message_set_wire_format = true; 45 | extensions 100 to max; 46 | } 47 | -------------------------------------------------------------------------------- /protoc-gen-go/testdata/proto3.proto: -------------------------------------------------------------------------------- 1 | // Go support for Protocol Buffers - Google's data interchange format 2 | // 3 | // Copyright 2014 The Go Authors. All rights reserved. 4 | // https://github.com/golang/protobuf 5 | // 6 | // Redistribution and use in source and binary forms, with or without 7 | // modification, are permitted provided that the following conditions are 8 | // met: 9 | // 10 | // * Redistributions of source code must retain the above copyright 11 | // notice, this list of conditions and the following disclaimer. 12 | // * Redistributions in binary form must reproduce the above 13 | // copyright notice, this list of conditions and the following disclaimer 14 | // in the documentation and/or other materials provided with the 15 | // distribution. 16 | // * Neither the name of Google Inc. nor the names of its 17 | // contributors may be used to endorse or promote products derived from 18 | // this software without specific prior written permission. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | syntax = "proto3"; 33 | 34 | package proto3; 35 | 36 | message Request { 37 | enum Flavour { 38 | SWEET = 0; 39 | SOUR = 1; 40 | UMAMI = 2; 41 | GOPHERLICIOUS = 3; 42 | } 43 | string name = 1; 44 | repeated int64 key = 2; 45 | Flavour taste = 3; 46 | Book book = 4; 47 | } 48 | 49 | message Book { 50 | string title = 1; 51 | bytes raw_data = 2; 52 | } 53 | -------------------------------------------------------------------------------- /protoc-gen-go/testdata/main_test.go: -------------------------------------------------------------------------------- 1 | // Go support for Protocol Buffers - Google's data interchange format 2 | // 3 | // Copyright 2010 The Go Authors. All rights reserved. 4 | // https://github.com/golang/protobuf 5 | // 6 | // Redistribution and use in source and binary forms, with or without 7 | // modification, are permitted provided that the following conditions are 8 | // met: 9 | // 10 | // * Redistributions of source code must retain the above copyright 11 | // notice, this list of conditions and the following disclaimer. 12 | // * Redistributions in binary form must reproduce the above 13 | // copyright notice, this list of conditions and the following disclaimer 14 | // in the documentation and/or other materials provided with the 15 | // distribution. 16 | // * Neither the name of Google Inc. nor the names of its 17 | // contributors may be used to endorse or promote products derived from 18 | // this software without specific prior written permission. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | // A simple binary to link together the protocol buffers in this test. 33 | 34 | package testdata 35 | 36 | import ( 37 | "testing" 38 | 39 | mytestpb "github.com/golang/protobuf/protoc-gen-go/testdata/my_test" 40 | multipb "github.com/golang/protobuf/protoc-gen-go/testdata/multi" 41 | ) 42 | 43 | func TestLink(t *testing.T) { 44 | _ = &multipb.Multi1{} 45 | _ = &mytestpb.Request{} 46 | } 47 | -------------------------------------------------------------------------------- /jsonpb/jsonpb_test_proto/more_test_objects.proto: -------------------------------------------------------------------------------- 1 | // Go support for Protocol Buffers - Google's data interchange format 2 | // 3 | // Copyright 2015 The Go Authors. All rights reserved. 4 | // https://github.com/golang/protobuf 5 | // 6 | // Redistribution and use in source and binary forms, with or without 7 | // modification, are permitted provided that the following conditions are 8 | // met: 9 | // 10 | // * Redistributions of source code must retain the above copyright 11 | // notice, this list of conditions and the following disclaimer. 12 | // * Redistributions in binary form must reproduce the above 13 | // copyright notice, this list of conditions and the following disclaimer 14 | // in the documentation and/or other materials provided with the 15 | // distribution. 16 | // * Neither the name of Google Inc. nor the names of its 17 | // contributors may be used to endorse or promote products derived from 18 | // this software without specific prior written permission. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | syntax = "proto3"; 33 | 34 | package jsonpb; 35 | 36 | message Simple3 { 37 | double dub = 1; 38 | } 39 | 40 | enum Numeral { 41 | UNKNOWN = 0; 42 | ARABIC = 1; 43 | ROMAN = 2; 44 | } 45 | 46 | message Mappy { 47 | map nummy = 1; 48 | map strry = 2; 49 | map objjy = 3; 50 | map buggy = 4; 51 | map booly = 5; 52 | map enumy = 6; 53 | } 54 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # Go support for Protocol Buffers - Google's data interchange format 2 | # 3 | # Copyright 2010 The Go Authors. All rights reserved. 4 | # https://github.com/golang/protobuf 5 | # 6 | # Redistribution and use in source and binary forms, with or without 7 | # modification, are permitted provided that the following conditions are 8 | # met: 9 | # 10 | # * Redistributions of source code must retain the above copyright 11 | # notice, this list of conditions and the following disclaimer. 12 | # * Redistributions in binary form must reproduce the above 13 | # copyright notice, this list of conditions and the following disclaimer 14 | # in the documentation and/or other materials provided with the 15 | # distribution. 16 | # * Neither the name of Google Inc. nor the names of its 17 | # contributors may be used to endorse or promote products derived from 18 | # this software without specific prior written permission. 19 | # 20 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | 33 | all: install 34 | 35 | install: 36 | go install ./proto ./jsonpb ./ptypes 37 | go install ./protoc-gen-go 38 | 39 | test: 40 | go test ./proto ./jsonpb ./ptypes 41 | make -C protoc-gen-go/testdata test 42 | 43 | clean: 44 | go clean ./... 45 | 46 | nuke: 47 | go clean -i ./... 48 | 49 | regenerate: 50 | make -C protoc-gen-go/descriptor regenerate 51 | make -C protoc-gen-go/plugin regenerate 52 | make -C protoc-gen-go/testdata regenerate 53 | make -C proto/testdata regenerate 54 | make -C jsonpb/jsonpb_test_proto regenerate 55 | -------------------------------------------------------------------------------- /protoc-gen-go/descriptor/Makefile: -------------------------------------------------------------------------------- 1 | # Go support for Protocol Buffers - Google's data interchange format 2 | # 3 | # Copyright 2010 The Go Authors. All rights reserved. 4 | # https://github.com/golang/protobuf 5 | # 6 | # Redistribution and use in source and binary forms, with or without 7 | # modification, are permitted provided that the following conditions are 8 | # met: 9 | # 10 | # * Redistributions of source code must retain the above copyright 11 | # notice, this list of conditions and the following disclaimer. 12 | # * Redistributions in binary form must reproduce the above 13 | # copyright notice, this list of conditions and the following disclaimer 14 | # in the documentation and/or other materials provided with the 15 | # distribution. 16 | # * Neither the name of Google Inc. nor the names of its 17 | # contributors may be used to endorse or promote products derived from 18 | # this software without specific prior written permission. 19 | # 20 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | # Not stored here, but descriptor.proto is in https://github.com/google/protobuf/ 33 | # at src/google/protobuf/descriptor.proto 34 | regenerate: 35 | echo WARNING! THIS RULE IS PROBABLY NOT RIGHT FOR YOUR INSTALLATION 36 | protoc --go_out=. -I$(HOME)/src/protobuf/src $(HOME)/src/protobuf/src/google/protobuf/descriptor.proto && \ 37 | sed 's,^package google_protobuf,package descriptor,' google/protobuf/descriptor.pb.go > \ 38 | $(GOPATH)/src/github.com/golang/protobuf/protoc-gen-go/descriptor/descriptor.pb.go && \ 39 | rm -f google/protobuf/descriptor.pb.go 40 | -------------------------------------------------------------------------------- /protoc-gen-go/testdata/grpc.proto: -------------------------------------------------------------------------------- 1 | // Go support for Protocol Buffers - Google's data interchange format 2 | // 3 | // Copyright 2015 The Go Authors. All rights reserved. 4 | // https://github.com/golang/protobuf 5 | // 6 | // Redistribution and use in source and binary forms, with or without 7 | // modification, are permitted provided that the following conditions are 8 | // met: 9 | // 10 | // * Redistributions of source code must retain the above copyright 11 | // notice, this list of conditions and the following disclaimer. 12 | // * Redistributions in binary form must reproduce the above 13 | // copyright notice, this list of conditions and the following disclaimer 14 | // in the documentation and/or other materials provided with the 15 | // distribution. 16 | // * Neither the name of Google Inc. nor the names of its 17 | // contributors may be used to endorse or promote products derived from 18 | // this software without specific prior written permission. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | syntax = "proto3"; 33 | 34 | package grpc.testing; 35 | 36 | message SimpleRequest { 37 | } 38 | 39 | message SimpleResponse { 40 | } 41 | 42 | message StreamMsg { 43 | } 44 | 45 | message StreamMsg2 { 46 | } 47 | 48 | service Test { 49 | rpc UnaryCall(SimpleRequest) returns (SimpleResponse); 50 | 51 | // This RPC streams from the server only. 52 | rpc Downstream(SimpleRequest) returns (stream StreamMsg); 53 | 54 | // This RPC streams from the client. 55 | rpc Upstream(stream StreamMsg) returns (SimpleResponse); 56 | 57 | // This one streams in both directions. 58 | rpc Bidi(stream StreamMsg) returns (stream StreamMsg2); 59 | } 60 | -------------------------------------------------------------------------------- /protoc-gen-go/plugin/Makefile: -------------------------------------------------------------------------------- 1 | # Go support for Protocol Buffers - Google's data interchange format 2 | # 3 | # Copyright 2010 The Go Authors. All rights reserved. 4 | # https://github.com/golang/protobuf 5 | # 6 | # Redistribution and use in source and binary forms, with or without 7 | # modification, are permitted provided that the following conditions are 8 | # met: 9 | # 10 | # * Redistributions of source code must retain the above copyright 11 | # notice, this list of conditions and the following disclaimer. 12 | # * Redistributions in binary form must reproduce the above 13 | # copyright notice, this list of conditions and the following disclaimer 14 | # in the documentation and/or other materials provided with the 15 | # distribution. 16 | # * Neither the name of Google Inc. nor the names of its 17 | # contributors may be used to endorse or promote products derived from 18 | # this software without specific prior written permission. 19 | # 20 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | # Not stored here, but plugin.proto is in https://github.com/google/protobuf/ 33 | # at src/google/protobuf/compiler/plugin.proto 34 | # Also we need to fix an import. 35 | regenerate: 36 | echo WARNING! THIS RULE IS PROBABLY NOT RIGHT FOR YOUR INSTALLATION 37 | protoc --go_out=Mgoogle/protobuf/descriptor.proto=github.com/golang/protobuf/protoc-gen-go/descriptor:. \ 38 | -I$(HOME)/src/protobuf/src $(HOME)/src/protobuf/src/google/protobuf/compiler/plugin.proto && \ 39 | mv google/protobuf/compiler/plugin.pb.go $(GOPATH)/src/github.com/golang/protobuf/protoc-gen-go/plugin 40 | 41 | restore: 42 | cp plugin.pb.golden plugin.pb.go 43 | 44 | preserve: 45 | cp plugin.pb.go plugin.pb.golden 46 | -------------------------------------------------------------------------------- /proto/size2_test.go: -------------------------------------------------------------------------------- 1 | // Go support for Protocol Buffers - Google's data interchange format 2 | // 3 | // Copyright 2012 The Go Authors. All rights reserved. 4 | // https://github.com/golang/protobuf 5 | // 6 | // Redistribution and use in source and binary forms, with or without 7 | // modification, are permitted provided that the following conditions are 8 | // met: 9 | // 10 | // * Redistributions of source code must retain the above copyright 11 | // notice, this list of conditions and the following disclaimer. 12 | // * Redistributions in binary form must reproduce the above 13 | // copyright notice, this list of conditions and the following disclaimer 14 | // in the documentation and/or other materials provided with the 15 | // distribution. 16 | // * Neither the name of Google Inc. nor the names of its 17 | // contributors may be used to endorse or promote products derived from 18 | // this software without specific prior written permission. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | package proto 33 | 34 | import ( 35 | "testing" 36 | ) 37 | 38 | // This is a separate file and package from size_test.go because that one uses 39 | // generated messages and thus may not be in package proto without having a circular 40 | // dependency, whereas this file tests unexported details of size.go. 41 | 42 | func TestVarintSize(t *testing.T) { 43 | // Check the edge cases carefully. 44 | testCases := []struct { 45 | n uint64 46 | size int 47 | }{ 48 | {0, 1}, 49 | {1, 1}, 50 | {127, 1}, 51 | {128, 2}, 52 | {16383, 2}, 53 | {16384, 3}, 54 | {1<<63 - 1, 9}, 55 | {1 << 63, 10}, 56 | } 57 | for _, tc := range testCases { 58 | size := sizeVarint(tc.n) 59 | if size != tc.size { 60 | t.Errorf("sizeVarint(%d) = %d, want %d", tc.n, size, tc.size) 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /protoc-gen-go/doc.go: -------------------------------------------------------------------------------- 1 | // Go support for Protocol Buffers - Google's data interchange format 2 | // 3 | // Copyright 2010 The Go Authors. All rights reserved. 4 | // https://github.com/golang/protobuf 5 | // 6 | // Redistribution and use in source and binary forms, with or without 7 | // modification, are permitted provided that the following conditions are 8 | // met: 9 | // 10 | // * Redistributions of source code must retain the above copyright 11 | // notice, this list of conditions and the following disclaimer. 12 | // * Redistributions in binary form must reproduce the above 13 | // copyright notice, this list of conditions and the following disclaimer 14 | // in the documentation and/or other materials provided with the 15 | // distribution. 16 | // * Neither the name of Google Inc. nor the names of its 17 | // contributors may be used to endorse or promote products derived from 18 | // this software without specific prior written permission. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | /* 33 | A plugin for the Google protocol buffer compiler to generate Go code. 34 | Run it by building this program and putting it in your path with the name 35 | protoc-gen-go 36 | That word 'go' at the end becomes part of the option string set for the 37 | protocol compiler, so once the protocol compiler (protoc) is installed 38 | you can run 39 | protoc --go_out=output_directory input_directory/file.proto 40 | to generate Go bindings for the protocol defined by file.proto. 41 | With that input, the output will be written to 42 | output_directory/file.pb.go 43 | 44 | The generated code is documented in the package comment for 45 | the library. 46 | 47 | See the README and documentation for protocol buffers to learn more: 48 | https://developers.google.com/protocol-buffers/ 49 | 50 | */ 51 | package documentation 52 | -------------------------------------------------------------------------------- /ptypes/empty/empty.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | syntax = "proto3"; 32 | 33 | package google.protobuf; 34 | 35 | option csharp_namespace = "Google.Protobuf.WellKnownTypes"; 36 | option go_package = "github.com/golang/protobuf/ptypes/empty"; 37 | option java_package = "com.google.protobuf"; 38 | option java_outer_classname = "EmptyProto"; 39 | option java_multiple_files = true; 40 | option java_generate_equals_and_hash = true; 41 | option objc_class_prefix = "GPB"; 42 | option cc_enable_arenas = true; 43 | 44 | // A generic empty message that you can re-use to avoid defining duplicated 45 | // empty messages in your APIs. A typical example is to use it as the request 46 | // or the response type of an API method. For instance: 47 | // 48 | // service Foo { 49 | // rpc Bar(google.protobuf.Empty) returns (google.protobuf.Empty); 50 | // } 51 | // 52 | // The JSON representation for `Empty` is empty JSON object `{}`. 53 | message Empty {} 54 | -------------------------------------------------------------------------------- /protoc-gen-go/testdata/imp.proto: -------------------------------------------------------------------------------- 1 | // Go support for Protocol Buffers - Google's data interchange format 2 | // 3 | // Copyright 2010 The Go Authors. All rights reserved. 4 | // https://github.com/golang/protobuf 5 | // 6 | // Redistribution and use in source and binary forms, with or without 7 | // modification, are permitted provided that the following conditions are 8 | // met: 9 | // 10 | // * Redistributions of source code must retain the above copyright 11 | // notice, this list of conditions and the following disclaimer. 12 | // * Redistributions in binary form must reproduce the above 13 | // copyright notice, this list of conditions and the following disclaimer 14 | // in the documentation and/or other materials provided with the 15 | // distribution. 16 | // * Neither the name of Google Inc. nor the names of its 17 | // contributors may be used to endorse or promote products derived from 18 | // this software without specific prior written permission. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | syntax = "proto2"; 33 | 34 | package imp; 35 | 36 | import "imp2.proto"; 37 | import "imp3.proto"; 38 | 39 | message ImportedMessage { 40 | required int64 field = 1; 41 | 42 | // The forwarded getters for these fields are fiddly to get right. 43 | optional ImportedMessage2 local_msg = 2; 44 | optional ForeignImportedMessage foreign_msg = 3; // in imp3.proto 45 | optional Owner enum_field = 4; 46 | oneof union { 47 | int32 state = 9; 48 | } 49 | 50 | repeated string name = 5; 51 | repeated Owner boss = 6; 52 | repeated ImportedMessage2 memo = 7; 53 | 54 | map msg_map = 8; 55 | 56 | enum Owner { 57 | DAVE = 1; 58 | MIKE = 2; 59 | } 60 | 61 | extensions 90 to 100; 62 | } 63 | 64 | message ImportedMessage2 { 65 | } 66 | 67 | message ImportedExtendable { 68 | option message_set_wire_format = true; 69 | extensions 100 to max; 70 | } 71 | -------------------------------------------------------------------------------- /proto/proto3_proto/proto3.proto: -------------------------------------------------------------------------------- 1 | // Go support for Protocol Buffers - Google's data interchange format 2 | // 3 | // Copyright 2014 The Go Authors. All rights reserved. 4 | // https://github.com/golang/protobuf 5 | // 6 | // Redistribution and use in source and binary forms, with or without 7 | // modification, are permitted provided that the following conditions are 8 | // met: 9 | // 10 | // * Redistributions of source code must retain the above copyright 11 | // notice, this list of conditions and the following disclaimer. 12 | // * Redistributions in binary form must reproduce the above 13 | // copyright notice, this list of conditions and the following disclaimer 14 | // in the documentation and/or other materials provided with the 15 | // distribution. 16 | // * Neither the name of Google Inc. nor the names of its 17 | // contributors may be used to endorse or promote products derived from 18 | // this software without specific prior written permission. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | syntax = "proto3"; 33 | 34 | import "google/protobuf/any.proto"; 35 | import "testdata/test.proto"; 36 | 37 | package proto3_proto; 38 | 39 | message Message { 40 | enum Humour { 41 | UNKNOWN = 0; 42 | PUNS = 1; 43 | SLAPSTICK = 2; 44 | BILL_BAILEY = 3; 45 | } 46 | 47 | string name = 1; 48 | Humour hilarity = 2; 49 | uint32 height_in_cm = 3; 50 | bytes data = 4; 51 | int64 result_count = 7; 52 | bool true_scotsman = 8; 53 | float score = 9; 54 | 55 | repeated uint64 key = 5; 56 | Nested nested = 6; 57 | repeated Humour r_funny = 16; 58 | 59 | map terrain = 10; 60 | testdata.SubDefaults proto2_field = 11; 61 | map proto2_value = 13; 62 | 63 | google.protobuf.Any anything = 14; 64 | repeated google.protobuf.Any many_things = 15; 65 | } 66 | 67 | message Nested { 68 | string bunny = 1; 69 | bool cute = 2; 70 | } 71 | 72 | message MessageWithMap { 73 | map byte_mapping = 1; 74 | } 75 | -------------------------------------------------------------------------------- /proto/message_set_test.go: -------------------------------------------------------------------------------- 1 | // Go support for Protocol Buffers - Google's data interchange format 2 | // 3 | // Copyright 2014 The Go Authors. All rights reserved. 4 | // https://github.com/golang/protobuf 5 | // 6 | // Redistribution and use in source and binary forms, with or without 7 | // modification, are permitted provided that the following conditions are 8 | // met: 9 | // 10 | // * Redistributions of source code must retain the above copyright 11 | // notice, this list of conditions and the following disclaimer. 12 | // * Redistributions in binary form must reproduce the above 13 | // copyright notice, this list of conditions and the following disclaimer 14 | // in the documentation and/or other materials provided with the 15 | // distribution. 16 | // * Neither the name of Google Inc. nor the names of its 17 | // contributors may be used to endorse or promote products derived from 18 | // this software without specific prior written permission. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | package proto 33 | 34 | import ( 35 | "bytes" 36 | "testing" 37 | ) 38 | 39 | func TestUnmarshalMessageSetWithDuplicate(t *testing.T) { 40 | // Check that a repeated message set entry will be concatenated. 41 | in := &messageSet{ 42 | Item: []*_MessageSet_Item{ 43 | {TypeId: Int32(12345), Message: []byte("hoo")}, 44 | {TypeId: Int32(12345), Message: []byte("hah")}, 45 | }, 46 | } 47 | b, err := Marshal(in) 48 | if err != nil { 49 | t.Fatalf("Marshal: %v", err) 50 | } 51 | t.Logf("Marshaled bytes: %q", b) 52 | 53 | var extensions XXX_InternalExtensions 54 | if err := UnmarshalMessageSet(b, &extensions); err != nil { 55 | t.Fatalf("UnmarshalMessageSet: %v", err) 56 | } 57 | ext, ok := extensions.p.extensionMap[12345] 58 | if !ok { 59 | t.Fatalf("Didn't retrieve extension 12345; map is %v", extensions.p.extensionMap) 60 | } 61 | // Skip wire type/field number and length varints. 62 | got := skipVarint(skipVarint(ext.enc)) 63 | if want := []byte("hoohah"); !bytes.Equal(got, want) { 64 | t.Errorf("Combined extension is %q, want %q", got, want) 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /protoc-gen-go/testdata/Makefile: -------------------------------------------------------------------------------- 1 | # Go support for Protocol Buffers - Google's data interchange format 2 | # 3 | # Copyright 2010 The Go Authors. All rights reserved. 4 | # https://github.com/golang/protobuf 5 | # 6 | # Redistribution and use in source and binary forms, with or without 7 | # modification, are permitted provided that the following conditions are 8 | # met: 9 | # 10 | # * Redistributions of source code must retain the above copyright 11 | # notice, this list of conditions and the following disclaimer. 12 | # * Redistributions in binary form must reproduce the above 13 | # copyright notice, this list of conditions and the following disclaimer 14 | # in the documentation and/or other materials provided with the 15 | # distribution. 16 | # * Neither the name of Google Inc. nor the names of its 17 | # contributors may be used to endorse or promote products derived from 18 | # this software without specific prior written permission. 19 | # 20 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | all: 33 | @echo run make test 34 | 35 | include ../../Make.protobuf 36 | 37 | test: golden testbuild 38 | 39 | #test: golden testbuild extension_test 40 | # ./extension_test 41 | # @echo PASS 42 | 43 | my_test/test.pb.go: my_test/test.proto 44 | protoc --go_out=Mmulti/multi1.proto=github.com/golang/protobuf/protoc-gen-go/testdata/multi:. $< 45 | 46 | golden: 47 | make -B my_test/test.pb.go 48 | sed -i '/return.*fileDescriptor/d' my_test/test.pb.go 49 | sed -i '/^var fileDescriptor/,/^}/d' my_test/test.pb.go 50 | gofmt -w my_test/test.pb.go 51 | diff -w my_test/test.pb.go my_test/test.pb.go.golden 52 | 53 | nuke: clean 54 | 55 | testbuild: regenerate 56 | go test 57 | 58 | regenerate: 59 | # Invoke protoc once to generate three independent .pb.go files in the same package. 60 | protoc --go_out=. multi/multi1.proto multi/multi2.proto multi/multi3.proto 61 | 62 | #extension_test: extension_test.$O 63 | # $(LD) -L. -o $@ $< 64 | 65 | #multi.a: multi3.pb.$O multi2.pb.$O multi1.pb.$O 66 | # rm -f multi.a 67 | # $(QUOTED_GOBIN)/gopack grc $@ $< 68 | 69 | #test.pb.go: imp.pb.go 70 | #multi1.pb.go: multi2.pb.go multi3.pb.go 71 | #main.$O: imp.pb.$O test.pb.$O multi.a 72 | #extension_test.$O: extension_base.pb.$O extension_extra.pb.$O extension_user.pb.$O 73 | -------------------------------------------------------------------------------- /ptypes/empty/empty.pb.go: -------------------------------------------------------------------------------- 1 | // Code generated by protoc-gen-go. 2 | // source: github.com/golang/protobuf/ptypes/empty/empty.proto 3 | // DO NOT EDIT! 4 | 5 | /* 6 | Package empty is a generated protocol buffer package. 7 | 8 | It is generated from these files: 9 | github.com/golang/protobuf/ptypes/empty/empty.proto 10 | 11 | It has these top-level messages: 12 | Empty 13 | */ 14 | package empty 15 | 16 | import proto "github.com/golang/protobuf/proto" 17 | import fmt "fmt" 18 | import math "math" 19 | 20 | // Reference imports to suppress errors if they are not otherwise used. 21 | var _ = proto.Marshal 22 | var _ = fmt.Errorf 23 | var _ = math.Inf 24 | 25 | // This is a compile-time assertion to ensure that this generated file 26 | // is compatible with the proto package it is being compiled against. 27 | // A compilation error at this line likely means your copy of the 28 | // proto package needs to be updated. 29 | const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package 30 | 31 | // A generic empty message that you can re-use to avoid defining duplicated 32 | // empty messages in your APIs. A typical example is to use it as the request 33 | // or the response type of an API method. For instance: 34 | // 35 | // service Foo { 36 | // rpc Bar(google.protobuf.Empty) returns (google.protobuf.Empty); 37 | // } 38 | // 39 | // The JSON representation for `Empty` is empty JSON object `{}`. 40 | type Empty struct { 41 | } 42 | 43 | func (m *Empty) Reset() { *m = Empty{} } 44 | func (m *Empty) String() string { return proto.CompactTextString(m) } 45 | func (*Empty) ProtoMessage() {} 46 | func (*Empty) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} } 47 | func (*Empty) XXX_WellKnownType() string { return "Empty" } 48 | 49 | func init() { 50 | proto.RegisterType((*Empty)(nil), "google.protobuf.Empty") 51 | } 52 | 53 | func init() { 54 | proto.RegisterFile("github.com/golang/protobuf/ptypes/empty/empty.proto", fileDescriptor0) 55 | } 56 | 57 | var fileDescriptor0 = []byte{ 58 | // 148 bytes of a gzipped FileDescriptorProto 59 | 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0x32, 0x4e, 0xcf, 0x2c, 0xc9, 60 | 0x28, 0x4d, 0xd2, 0x4b, 0xce, 0xcf, 0xd5, 0x4f, 0xcf, 0xcf, 0x49, 0xcc, 0x4b, 0xd7, 0x2f, 0x28, 61 | 0xca, 0x2f, 0xc9, 0x4f, 0x2a, 0x4d, 0xd3, 0x2f, 0x28, 0xa9, 0x2c, 0x48, 0x2d, 0xd6, 0x4f, 0xcd, 62 | 0x05, 0x32, 0x20, 0xa4, 0x1e, 0x58, 0x4e, 0x88, 0x3f, 0x3d, 0x3f, 0x3f, 0x3d, 0x27, 0x55, 0x0f, 63 | 0xa6, 0x52, 0x89, 0x9d, 0x8b, 0xd5, 0x15, 0x24, 0xef, 0x54, 0xc9, 0x25, 0x0c, 0x34, 0x49, 0x0f, 64 | 0x4d, 0xde, 0x89, 0x0b, 0x2c, 0x1b, 0x00, 0xe2, 0x06, 0x30, 0x46, 0xa9, 0x13, 0x69, 0xe7, 0x02, 65 | 0x46, 0xc6, 0x1f, 0x8c, 0x8c, 0x8b, 0x98, 0x98, 0xdd, 0x03, 0x9c, 0x56, 0x31, 0xc9, 0xb9, 0x43, 66 | 0x0c, 0x0d, 0x80, 0x2a, 0xd5, 0x0b, 0x4f, 0xcd, 0xc9, 0xf1, 0xce, 0xcb, 0x2f, 0xcf, 0x0b, 0x01, 67 | 0x69, 0x49, 0x62, 0x03, 0x9b, 0x61, 0x0c, 0x08, 0x00, 0x00, 0xff, 0xff, 0x7f, 0xbb, 0xf4, 0x0e, 68 | 0xd2, 0x00, 0x00, 0x00, 69 | } 70 | -------------------------------------------------------------------------------- /proto/testdata/golden_test.go: -------------------------------------------------------------------------------- 1 | // Go support for Protocol Buffers - Google's data interchange format 2 | // 3 | // Copyright 2012 The Go Authors. All rights reserved. 4 | // https://github.com/golang/protobuf 5 | // 6 | // Redistribution and use in source and binary forms, with or without 7 | // modification, are permitted provided that the following conditions are 8 | // met: 9 | // 10 | // * Redistributions of source code must retain the above copyright 11 | // notice, this list of conditions and the following disclaimer. 12 | // * Redistributions in binary form must reproduce the above 13 | // copyright notice, this list of conditions and the following disclaimer 14 | // in the documentation and/or other materials provided with the 15 | // distribution. 16 | // * Neither the name of Google Inc. nor the names of its 17 | // contributors may be used to endorse or promote products derived from 18 | // this software without specific prior written permission. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | // Verify that the compiler output for test.proto is unchanged. 33 | 34 | package testdata 35 | 36 | import ( 37 | "crypto/sha1" 38 | "fmt" 39 | "io/ioutil" 40 | "os" 41 | "os/exec" 42 | "path/filepath" 43 | "testing" 44 | ) 45 | 46 | // sum returns in string form (for easy comparison) the SHA-1 hash of the named file. 47 | func sum(t *testing.T, name string) string { 48 | data, err := ioutil.ReadFile(name) 49 | if err != nil { 50 | t.Fatal(err) 51 | } 52 | t.Logf("sum(%q): length is %d", name, len(data)) 53 | hash := sha1.New() 54 | _, err = hash.Write(data) 55 | if err != nil { 56 | t.Fatal(err) 57 | } 58 | return fmt.Sprintf("% x", hash.Sum(nil)) 59 | } 60 | 61 | func run(t *testing.T, name string, args ...string) { 62 | cmd := exec.Command(name, args...) 63 | cmd.Stdin = os.Stdin 64 | cmd.Stdout = os.Stdout 65 | cmd.Stderr = os.Stderr 66 | err := cmd.Run() 67 | if err != nil { 68 | t.Fatal(err) 69 | } 70 | } 71 | 72 | func TestGolden(t *testing.T) { 73 | // Compute the original checksum. 74 | goldenSum := sum(t, "test.pb.go") 75 | // Run the proto compiler. 76 | run(t, "protoc", "--go_out="+os.TempDir(), "test.proto") 77 | newFile := filepath.Join(os.TempDir(), "test.pb.go") 78 | defer os.Remove(newFile) 79 | // Compute the new checksum. 80 | newSum := sum(t, newFile) 81 | // Verify 82 | if newSum != goldenSum { 83 | run(t, "diff", "-u", "test.pb.go", newFile) 84 | t.Fatal("Code generated by protoc-gen-go has changed; update test.pb.go") 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /protoc-gen-go/generator/name_test.go: -------------------------------------------------------------------------------- 1 | // Go support for Protocol Buffers - Google's data interchange format 2 | // 3 | // Copyright 2013 The Go Authors. All rights reserved. 4 | // https://github.com/golang/protobuf 5 | // 6 | // Redistribution and use in source and binary forms, with or without 7 | // modification, are permitted provided that the following conditions are 8 | // met: 9 | // 10 | // * Redistributions of source code must retain the above copyright 11 | // notice, this list of conditions and the following disclaimer. 12 | // * Redistributions in binary form must reproduce the above 13 | // copyright notice, this list of conditions and the following disclaimer 14 | // in the documentation and/or other materials provided with the 15 | // distribution. 16 | // * Neither the name of Google Inc. nor the names of its 17 | // contributors may be used to endorse or promote products derived from 18 | // this software without specific prior written permission. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | package generator 33 | 34 | import ( 35 | "testing" 36 | 37 | "github.com/golang/protobuf/protoc-gen-go/descriptor" 38 | ) 39 | 40 | func TestCamelCase(t *testing.T) { 41 | tests := []struct { 42 | in, want string 43 | }{ 44 | {"one", "One"}, 45 | {"one_two", "OneTwo"}, 46 | {"_my_field_name_2", "XMyFieldName_2"}, 47 | {"Something_Capped", "Something_Capped"}, 48 | {"my_Name", "My_Name"}, 49 | {"OneTwo", "OneTwo"}, 50 | {"_", "X"}, 51 | {"_a_", "XA_"}, 52 | } 53 | for _, tc := range tests { 54 | if got := CamelCase(tc.in); got != tc.want { 55 | t.Errorf("CamelCase(%q) = %q, want %q", tc.in, got, tc.want) 56 | } 57 | } 58 | } 59 | 60 | func TestGoPackageOption(t *testing.T) { 61 | tests := []struct { 62 | in string 63 | impPath, pkg string 64 | ok bool 65 | }{ 66 | {"", "", "", false}, 67 | {"foo", "", "foo", true}, 68 | {"github.com/golang/bar", "github.com/golang/bar", "bar", true}, 69 | {"github.com/golang/bar;baz", "github.com/golang/bar", "baz", true}, 70 | } 71 | for _, tc := range tests { 72 | d := &FileDescriptor{ 73 | FileDescriptorProto: &descriptor.FileDescriptorProto{ 74 | Options: &descriptor.FileOptions{ 75 | GoPackage: &tc.in, 76 | }, 77 | }, 78 | } 79 | impPath, pkg, ok := d.goPackageOption() 80 | if impPath != tc.impPath || pkg != tc.pkg || ok != tc.ok { 81 | t.Errorf("go_package = %q => (%q, %q, %t), want (%q, %q, %t)", tc.in, 82 | impPath, pkg, ok, tc.impPath, tc.pkg, tc.ok) 83 | } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /protoc-gen-go/plugin/plugin.pb.golden: -------------------------------------------------------------------------------- 1 | // Code generated by protoc-gen-go. 2 | // source: google/protobuf/compiler/plugin.proto 3 | // DO NOT EDIT! 4 | 5 | package google_protobuf_compiler 6 | 7 | import proto "github.com/golang/protobuf/proto" 8 | import "math" 9 | import google_protobuf "github.com/golang/protobuf/protoc-gen-go/descriptor" 10 | 11 | // Reference proto and math imports to suppress error if they are not otherwise used. 12 | var _ = proto.GetString 13 | var _ = math.Inf 14 | 15 | type CodeGeneratorRequest struct { 16 | FileToGenerate []string `protobuf:"bytes,1,rep,name=file_to_generate" json:"file_to_generate,omitempty"` 17 | Parameter *string `protobuf:"bytes,2,opt,name=parameter" json:"parameter,omitempty"` 18 | ProtoFile []*google_protobuf.FileDescriptorProto `protobuf:"bytes,15,rep,name=proto_file" json:"proto_file,omitempty"` 19 | XXX_unrecognized []byte `json:"-"` 20 | } 21 | 22 | func (this *CodeGeneratorRequest) Reset() { *this = CodeGeneratorRequest{} } 23 | func (this *CodeGeneratorRequest) String() string { return proto.CompactTextString(this) } 24 | func (*CodeGeneratorRequest) ProtoMessage() {} 25 | 26 | func (this *CodeGeneratorRequest) GetParameter() string { 27 | if this != nil && this.Parameter != nil { 28 | return *this.Parameter 29 | } 30 | return "" 31 | } 32 | 33 | type CodeGeneratorResponse struct { 34 | Error *string `protobuf:"bytes,1,opt,name=error" json:"error,omitempty"` 35 | File []*CodeGeneratorResponse_File `protobuf:"bytes,15,rep,name=file" json:"file,omitempty"` 36 | XXX_unrecognized []byte `json:"-"` 37 | } 38 | 39 | func (this *CodeGeneratorResponse) Reset() { *this = CodeGeneratorResponse{} } 40 | func (this *CodeGeneratorResponse) String() string { return proto.CompactTextString(this) } 41 | func (*CodeGeneratorResponse) ProtoMessage() {} 42 | 43 | func (this *CodeGeneratorResponse) GetError() string { 44 | if this != nil && this.Error != nil { 45 | return *this.Error 46 | } 47 | return "" 48 | } 49 | 50 | type CodeGeneratorResponse_File struct { 51 | Name *string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"` 52 | InsertionPoint *string `protobuf:"bytes,2,opt,name=insertion_point" json:"insertion_point,omitempty"` 53 | Content *string `protobuf:"bytes,15,opt,name=content" json:"content,omitempty"` 54 | XXX_unrecognized []byte `json:"-"` 55 | } 56 | 57 | func (this *CodeGeneratorResponse_File) Reset() { *this = CodeGeneratorResponse_File{} } 58 | func (this *CodeGeneratorResponse_File) String() string { return proto.CompactTextString(this) } 59 | func (*CodeGeneratorResponse_File) ProtoMessage() {} 60 | 61 | func (this *CodeGeneratorResponse_File) GetName() string { 62 | if this != nil && this.Name != nil { 63 | return *this.Name 64 | } 65 | return "" 66 | } 67 | 68 | func (this *CodeGeneratorResponse_File) GetInsertionPoint() string { 69 | if this != nil && this.InsertionPoint != nil { 70 | return *this.InsertionPoint 71 | } 72 | return "" 73 | } 74 | 75 | func (this *CodeGeneratorResponse_File) GetContent() string { 76 | if this != nil && this.Content != nil { 77 | return *this.Content 78 | } 79 | return "" 80 | } 81 | 82 | func init() { 83 | } 84 | -------------------------------------------------------------------------------- /protoc-gen-go/testdata/extension_user.proto: -------------------------------------------------------------------------------- 1 | // Go support for Protocol Buffers - Google's data interchange format 2 | // 3 | // Copyright 2010 The Go Authors. All rights reserved. 4 | // https://github.com/golang/protobuf 5 | // 6 | // Redistribution and use in source and binary forms, with or without 7 | // modification, are permitted provided that the following conditions are 8 | // met: 9 | // 10 | // * Redistributions of source code must retain the above copyright 11 | // notice, this list of conditions and the following disclaimer. 12 | // * Redistributions in binary form must reproduce the above 13 | // copyright notice, this list of conditions and the following disclaimer 14 | // in the documentation and/or other materials provided with the 15 | // distribution. 16 | // * Neither the name of Google Inc. nor the names of its 17 | // contributors may be used to endorse or promote products derived from 18 | // this software without specific prior written permission. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | syntax = "proto2"; 33 | 34 | import "extension_base.proto"; 35 | import "extension_extra.proto"; 36 | 37 | package extension_user; 38 | 39 | message UserMessage { 40 | optional string name = 1; 41 | optional string rank = 2; 42 | } 43 | 44 | // Extend with a message 45 | extend extension_base.BaseMessage { 46 | optional UserMessage user_message = 5; 47 | } 48 | 49 | // Extend with a foreign message 50 | extend extension_base.BaseMessage { 51 | optional extension_extra.ExtraMessage extra_message = 9; 52 | } 53 | 54 | // Extend with some primitive types 55 | extend extension_base.BaseMessage { 56 | optional int32 width = 6; 57 | optional int64 area = 7; 58 | } 59 | 60 | // Extend inside the scope of another type 61 | message LoudMessage { 62 | extend extension_base.BaseMessage { 63 | optional uint32 volume = 8; 64 | } 65 | extensions 100 to max; 66 | } 67 | 68 | // Extend inside the scope of another type, using a message. 69 | message LoginMessage { 70 | extend extension_base.BaseMessage { 71 | optional UserMessage user_message = 16; 72 | } 73 | } 74 | 75 | // Extend with a repeated field 76 | extend extension_base.BaseMessage { 77 | repeated Detail detail = 17; 78 | } 79 | 80 | message Detail { 81 | optional string color = 1; 82 | } 83 | 84 | // An extension of an extension 85 | message Announcement { 86 | optional string words = 1; 87 | extend LoudMessage { 88 | optional Announcement loud_ext = 100; 89 | } 90 | } 91 | 92 | // Something that can be put in a message set. 93 | message OldStyleParcel { 94 | extend extension_base.OldStyleMessage { 95 | optional OldStyleParcel message_set_extension = 2001; 96 | } 97 | 98 | required string name = 1; 99 | optional int32 height = 2; 100 | } 101 | -------------------------------------------------------------------------------- /protoc-gen-go/main.go: -------------------------------------------------------------------------------- 1 | // Go support for Protocol Buffers - Google's data interchange format 2 | // 3 | // Copyright 2010 The Go Authors. All rights reserved. 4 | // https://github.com/golang/protobuf 5 | // 6 | // Redistribution and use in source and binary forms, with or without 7 | // modification, are permitted provided that the following conditions are 8 | // met: 9 | // 10 | // * Redistributions of source code must retain the above copyright 11 | // notice, this list of conditions and the following disclaimer. 12 | // * Redistributions in binary form must reproduce the above 13 | // copyright notice, this list of conditions and the following disclaimer 14 | // in the documentation and/or other materials provided with the 15 | // distribution. 16 | // * Neither the name of Google Inc. nor the names of its 17 | // contributors may be used to endorse or promote products derived from 18 | // this software without specific prior written permission. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | // protoc-gen-go is a plugin for the Google protocol buffer compiler to generate 33 | // Go code. Run it by building this program and putting it in your path with 34 | // the name 35 | // protoc-gen-go 36 | // That word 'go' at the end becomes part of the option string set for the 37 | // protocol compiler, so once the protocol compiler (protoc) is installed 38 | // you can run 39 | // protoc --go_out=output_directory input_directory/file.proto 40 | // to generate Go bindings for the protocol defined by file.proto. 41 | // With that input, the output will be written to 42 | // output_directory/file.pb.go 43 | // 44 | // The generated code is documented in the package comment for 45 | // the library. 46 | // 47 | // See the README and documentation for protocol buffers to learn more: 48 | // https://developers.google.com/protocol-buffers/ 49 | package main 50 | 51 | import ( 52 | "io/ioutil" 53 | "os" 54 | 55 | "github.com/golang/protobuf/proto" 56 | "github.com/golang/protobuf/protoc-gen-go/generator" 57 | ) 58 | 59 | func main() { 60 | // Begin by allocating a generator. The request and response structures are stored there 61 | // so we can do error handling easily - the response structure contains the field to 62 | // report failure. 63 | g := generator.New() 64 | 65 | data, err := ioutil.ReadAll(os.Stdin) 66 | if err != nil { 67 | g.Error(err, "reading input") 68 | } 69 | 70 | if err := proto.Unmarshal(data, g.Request); err != nil { 71 | g.Error(err, "parsing input proto") 72 | } 73 | 74 | if len(g.Request.FileToGenerate) == 0 { 75 | g.Fail("no files to generate") 76 | } 77 | 78 | g.CommandLineParameters(g.Request.GetParameter()) 79 | 80 | // Create a wrapped version of the Descriptors and EnumDescriptors that 81 | // point to the file that defines them. 82 | g.WrapTypes() 83 | 84 | g.SetPackageNames() 85 | g.BuildTypeNameMap() 86 | 87 | g.GenerateAllFiles() 88 | 89 | // Send back the results. 90 | data, err = proto.Marshal(g.Response) 91 | if err != nil { 92 | g.Error(err, "failed to marshal output proto") 93 | } 94 | _, err = os.Stdout.Write(data) 95 | if err != nil { 96 | g.Error(err, "failed to write output proto") 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /protoc-gen-go/testdata/imp.pb.go.golden: -------------------------------------------------------------------------------- 1 | // Code generated by protoc-gen-go. 2 | // source: imp.proto 3 | // DO NOT EDIT! 4 | 5 | package imp 6 | 7 | import proto "github.com/golang/protobuf/proto" 8 | import "math" 9 | import "os" 10 | import imp1 "imp2.pb" 11 | 12 | // Reference proto & math imports to suppress error if they are not otherwise used. 13 | var _ = proto.GetString 14 | var _ = math.Inf 15 | 16 | // Types from public import imp2.proto 17 | type PubliclyImportedMessage imp1.PubliclyImportedMessage 18 | 19 | func (this *PubliclyImportedMessage) Reset() { (*imp1.PubliclyImportedMessage)(this).Reset() } 20 | func (this *PubliclyImportedMessage) String() string { 21 | return (*imp1.PubliclyImportedMessage)(this).String() 22 | } 23 | 24 | // PubliclyImportedMessage from public import imp.proto 25 | 26 | type ImportedMessage_Owner int32 27 | 28 | const ( 29 | ImportedMessage_DAVE ImportedMessage_Owner = 1 30 | ImportedMessage_MIKE ImportedMessage_Owner = 2 31 | ) 32 | 33 | var ImportedMessage_Owner_name = map[int32]string{ 34 | 1: "DAVE", 35 | 2: "MIKE", 36 | } 37 | var ImportedMessage_Owner_value = map[string]int32{ 38 | "DAVE": 1, 39 | "MIKE": 2, 40 | } 41 | 42 | // NewImportedMessage_Owner is deprecated. Use x.Enum() instead. 43 | func NewImportedMessage_Owner(x ImportedMessage_Owner) *ImportedMessage_Owner { 44 | e := ImportedMessage_Owner(x) 45 | return &e 46 | } 47 | func (x ImportedMessage_Owner) Enum() *ImportedMessage_Owner { 48 | p := new(ImportedMessage_Owner) 49 | *p = x 50 | return p 51 | } 52 | func (x ImportedMessage_Owner) String() string { 53 | return proto.EnumName(ImportedMessage_Owner_name, int32(x)) 54 | } 55 | 56 | type ImportedMessage struct { 57 | Field *int64 `protobuf:"varint,1,req,name=field" json:"field,omitempty"` 58 | XXX_extensions map[int32][]byte `json:",omitempty"` 59 | XXX_unrecognized []byte `json:",omitempty"` 60 | } 61 | 62 | func (this *ImportedMessage) Reset() { *this = ImportedMessage{} } 63 | func (this *ImportedMessage) String() string { return proto.CompactTextString(this) } 64 | 65 | var extRange_ImportedMessage = []proto.ExtensionRange{ 66 | proto.ExtensionRange{90, 100}, 67 | } 68 | 69 | func (*ImportedMessage) ExtensionRangeArray() []proto.ExtensionRange { 70 | return extRange_ImportedMessage 71 | } 72 | func (this *ImportedMessage) ExtensionMap() map[int32][]byte { 73 | if this.XXX_extensions == nil { 74 | this.XXX_extensions = make(map[int32][]byte) 75 | } 76 | return this.XXX_extensions 77 | } 78 | 79 | type ImportedExtendable struct { 80 | XXX_extensions map[int32][]byte `json:",omitempty"` 81 | XXX_unrecognized []byte `json:",omitempty"` 82 | } 83 | 84 | func (this *ImportedExtendable) Reset() { *this = ImportedExtendable{} } 85 | func (this *ImportedExtendable) String() string { return proto.CompactTextString(this) } 86 | 87 | func (this *ImportedExtendable) Marshal() ([]byte, error) { 88 | return proto.MarshalMessageSet(this.ExtensionMap()) 89 | } 90 | func (this *ImportedExtendable) Unmarshal(buf []byte) error { 91 | return proto.UnmarshalMessageSet(buf, this.ExtensionMap()) 92 | } 93 | // ensure ImportedExtendable satisfies proto.Marshaler and proto.Unmarshaler 94 | var _ proto.Marshaler = (*ImportedExtendable)(nil) 95 | var _ proto.Unmarshaler = (*ImportedExtendable)(nil) 96 | 97 | var extRange_ImportedExtendable = []proto.ExtensionRange{ 98 | proto.ExtensionRange{100, 536870911}, 99 | } 100 | 101 | func (*ImportedExtendable) ExtensionRangeArray() []proto.ExtensionRange { 102 | return extRange_ImportedExtendable 103 | } 104 | func (this *ImportedExtendable) ExtensionMap() map[int32][]byte { 105 | if this.XXX_extensions == nil { 106 | this.XXX_extensions = make(map[int32][]byte) 107 | } 108 | return this.XXX_extensions 109 | } 110 | 111 | func init() { 112 | proto.RegisterEnum("imp.ImportedMessage_Owner", ImportedMessage_Owner_name, ImportedMessage_Owner_value) 113 | } 114 | -------------------------------------------------------------------------------- /ptypes/struct/struct.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | syntax = "proto3"; 32 | 33 | package google.protobuf; 34 | 35 | option csharp_namespace = "Google.Protobuf.WellKnownTypes"; 36 | option go_package = "github.com/golang/protobuf/ptypes/struct;structpb"; 37 | option java_package = "com.google.protobuf"; 38 | option java_outer_classname = "StructProto"; 39 | option java_multiple_files = true; 40 | option java_generate_equals_and_hash = true; 41 | option objc_class_prefix = "GPB"; 42 | 43 | 44 | // `Struct` represents a structured data value, consisting of fields 45 | // which map to dynamically typed values. In some languages, `Struct` 46 | // might be supported by a native representation. For example, in 47 | // scripting languages like JS a struct is represented as an 48 | // object. The details of that representation are described together 49 | // with the proto support for the language. 50 | // 51 | // The JSON representation for `Struct` is JSON object. 52 | message Struct { 53 | // Unordered map of dynamically typed values. 54 | map fields = 1; 55 | } 56 | 57 | // `Value` represents a dynamically typed value which can be either 58 | // null, a number, a string, a boolean, a recursive struct value, or a 59 | // list of values. A producer of value is expected to set one of that 60 | // variants, absence of any variant indicates an error. 61 | // 62 | // The JSON representation for `Value` is JSON value. 63 | message Value { 64 | // The kind of value. 65 | oneof kind { 66 | // Represents a null value. 67 | NullValue null_value = 1; 68 | // Represents a double value. 69 | double number_value = 2; 70 | // Represents a string value. 71 | string string_value = 3; 72 | // Represents a boolean value. 73 | bool bool_value = 4; 74 | // Represents a structured value. 75 | Struct struct_value = 5; 76 | // Represents a repeated `Value`. 77 | ListValue list_value = 6; 78 | } 79 | } 80 | 81 | // `NullValue` is a singleton enumeration to represent the null value for the 82 | // `Value` type union. 83 | // 84 | // The JSON representation for `NullValue` is JSON `null`. 85 | enum NullValue { 86 | // Null value. 87 | NULL_VALUE = 0; 88 | } 89 | 90 | // `ListValue` is a wrapper around a repeated field of values. 91 | // 92 | // The JSON representation for `ListValue` is JSON array. 93 | message ListValue { 94 | // Repeated field of dynamically typed values. 95 | repeated Value values = 1; 96 | } 97 | -------------------------------------------------------------------------------- /ptypes/duration.go: -------------------------------------------------------------------------------- 1 | // Go support for Protocol Buffers - Google's data interchange format 2 | // 3 | // Copyright 2016 The Go Authors. All rights reserved. 4 | // https://github.com/golang/protobuf 5 | // 6 | // Redistribution and use in source and binary forms, with or without 7 | // modification, are permitted provided that the following conditions are 8 | // met: 9 | // 10 | // * Redistributions of source code must retain the above copyright 11 | // notice, this list of conditions and the following disclaimer. 12 | // * Redistributions in binary form must reproduce the above 13 | // copyright notice, this list of conditions and the following disclaimer 14 | // in the documentation and/or other materials provided with the 15 | // distribution. 16 | // * Neither the name of Google Inc. nor the names of its 17 | // contributors may be used to endorse or promote products derived from 18 | // this software without specific prior written permission. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | package ptypes 33 | 34 | // This file implements conversions between google.protobuf.Duration 35 | // and time.Duration. 36 | 37 | import ( 38 | "errors" 39 | "fmt" 40 | "time" 41 | 42 | durpb "github.com/golang/protobuf/ptypes/duration" 43 | ) 44 | 45 | const ( 46 | // Range of a durpb.Duration in seconds, as specified in 47 | // google/protobuf/duration.proto. This is about 10,000 years in seconds. 48 | maxSeconds = int64(10000 * 365.25 * 24 * 60 * 60) 49 | minSeconds = -maxSeconds 50 | ) 51 | 52 | // validateDuration determines whether the durpb.Duration is valid according to the 53 | // definition in google/protobuf/duration.proto. A valid durpb.Duration 54 | // may still be too large to fit into a time.Duration (the range of durpb.Duration 55 | // is about 10,000 years, and the range of time.Duration is about 290). 56 | func validateDuration(d *durpb.Duration) error { 57 | if d == nil { 58 | return errors.New("duration: nil Duration") 59 | } 60 | if d.Seconds < minSeconds || d.Seconds > maxSeconds { 61 | return fmt.Errorf("duration: %v: seconds out of range", d) 62 | } 63 | if d.Nanos <= -1e9 || d.Nanos >= 1e9 { 64 | return fmt.Errorf("duration: %v: nanos out of range", d) 65 | } 66 | // Seconds and Nanos must have the same sign, unless d.Nanos is zero. 67 | if (d.Seconds < 0 && d.Nanos > 0) || (d.Seconds > 0 && d.Nanos < 0) { 68 | return fmt.Errorf("duration: %v: seconds and nanos have different signs", d) 69 | } 70 | return nil 71 | } 72 | 73 | // Duration converts a durpb.Duration to a time.Duration. Duration 74 | // returns an error if the durpb.Duration is invalid or is too large to be 75 | // represented in a time.Duration. 76 | func Duration(p *durpb.Duration) (time.Duration, error) { 77 | if err := validateDuration(p); err != nil { 78 | return 0, err 79 | } 80 | d := time.Duration(p.Seconds) * time.Second 81 | if int64(d/time.Second) != p.Seconds { 82 | return 0, fmt.Errorf("duration: %v is out of range for time.Duration", p) 83 | } 84 | if p.Nanos != 0 { 85 | d += time.Duration(p.Nanos) 86 | if (d < 0) != (p.Nanos < 0) { 87 | return 0, fmt.Errorf("duration: %v is out of range for time.Duration", p) 88 | } 89 | } 90 | return d, nil 91 | } 92 | 93 | // DurationProto converts a time.Duration to a durpb.Duration. 94 | func DurationProto(d time.Duration) *durpb.Duration { 95 | nanos := d.Nanoseconds() 96 | secs := nanos / 1e9 97 | nanos -= secs * 1e9 98 | return &durpb.Duration{ 99 | Seconds: secs, 100 | Nanos: int32(nanos), 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /ptypes/wrappers/wrappers.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | // Wrappers for primitive (non-message) types. These types are useful 32 | // for embedding primitives in the `google.protobuf.Any` type and for places 33 | // where we need to distinguish between the absence of a primitive 34 | // typed field and its default value. 35 | 36 | syntax = "proto3"; 37 | 38 | package google.protobuf; 39 | 40 | option csharp_namespace = "Google.Protobuf.WellKnownTypes"; 41 | option cc_enable_arenas = true; 42 | option go_package = "github.com/golang/protobuf/ptypes/wrappers"; 43 | option java_package = "com.google.protobuf"; 44 | option java_outer_classname = "WrappersProto"; 45 | option java_multiple_files = true; 46 | option java_generate_equals_and_hash = true; 47 | option objc_class_prefix = "GPB"; 48 | 49 | // Wrapper message for `double`. 50 | // 51 | // The JSON representation for `DoubleValue` is JSON number. 52 | message DoubleValue { 53 | // The double value. 54 | double value = 1; 55 | } 56 | 57 | // Wrapper message for `float`. 58 | // 59 | // The JSON representation for `FloatValue` is JSON number. 60 | message FloatValue { 61 | // The float value. 62 | float value = 1; 63 | } 64 | 65 | // Wrapper message for `int64`. 66 | // 67 | // The JSON representation for `Int64Value` is JSON string. 68 | message Int64Value { 69 | // The int64 value. 70 | int64 value = 1; 71 | } 72 | 73 | // Wrapper message for `uint64`. 74 | // 75 | // The JSON representation for `UInt64Value` is JSON string. 76 | message UInt64Value { 77 | // The uint64 value. 78 | uint64 value = 1; 79 | } 80 | 81 | // Wrapper message for `int32`. 82 | // 83 | // The JSON representation for `Int32Value` is JSON number. 84 | message Int32Value { 85 | // The int32 value. 86 | int32 value = 1; 87 | } 88 | 89 | // Wrapper message for `uint32`. 90 | // 91 | // The JSON representation for `UInt32Value` is JSON number. 92 | message UInt32Value { 93 | // The uint32 value. 94 | uint32 value = 1; 95 | } 96 | 97 | // Wrapper message for `bool`. 98 | // 99 | // The JSON representation for `BoolValue` is JSON `true` and `false`. 100 | message BoolValue { 101 | // The bool value. 102 | bool value = 1; 103 | } 104 | 105 | // Wrapper message for `string`. 106 | // 107 | // The JSON representation for `StringValue` is JSON string. 108 | message StringValue { 109 | // The string value. 110 | string value = 1; 111 | } 112 | 113 | // Wrapper message for `bytes`. 114 | // 115 | // The JSON representation for `BytesValue` is JSON string. 116 | message BytesValue { 117 | // The bytes value. 118 | bytes value = 1; 119 | } 120 | -------------------------------------------------------------------------------- /ptypes/any_test.go: -------------------------------------------------------------------------------- 1 | // Go support for Protocol Buffers - Google's data interchange format 2 | // 3 | // Copyright 2016 The Go Authors. All rights reserved. 4 | // https://github.com/golang/protobuf 5 | // 6 | // Redistribution and use in source and binary forms, with or without 7 | // modification, are permitted provided that the following conditions are 8 | // met: 9 | // 10 | // * Redistributions of source code must retain the above copyright 11 | // notice, this list of conditions and the following disclaimer. 12 | // * Redistributions in binary form must reproduce the above 13 | // copyright notice, this list of conditions and the following disclaimer 14 | // in the documentation and/or other materials provided with the 15 | // distribution. 16 | // * Neither the name of Google Inc. nor the names of its 17 | // contributors may be used to endorse or promote products derived from 18 | // this software without specific prior written permission. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | package ptypes 33 | 34 | import ( 35 | "testing" 36 | 37 | "github.com/golang/protobuf/proto" 38 | pb "github.com/golang/protobuf/protoc-gen-go/descriptor" 39 | "github.com/golang/protobuf/ptypes/any" 40 | ) 41 | 42 | func TestMarshalUnmarshal(t *testing.T) { 43 | orig := &any.Any{Value: []byte("test")} 44 | 45 | packed, err := MarshalAny(orig) 46 | if err != nil { 47 | t.Errorf("MarshalAny(%+v): got: _, %v exp: _, nil", orig, err) 48 | } 49 | 50 | unpacked := &any.Any{} 51 | err = UnmarshalAny(packed, unpacked) 52 | if err != nil || !proto.Equal(unpacked, orig) { 53 | t.Errorf("got: %v, %+v; want nil, %+v", err, unpacked, orig) 54 | } 55 | } 56 | 57 | func TestIs(t *testing.T) { 58 | a, err := MarshalAny(&pb.FileDescriptorProto{}) 59 | if err != nil { 60 | t.Fatal(err) 61 | } 62 | if Is(a, &pb.DescriptorProto{}) { 63 | t.Error("FileDescriptorProto is not a DescriptorProto, but Is says it is") 64 | } 65 | if !Is(a, &pb.FileDescriptorProto{}) { 66 | t.Error("FileDescriptorProto is indeed a FileDescriptorProto, but Is says it is not") 67 | } 68 | } 69 | 70 | func TestIsDifferentUrlPrefixes(t *testing.T) { 71 | m := &pb.FileDescriptorProto{} 72 | a := &any.Any{TypeUrl: "foo/bar/" + proto.MessageName(m)} 73 | if !Is(a, m) { 74 | t.Errorf("message with type url %q didn't satisfy Is for type %q", a.TypeUrl, proto.MessageName(m)) 75 | } 76 | } 77 | 78 | func TestUnmarshalDynamic(t *testing.T) { 79 | want := &pb.FileDescriptorProto{Name: proto.String("foo")} 80 | a, err := MarshalAny(want) 81 | if err != nil { 82 | t.Fatal(err) 83 | } 84 | var got DynamicAny 85 | if err := UnmarshalAny(a, &got); err != nil { 86 | t.Fatal(err) 87 | } 88 | if !proto.Equal(got.Message, want) { 89 | t.Errorf("invalid result from UnmarshalAny, got %q want %q", got.Message, want) 90 | } 91 | } 92 | 93 | func TestEmpty(t *testing.T) { 94 | want := &pb.FileDescriptorProto{} 95 | a, err := MarshalAny(want) 96 | if err != nil { 97 | t.Fatal(err) 98 | } 99 | got, err := Empty(a) 100 | if err != nil { 101 | t.Fatal(err) 102 | } 103 | if !proto.Equal(got, want) { 104 | t.Errorf("unequal empty message, got %q, want %q", got, want) 105 | } 106 | 107 | // that's a valid type_url for a message which shouldn't be linked into this 108 | // test binary. We want an error. 109 | a.TypeUrl = "type.googleapis.com/google.protobuf.FieldMask" 110 | if _, err := Empty(a); err == nil { 111 | t.Errorf("got no error for an attempt to create a message of type %q, which shouldn't be linked in", a.TypeUrl) 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /ptypes/duration/duration.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | syntax = "proto3"; 32 | 33 | package google.protobuf; 34 | 35 | option csharp_namespace = "Google.Protobuf.WellKnownTypes"; 36 | option go_package = "github.com/golang/protobuf/ptypes/duration"; 37 | option java_package = "com.google.protobuf"; 38 | option java_outer_classname = "DurationProto"; 39 | option java_multiple_files = true; 40 | option java_generate_equals_and_hash = true; 41 | option objc_class_prefix = "GPB"; 42 | 43 | // A Duration represents a signed, fixed-length span of time represented 44 | // as a count of seconds and fractions of seconds at nanosecond 45 | // resolution. It is independent of any calendar and concepts like "day" 46 | // or "month". It is related to Timestamp in that the difference between 47 | // two Timestamp values is a Duration and it can be added or subtracted 48 | // from a Timestamp. Range is approximately +-10,000 years. 49 | // 50 | // Example 1: Compute Duration from two Timestamps in pseudo code. 51 | // 52 | // Timestamp start = ...; 53 | // Timestamp end = ...; 54 | // Duration duration = ...; 55 | // 56 | // duration.seconds = end.seconds - start.seconds; 57 | // duration.nanos = end.nanos - start.nanos; 58 | // 59 | // if (duration.seconds < 0 && duration.nanos > 0) { 60 | // duration.seconds += 1; 61 | // duration.nanos -= 1000000000; 62 | // } else if (durations.seconds > 0 && duration.nanos < 0) { 63 | // duration.seconds -= 1; 64 | // duration.nanos += 1000000000; 65 | // } 66 | // 67 | // Example 2: Compute Timestamp from Timestamp + Duration in pseudo code. 68 | // 69 | // Timestamp start = ...; 70 | // Duration duration = ...; 71 | // Timestamp end = ...; 72 | // 73 | // end.seconds = start.seconds + duration.seconds; 74 | // end.nanos = start.nanos + duration.nanos; 75 | // 76 | // if (end.nanos < 0) { 77 | // end.seconds -= 1; 78 | // end.nanos += 1000000000; 79 | // } else if (end.nanos >= 1000000000) { 80 | // end.seconds += 1; 81 | // end.nanos -= 1000000000; 82 | // } 83 | // 84 | // 85 | message Duration { 86 | 87 | // Signed seconds of the span of time. Must be from -315,576,000,000 88 | // to +315,576,000,000 inclusive. 89 | int64 seconds = 1; 90 | 91 | // Signed fractions of a second at nanosecond resolution of the span 92 | // of time. Durations less than one second are represented with a 0 93 | // `seconds` field and a positive or negative `nanos` field. For durations 94 | // of one second or more, a non-zero value for the `nanos` field must be 95 | // of the same sign as the `seconds` field. Must be from -999,999,999 96 | // to +999,999,999 inclusive. 97 | int32 nanos = 2; 98 | } 99 | -------------------------------------------------------------------------------- /proto/proto3_test.go: -------------------------------------------------------------------------------- 1 | // Go support for Protocol Buffers - Google's data interchange format 2 | // 3 | // Copyright 2014 The Go Authors. All rights reserved. 4 | // https://github.com/golang/protobuf 5 | // 6 | // Redistribution and use in source and binary forms, with or without 7 | // modification, are permitted provided that the following conditions are 8 | // met: 9 | // 10 | // * Redistributions of source code must retain the above copyright 11 | // notice, this list of conditions and the following disclaimer. 12 | // * Redistributions in binary form must reproduce the above 13 | // copyright notice, this list of conditions and the following disclaimer 14 | // in the documentation and/or other materials provided with the 15 | // distribution. 16 | // * Neither the name of Google Inc. nor the names of its 17 | // contributors may be used to endorse or promote products derived from 18 | // this software without specific prior written permission. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | package proto_test 33 | 34 | import ( 35 | "testing" 36 | 37 | "github.com/golang/protobuf/proto" 38 | pb "github.com/golang/protobuf/proto/proto3_proto" 39 | tpb "github.com/golang/protobuf/proto/testdata" 40 | ) 41 | 42 | func TestProto3ZeroValues(t *testing.T) { 43 | tests := []struct { 44 | desc string 45 | m proto.Message 46 | }{ 47 | {"zero message", &pb.Message{}}, 48 | {"empty bytes field", &pb.Message{Data: []byte{}}}, 49 | } 50 | for _, test := range tests { 51 | b, err := proto.Marshal(test.m) 52 | if err != nil { 53 | t.Errorf("%s: proto.Marshal: %v", test.desc, err) 54 | continue 55 | } 56 | if len(b) > 0 { 57 | t.Errorf("%s: Encoding is non-empty: %q", test.desc, b) 58 | } 59 | } 60 | } 61 | 62 | func TestRoundTripProto3(t *testing.T) { 63 | m := &pb.Message{ 64 | Name: "David", // (2 | 1<<3): 0x0a 0x05 "David" 65 | Hilarity: pb.Message_PUNS, // (0 | 2<<3): 0x10 0x01 66 | HeightInCm: 178, // (0 | 3<<3): 0x18 0xb2 0x01 67 | Data: []byte("roboto"), // (2 | 4<<3): 0x20 0x06 "roboto" 68 | ResultCount: 47, // (0 | 7<<3): 0x38 0x2f 69 | TrueScotsman: true, // (0 | 8<<3): 0x40 0x01 70 | Score: 8.1, // (5 | 9<<3): 0x4d <8.1> 71 | 72 | Key: []uint64{1, 0xdeadbeef}, 73 | Nested: &pb.Nested{ 74 | Bunny: "Monty", 75 | }, 76 | } 77 | t.Logf(" m: %v", m) 78 | 79 | b, err := proto.Marshal(m) 80 | if err != nil { 81 | t.Fatalf("proto.Marshal: %v", err) 82 | } 83 | t.Logf(" b: %q", b) 84 | 85 | m2 := new(pb.Message) 86 | if err := proto.Unmarshal(b, m2); err != nil { 87 | t.Fatalf("proto.Unmarshal: %v", err) 88 | } 89 | t.Logf("m2: %v", m2) 90 | 91 | if !proto.Equal(m, m2) { 92 | t.Errorf("proto.Equal returned false:\n m: %v\nm2: %v", m, m2) 93 | } 94 | } 95 | 96 | func TestProto3SetDefaults(t *testing.T) { 97 | in := &pb.Message{ 98 | Terrain: map[string]*pb.Nested{ 99 | "meadow": new(pb.Nested), 100 | }, 101 | Proto2Field: new(tpb.SubDefaults), 102 | Proto2Value: map[string]*tpb.SubDefaults{ 103 | "badlands": new(tpb.SubDefaults), 104 | }, 105 | } 106 | 107 | got := proto.Clone(in).(*pb.Message) 108 | proto.SetDefaults(got) 109 | 110 | // There are no defaults in proto3. Everything should be the zero value, but 111 | // we need to remember to set defaults for nested proto2 messages. 112 | want := &pb.Message{ 113 | Terrain: map[string]*pb.Nested{ 114 | "meadow": new(pb.Nested), 115 | }, 116 | Proto2Field: &tpb.SubDefaults{N: proto.Int64(7)}, 117 | Proto2Value: map[string]*tpb.SubDefaults{ 118 | "badlands": &tpb.SubDefaults{N: proto.Int64(7)}, 119 | }, 120 | } 121 | 122 | if !proto.Equal(got, want) { 123 | t.Errorf("with in = %v\nproto.SetDefaults(in) =>\ngot %v\nwant %v", in, got, want) 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /jsonpb/jsonpb_test_proto/test_objects.proto: -------------------------------------------------------------------------------- 1 | // Go support for Protocol Buffers - Google's data interchange format 2 | // 3 | // Copyright 2015 The Go Authors. All rights reserved. 4 | // https://github.com/golang/protobuf 5 | // 6 | // Redistribution and use in source and binary forms, with or without 7 | // modification, are permitted provided that the following conditions are 8 | // met: 9 | // 10 | // * Redistributions of source code must retain the above copyright 11 | // notice, this list of conditions and the following disclaimer. 12 | // * Redistributions in binary form must reproduce the above 13 | // copyright notice, this list of conditions and the following disclaimer 14 | // in the documentation and/or other materials provided with the 15 | // distribution. 16 | // * Neither the name of Google Inc. nor the names of its 17 | // contributors may be used to endorse or promote products derived from 18 | // this software without specific prior written permission. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | syntax = "proto2"; 33 | 34 | import "google/protobuf/any.proto"; 35 | import "google/protobuf/duration.proto"; 36 | import "google/protobuf/struct.proto"; 37 | import "google/protobuf/timestamp.proto"; 38 | import "google/protobuf/wrappers.proto"; 39 | 40 | package jsonpb; 41 | 42 | // Test message for holding primitive types. 43 | message Simple { 44 | optional bool o_bool = 1; 45 | optional int32 o_int32 = 2; 46 | optional int64 o_int64 = 3; 47 | optional uint32 o_uint32 = 4; 48 | optional uint64 o_uint64 = 5; 49 | optional sint32 o_sint32 = 6; 50 | optional sint64 o_sint64 = 7; 51 | optional float o_float = 8; 52 | optional double o_double = 9; 53 | optional string o_string = 10; 54 | optional bytes o_bytes = 11; 55 | } 56 | 57 | // Test message for holding repeated primitives. 58 | message Repeats { 59 | repeated bool r_bool = 1; 60 | repeated int32 r_int32 = 2; 61 | repeated int64 r_int64 = 3; 62 | repeated uint32 r_uint32 = 4; 63 | repeated uint64 r_uint64 = 5; 64 | repeated sint32 r_sint32 = 6; 65 | repeated sint64 r_sint64 = 7; 66 | repeated float r_float = 8; 67 | repeated double r_double = 9; 68 | repeated string r_string = 10; 69 | repeated bytes r_bytes = 11; 70 | } 71 | 72 | // Test message for holding enums and nested messages. 73 | message Widget { 74 | enum Color { 75 | RED = 0; 76 | GREEN = 1; 77 | BLUE = 2; 78 | }; 79 | optional Color color = 1; 80 | repeated Color r_color = 2; 81 | 82 | optional Simple simple = 10; 83 | repeated Simple r_simple = 11; 84 | 85 | optional Repeats repeats = 20; 86 | repeated Repeats r_repeats = 21; 87 | } 88 | 89 | message Maps { 90 | map m_int64_str = 1; 91 | map m_bool_simple = 2; 92 | } 93 | 94 | message MsgWithOneof { 95 | oneof union { 96 | string title = 1; 97 | int64 salary = 2; 98 | string Country = 3; 99 | } 100 | } 101 | 102 | message Real { 103 | optional double value = 1; 104 | extensions 100 to max; 105 | } 106 | 107 | extend Real { 108 | optional string name = 124; 109 | } 110 | 111 | message Complex { 112 | extend Real { 113 | optional Complex real_extension = 123; 114 | } 115 | optional double imaginary = 1; 116 | extensions 100 to max; 117 | } 118 | 119 | message MsgWithListVal { 120 | map mapped = 1; 121 | repeated google.protobuf.ListValue repeated = 2; 122 | } 123 | 124 | message KnownTypes { 125 | optional google.protobuf.Any an = 14; 126 | optional google.protobuf.Duration dur = 1; 127 | optional google.protobuf.Struct st = 12; 128 | optional google.protobuf.Timestamp ts = 2; 129 | 130 | optional google.protobuf.DoubleValue dbl = 3; 131 | optional google.protobuf.FloatValue flt = 4; 132 | optional google.protobuf.Int64Value i64 = 5; 133 | optional google.protobuf.UInt64Value u64 = 6; 134 | optional google.protobuf.Int32Value i32 = 7; 135 | optional google.protobuf.UInt32Value u32 = 8; 136 | optional google.protobuf.BoolValue bool = 9; 137 | optional google.protobuf.StringValue str = 10; 138 | optional google.protobuf.BytesValue bytes = 11; 139 | optional google.protobuf.ListValue listval = 15; 140 | optional google.protobuf.Value val = 16; 141 | } 142 | -------------------------------------------------------------------------------- /ptypes/timestamp/timestamp.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | syntax = "proto3"; 32 | 33 | package google.protobuf; 34 | 35 | option csharp_namespace = "Google.Protobuf.WellKnownTypes"; 36 | option cc_enable_arenas = true; 37 | option go_package = "github.com/golang/protobuf/ptypes/timestamp"; 38 | option java_package = "com.google.protobuf"; 39 | option java_outer_classname = "TimestampProto"; 40 | option java_multiple_files = true; 41 | option java_generate_equals_and_hash = true; 42 | option objc_class_prefix = "GPB"; 43 | 44 | // A Timestamp represents a point in time independent of any time zone 45 | // or calendar, represented as seconds and fractions of seconds at 46 | // nanosecond resolution in UTC Epoch time. It is encoded using the 47 | // Proleptic Gregorian Calendar which extends the Gregorian calendar 48 | // backwards to year one. It is encoded assuming all minutes are 60 49 | // seconds long, i.e. leap seconds are "smeared" so that no leap second 50 | // table is needed for interpretation. Range is from 51 | // 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z. 52 | // By restricting to that range, we ensure that we can convert to 53 | // and from RFC 3339 date strings. 54 | // See [https://www.ietf.org/rfc/rfc3339.txt](https://www.ietf.org/rfc/rfc3339.txt). 55 | // 56 | // Example 1: Compute Timestamp from POSIX `time()`. 57 | // 58 | // Timestamp timestamp; 59 | // timestamp.set_seconds(time(NULL)); 60 | // timestamp.set_nanos(0); 61 | // 62 | // Example 2: Compute Timestamp from POSIX `gettimeofday()`. 63 | // 64 | // struct timeval tv; 65 | // gettimeofday(&tv, NULL); 66 | // 67 | // Timestamp timestamp; 68 | // timestamp.set_seconds(tv.tv_sec); 69 | // timestamp.set_nanos(tv.tv_usec * 1000); 70 | // 71 | // Example 3: Compute Timestamp from Win32 `GetSystemTimeAsFileTime()`. 72 | // 73 | // FILETIME ft; 74 | // GetSystemTimeAsFileTime(&ft); 75 | // UINT64 ticks = (((UINT64)ft.dwHighDateTime) << 32) | ft.dwLowDateTime; 76 | // 77 | // // A Windows tick is 100 nanoseconds. Windows epoch 1601-01-01T00:00:00Z 78 | // // is 11644473600 seconds before Unix epoch 1970-01-01T00:00:00Z. 79 | // Timestamp timestamp; 80 | // timestamp.set_seconds((INT64) ((ticks / 10000000) - 11644473600LL)); 81 | // timestamp.set_nanos((INT32) ((ticks % 10000000) * 100)); 82 | // 83 | // Example 4: Compute Timestamp from Java `System.currentTimeMillis()`. 84 | // 85 | // long millis = System.currentTimeMillis(); 86 | // 87 | // Timestamp timestamp = Timestamp.newBuilder().setSeconds(millis / 1000) 88 | // .setNanos((int) ((millis % 1000) * 1000000)).build(); 89 | // 90 | // 91 | // Example 5: Compute Timestamp from current time in Python. 92 | // 93 | // now = time.time() 94 | // seconds = int(now) 95 | // nanos = int((now - seconds) * 10**9) 96 | // timestamp = Timestamp(seconds=seconds, nanos=nanos) 97 | // 98 | // 99 | message Timestamp { 100 | 101 | // Represents seconds of UTC time since Unix epoch 102 | // 1970-01-01T00:00:00Z. Must be from from 0001-01-01T00:00:00Z to 103 | // 9999-12-31T23:59:59Z inclusive. 104 | int64 seconds = 1; 105 | 106 | // Non-negative fractions of a second at nanosecond resolution. Negative 107 | // second values with fractions must still have non-negative nanos values 108 | // that count forward in time. Must be from 0 to 999,999,999 109 | // inclusive. 110 | int32 nanos = 2; 111 | } 112 | -------------------------------------------------------------------------------- /ptypes/timestamp.go: -------------------------------------------------------------------------------- 1 | // Go support for Protocol Buffers - Google's data interchange format 2 | // 3 | // Copyright 2016 The Go Authors. All rights reserved. 4 | // https://github.com/golang/protobuf 5 | // 6 | // Redistribution and use in source and binary forms, with or without 7 | // modification, are permitted provided that the following conditions are 8 | // met: 9 | // 10 | // * Redistributions of source code must retain the above copyright 11 | // notice, this list of conditions and the following disclaimer. 12 | // * Redistributions in binary form must reproduce the above 13 | // copyright notice, this list of conditions and the following disclaimer 14 | // in the documentation and/or other materials provided with the 15 | // distribution. 16 | // * Neither the name of Google Inc. nor the names of its 17 | // contributors may be used to endorse or promote products derived from 18 | // this software without specific prior written permission. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | package ptypes 33 | 34 | // This file implements operations on google.protobuf.Timestamp. 35 | 36 | import ( 37 | "errors" 38 | "fmt" 39 | "time" 40 | 41 | tspb "github.com/golang/protobuf/ptypes/timestamp" 42 | ) 43 | 44 | const ( 45 | // Seconds field of the earliest valid Timestamp. 46 | // This is time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC).Unix(). 47 | minValidSeconds = -62135596800 48 | // Seconds field just after the latest valid Timestamp. 49 | // This is time.Date(10000, 1, 1, 0, 0, 0, 0, time.UTC).Unix(). 50 | maxValidSeconds = 253402300800 51 | ) 52 | 53 | // validateTimestamp determines whether a Timestamp is valid. 54 | // A valid timestamp represents a time in the range 55 | // [0001-01-01, 10000-01-01) and has a Nanos field 56 | // in the range [0, 1e9). 57 | // 58 | // If the Timestamp is valid, validateTimestamp returns nil. 59 | // Otherwise, it returns an error that describes 60 | // the problem. 61 | // 62 | // Every valid Timestamp can be represented by a time.Time, but the converse is not true. 63 | func validateTimestamp(ts *tspb.Timestamp) error { 64 | if ts == nil { 65 | return errors.New("timestamp: nil Timestamp") 66 | } 67 | if ts.Seconds < minValidSeconds { 68 | return fmt.Errorf("timestamp: %v before 0001-01-01", ts) 69 | } 70 | if ts.Seconds >= maxValidSeconds { 71 | return fmt.Errorf("timestamp: %v after 10000-01-01", ts) 72 | } 73 | if ts.Nanos < 0 || ts.Nanos >= 1e9 { 74 | return fmt.Errorf("timestamp: %v: nanos not in range [0, 1e9)", ts) 75 | } 76 | return nil 77 | } 78 | 79 | // Timestamp converts a google.protobuf.Timestamp proto to a time.Time. 80 | // It returns an error if the argument is invalid. 81 | // 82 | // Unlike most Go functions, if Timestamp returns an error, the first return value 83 | // is not the zero time.Time. Instead, it is the value obtained from the 84 | // time.Unix function when passed the contents of the Timestamp, in the UTC 85 | // locale. This may or may not be a meaningful time; many invalid Timestamps 86 | // do map to valid time.Times. 87 | // 88 | // A nil Timestamp returns an error. The first return value in that case is 89 | // undefined. 90 | func Timestamp(ts *tspb.Timestamp) (time.Time, error) { 91 | // Don't return the zero value on error, because corresponds to a valid 92 | // timestamp. Instead return whatever time.Unix gives us. 93 | var t time.Time 94 | if ts == nil { 95 | t = time.Unix(0, 0).UTC() // treat nil like the empty Timestamp 96 | } else { 97 | t = time.Unix(ts.Seconds, int64(ts.Nanos)).UTC() 98 | } 99 | return t, validateTimestamp(ts) 100 | } 101 | 102 | // TimestampProto converts the time.Time to a google.protobuf.Timestamp proto. 103 | // It returns an error if the resulting Timestamp is invalid. 104 | func TimestampProto(t time.Time) (*tspb.Timestamp, error) { 105 | seconds := t.Unix() 106 | nanos := int32(t.Sub(time.Unix(seconds, 0))) 107 | ts := &tspb.Timestamp{ 108 | Seconds: seconds, 109 | Nanos: nanos, 110 | } 111 | if err := validateTimestamp(ts); err != nil { 112 | return nil, err 113 | } 114 | return ts, nil 115 | } 116 | 117 | // TimestampString returns the RFC 3339 string for valid Timestamps. For invalid 118 | // Timestamps, it returns an error message in parentheses. 119 | func TimestampString(ts *tspb.Timestamp) string { 120 | t, err := Timestamp(ts) 121 | if err != nil { 122 | return fmt.Sprintf("(%v)", err) 123 | } 124 | return t.Format(time.RFC3339Nano) 125 | } 126 | -------------------------------------------------------------------------------- /ptypes/duration/duration.pb.go: -------------------------------------------------------------------------------- 1 | // Code generated by protoc-gen-go. 2 | // source: github.com/golang/protobuf/ptypes/duration/duration.proto 3 | // DO NOT EDIT! 4 | 5 | /* 6 | Package duration is a generated protocol buffer package. 7 | 8 | It is generated from these files: 9 | github.com/golang/protobuf/ptypes/duration/duration.proto 10 | 11 | It has these top-level messages: 12 | Duration 13 | */ 14 | package duration 15 | 16 | import proto "github.com/golang/protobuf/proto" 17 | import fmt "fmt" 18 | import math "math" 19 | 20 | // Reference imports to suppress errors if they are not otherwise used. 21 | var _ = proto.Marshal 22 | var _ = fmt.Errorf 23 | var _ = math.Inf 24 | 25 | // This is a compile-time assertion to ensure that this generated file 26 | // is compatible with the proto package it is being compiled against. 27 | // A compilation error at this line likely means your copy of the 28 | // proto package needs to be updated. 29 | const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package 30 | 31 | // A Duration represents a signed, fixed-length span of time represented 32 | // as a count of seconds and fractions of seconds at nanosecond 33 | // resolution. It is independent of any calendar and concepts like "day" 34 | // or "month". It is related to Timestamp in that the difference between 35 | // two Timestamp values is a Duration and it can be added or subtracted 36 | // from a Timestamp. Range is approximately +-10,000 years. 37 | // 38 | // Example 1: Compute Duration from two Timestamps in pseudo code. 39 | // 40 | // Timestamp start = ...; 41 | // Timestamp end = ...; 42 | // Duration duration = ...; 43 | // 44 | // duration.seconds = end.seconds - start.seconds; 45 | // duration.nanos = end.nanos - start.nanos; 46 | // 47 | // if (duration.seconds < 0 && duration.nanos > 0) { 48 | // duration.seconds += 1; 49 | // duration.nanos -= 1000000000; 50 | // } else if (durations.seconds > 0 && duration.nanos < 0) { 51 | // duration.seconds -= 1; 52 | // duration.nanos += 1000000000; 53 | // } 54 | // 55 | // Example 2: Compute Timestamp from Timestamp + Duration in pseudo code. 56 | // 57 | // Timestamp start = ...; 58 | // Duration duration = ...; 59 | // Timestamp end = ...; 60 | // 61 | // end.seconds = start.seconds + duration.seconds; 62 | // end.nanos = start.nanos + duration.nanos; 63 | // 64 | // if (end.nanos < 0) { 65 | // end.seconds -= 1; 66 | // end.nanos += 1000000000; 67 | // } else if (end.nanos >= 1000000000) { 68 | // end.seconds += 1; 69 | // end.nanos -= 1000000000; 70 | // } 71 | // 72 | // 73 | type Duration struct { 74 | // Signed seconds of the span of time. Must be from -315,576,000,000 75 | // to +315,576,000,000 inclusive. 76 | Seconds int64 `protobuf:"varint,1,opt,name=seconds" json:"seconds,omitempty"` 77 | // Signed fractions of a second at nanosecond resolution of the span 78 | // of time. Durations less than one second are represented with a 0 79 | // `seconds` field and a positive or negative `nanos` field. For durations 80 | // of one second or more, a non-zero value for the `nanos` field must be 81 | // of the same sign as the `seconds` field. Must be from -999,999,999 82 | // to +999,999,999 inclusive. 83 | Nanos int32 `protobuf:"varint,2,opt,name=nanos" json:"nanos,omitempty"` 84 | } 85 | 86 | func (m *Duration) Reset() { *m = Duration{} } 87 | func (m *Duration) String() string { return proto.CompactTextString(m) } 88 | func (*Duration) ProtoMessage() {} 89 | func (*Duration) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} } 90 | func (*Duration) XXX_WellKnownType() string { return "Duration" } 91 | 92 | func init() { 93 | proto.RegisterType((*Duration)(nil), "google.protobuf.Duration") 94 | } 95 | 96 | func init() { 97 | proto.RegisterFile("github.com/golang/protobuf/ptypes/duration/duration.proto", fileDescriptor0) 98 | } 99 | 100 | var fileDescriptor0 = []byte{ 101 | // 187 bytes of a gzipped FileDescriptorProto 102 | 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0xb2, 0x4c, 0xcf, 0x2c, 0xc9, 103 | 0x28, 0x4d, 0xd2, 0x4b, 0xce, 0xcf, 0xd5, 0x4f, 0xcf, 0xcf, 0x49, 0xcc, 0x4b, 0xd7, 0x2f, 0x28, 104 | 0xca, 0x2f, 0xc9, 0x4f, 0x2a, 0x4d, 0xd3, 0x2f, 0x28, 0xa9, 0x2c, 0x48, 0x2d, 0xd6, 0x4f, 0x29, 105 | 0x2d, 0x4a, 0x2c, 0xc9, 0xcc, 0xcf, 0x83, 0x33, 0xf4, 0xc0, 0x2a, 0x84, 0xf8, 0xd3, 0xf3, 0xf3, 106 | 0xd3, 0x73, 0x52, 0xf5, 0x60, 0xea, 0x95, 0xac, 0xb8, 0x38, 0x5c, 0xa0, 0x4a, 0x84, 0x24, 0xb8, 107 | 0xd8, 0x8b, 0x53, 0x93, 0xf3, 0xf3, 0x52, 0x8a, 0x25, 0x18, 0x15, 0x18, 0x35, 0x98, 0x83, 0x60, 108 | 0x5c, 0x21, 0x11, 0x2e, 0xd6, 0xbc, 0xc4, 0xbc, 0xfc, 0x62, 0x09, 0x26, 0xa0, 0x38, 0x6b, 0x10, 109 | 0x84, 0xe3, 0x54, 0xc3, 0x25, 0x0c, 0x74, 0x82, 0x1e, 0x9a, 0x91, 0x4e, 0xbc, 0x30, 0x03, 0x03, 110 | 0x40, 0x22, 0x01, 0x8c, 0x51, 0x5a, 0xc4, 0xbb, 0x77, 0x01, 0x23, 0xe3, 0x22, 0x26, 0x66, 0xf7, 111 | 0x00, 0xa7, 0x55, 0x4c, 0x72, 0xee, 0x10, 0x73, 0x03, 0xa0, 0x4a, 0xf5, 0xc2, 0x53, 0x73, 0x72, 112 | 0xbc, 0xf3, 0xf2, 0xcb, 0xf3, 0x42, 0x40, 0x5a, 0x92, 0xd8, 0xc0, 0x66, 0x18, 0x03, 0x02, 0x00, 113 | 0x00, 0xff, 0xff, 0x62, 0xfb, 0xb1, 0x51, 0x0e, 0x01, 0x00, 0x00, 114 | } 115 | -------------------------------------------------------------------------------- /protoc-gen-go/testdata/my_test/test.proto: -------------------------------------------------------------------------------- 1 | // Go support for Protocol Buffers - Google's data interchange format 2 | // 3 | // Copyright 2010 The Go Authors. All rights reserved. 4 | // https://github.com/golang/protobuf 5 | // 6 | // Redistribution and use in source and binary forms, with or without 7 | // modification, are permitted provided that the following conditions are 8 | // met: 9 | // 10 | // * Redistributions of source code must retain the above copyright 11 | // notice, this list of conditions and the following disclaimer. 12 | // * Redistributions in binary form must reproduce the above 13 | // copyright notice, this list of conditions and the following disclaimer 14 | // in the documentation and/or other materials provided with the 15 | // distribution. 16 | // * Neither the name of Google Inc. nor the names of its 17 | // contributors may be used to endorse or promote products derived from 18 | // this software without specific prior written permission. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | syntax = "proto2"; 33 | 34 | // This package holds interesting messages. 35 | package my.test; // dotted package name 36 | 37 | //import "imp.proto"; 38 | import "multi/multi1.proto"; // unused import 39 | 40 | enum HatType { 41 | // deliberately skipping 0 42 | FEDORA = 1; 43 | FEZ = 2; 44 | } 45 | 46 | // This enum represents days of the week. 47 | enum Days { 48 | option allow_alias = true; 49 | 50 | MONDAY = 1; 51 | TUESDAY = 2; 52 | LUNDI = 1; // same value as MONDAY 53 | } 54 | 55 | // This is a message that might be sent somewhere. 56 | message Request { 57 | enum Color { 58 | RED = 0; 59 | GREEN = 1; 60 | BLUE = 2; 61 | } 62 | repeated int64 key = 1; 63 | // optional imp.ImportedMessage imported_message = 2; 64 | optional Color hue = 3; // no default 65 | optional HatType hat = 4 [default=FEDORA]; 66 | // optional imp.ImportedMessage.Owner owner = 6; 67 | optional float deadline = 7 [default=inf]; 68 | optional group SomeGroup = 8 { 69 | optional int32 group_field = 9; 70 | } 71 | 72 | // These foreign types are in imp2.proto, 73 | // which is publicly imported by imp.proto. 74 | // optional imp.PubliclyImportedMessage pub = 10; 75 | // optional imp.PubliclyImportedEnum pub_enum = 13 [default=HAIR]; 76 | 77 | 78 | // This is a map field. It will generate map[int32]string. 79 | map name_mapping = 14; 80 | // This is a map field whose value type is a message. 81 | map msg_mapping = 15; 82 | 83 | optional int32 reset = 12; 84 | // This field should not conflict with any getters. 85 | optional string get_key = 16; 86 | } 87 | 88 | message Reply { 89 | message Entry { 90 | required int64 key_that_needs_1234camel_CasIng = 1; 91 | optional int64 value = 2 [default=7]; 92 | optional int64 _my_field_name_2 = 3; 93 | enum Game { 94 | FOOTBALL = 1; 95 | TENNIS = 2; 96 | } 97 | } 98 | repeated Entry found = 1; 99 | repeated int32 compact_keys = 2 [packed=true]; 100 | extensions 100 to max; 101 | } 102 | 103 | message OtherBase { 104 | optional string name = 1; 105 | extensions 100 to max; 106 | } 107 | 108 | message ReplyExtensions { 109 | extend Reply { 110 | optional double time = 101; 111 | optional ReplyExtensions carrot = 105; 112 | } 113 | extend OtherBase { 114 | optional ReplyExtensions donut = 101; 115 | } 116 | } 117 | 118 | message OtherReplyExtensions { 119 | optional int32 key = 1; 120 | } 121 | 122 | // top-level extension 123 | extend Reply { 124 | optional string tag = 103; 125 | optional OtherReplyExtensions donut = 106; 126 | // optional imp.ImportedMessage elephant = 107; // extend with message from another file. 127 | } 128 | 129 | message OldReply { 130 | // Extensions will be encoded in MessageSet wire format. 131 | option message_set_wire_format = true; 132 | extensions 100 to max; 133 | } 134 | 135 | message Communique { 136 | optional bool make_me_cry = 1; 137 | 138 | // This is a oneof, called "union". 139 | oneof union { 140 | int32 number = 5; 141 | string name = 6; 142 | bytes data = 7; 143 | double temp_c = 8; 144 | float height = 9; 145 | Days today = 10; 146 | bool maybe = 11; 147 | sint32 delta = 12; // name will conflict with Delta below 148 | Reply msg = 13; 149 | group SomeGroup = 14 { 150 | optional string member = 15; 151 | } 152 | } 153 | 154 | message Delta {} 155 | } 156 | 157 | -------------------------------------------------------------------------------- /ptypes/any.go: -------------------------------------------------------------------------------- 1 | // Go support for Protocol Buffers - Google's data interchange format 2 | // 3 | // Copyright 2016 The Go Authors. All rights reserved. 4 | // https://github.com/golang/protobuf 5 | // 6 | // Redistribution and use in source and binary forms, with or without 7 | // modification, are permitted provided that the following conditions are 8 | // met: 9 | // 10 | // * Redistributions of source code must retain the above copyright 11 | // notice, this list of conditions and the following disclaimer. 12 | // * Redistributions in binary form must reproduce the above 13 | // copyright notice, this list of conditions and the following disclaimer 14 | // in the documentation and/or other materials provided with the 15 | // distribution. 16 | // * Neither the name of Google Inc. nor the names of its 17 | // contributors may be used to endorse or promote products derived from 18 | // this software without specific prior written permission. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | package ptypes 33 | 34 | // This file implements functions to marshal proto.Message to/from 35 | // google.protobuf.Any message. 36 | 37 | import ( 38 | "fmt" 39 | "reflect" 40 | "strings" 41 | 42 | "github.com/golang/protobuf/proto" 43 | "github.com/golang/protobuf/ptypes/any" 44 | ) 45 | 46 | const googleApis = "type.googleapis.com/" 47 | 48 | // AnyMessageName returns the name of the message contained in a google.protobuf.Any message. 49 | // 50 | // Note that regular type assertions should be done using the Is 51 | // function. AnyMessageName is provided for less common use cases like filtering a 52 | // sequence of Any messages based on a set of allowed message type names. 53 | func AnyMessageName(any *any.Any) (string, error) { 54 | slash := strings.LastIndex(any.TypeUrl, "/") 55 | if slash < 0 { 56 | return "", fmt.Errorf("message type url %q is invalid", any.TypeUrl) 57 | } 58 | return any.TypeUrl[slash+1:], nil 59 | } 60 | 61 | // MarshalAny takes the protocol buffer and encodes it into google.protobuf.Any. 62 | func MarshalAny(pb proto.Message) (*any.Any, error) { 63 | value, err := proto.Marshal(pb) 64 | if err != nil { 65 | return nil, err 66 | } 67 | return &any.Any{TypeUrl: googleApis + proto.MessageName(pb), Value: value}, nil 68 | } 69 | 70 | // DynamicAny is a value that can be passed to UnmarshalAny to automatically 71 | // allocate a proto.Message for the type specified in a google.protobuf.Any 72 | // message. The allocated message is stored in the embedded proto.Message. 73 | // 74 | // Example: 75 | // 76 | // var x ptypes.DynamicAny 77 | // if err := ptypes.UnmarshalAny(a, &x); err != nil { ... } 78 | // fmt.Printf("unmarshaled message: %v", x.Message) 79 | type DynamicAny struct { 80 | proto.Message 81 | } 82 | 83 | // Empty returns a new proto.Message of the type specified in a 84 | // google.protobuf.Any message. It returns an error if corresponding message 85 | // type isn't linked in. 86 | func Empty(any *any.Any) (proto.Message, error) { 87 | aname, err := AnyMessageName(any) 88 | if err != nil { 89 | return nil, err 90 | } 91 | 92 | t := proto.MessageType(aname) 93 | if t == nil { 94 | return nil, fmt.Errorf("any: message type %q isn't linked in", aname) 95 | } 96 | return reflect.New(t.Elem()).Interface().(proto.Message), nil 97 | } 98 | 99 | // UnmarshalAny parses the protocol buffer representation in a google.protobuf.Any 100 | // message and places the decoded result in pb. It returns an error if type of 101 | // contents of Any message does not match type of pb message. 102 | // 103 | // pb can be a proto.Message, or a *DynamicAny. 104 | func UnmarshalAny(any *any.Any, pb proto.Message) error { 105 | if d, ok := pb.(*DynamicAny); ok { 106 | if d.Message == nil { 107 | var err error 108 | d.Message, err = Empty(any) 109 | if err != nil { 110 | return err 111 | } 112 | } 113 | return UnmarshalAny(any, d.Message) 114 | } 115 | 116 | aname, err := AnyMessageName(any) 117 | if err != nil { 118 | return err 119 | } 120 | 121 | mname := proto.MessageName(pb) 122 | if aname != mname { 123 | return fmt.Errorf("mismatched message type: got %q want %q", aname, mname) 124 | } 125 | return proto.Unmarshal(any.Value, pb) 126 | } 127 | 128 | // Is returns true if any value contains a given message type. 129 | func Is(any *any.Any, pb proto.Message) bool { 130 | aname, err := AnyMessageName(any) 131 | if err != nil { 132 | return false 133 | } 134 | 135 | return aname == proto.MessageName(pb) 136 | } 137 | -------------------------------------------------------------------------------- /ptypes/duration_test.go: -------------------------------------------------------------------------------- 1 | // Go support for Protocol Buffers - Google's data interchange format 2 | // 3 | // Copyright 2016 The Go Authors. All rights reserved. 4 | // https://github.com/golang/protobuf 5 | // 6 | // Redistribution and use in source and binary forms, with or without 7 | // modification, are permitted provided that the following conditions are 8 | // met: 9 | // 10 | // * Redistributions of source code must retain the above copyright 11 | // notice, this list of conditions and the following disclaimer. 12 | // * Redistributions in binary form must reproduce the above 13 | // copyright notice, this list of conditions and the following disclaimer 14 | // in the documentation and/or other materials provided with the 15 | // distribution. 16 | // * Neither the name of Google Inc. nor the names of its 17 | // contributors may be used to endorse or promote products derived from 18 | // this software without specific prior written permission. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | package ptypes 33 | 34 | import ( 35 | "math" 36 | "testing" 37 | "time" 38 | 39 | "github.com/golang/protobuf/proto" 40 | durpb "github.com/golang/protobuf/ptypes/duration" 41 | ) 42 | 43 | const ( 44 | minGoSeconds = math.MinInt64 / int64(1e9) 45 | maxGoSeconds = math.MaxInt64 / int64(1e9) 46 | ) 47 | 48 | var durationTests = []struct { 49 | proto *durpb.Duration 50 | isValid bool 51 | inRange bool 52 | dur time.Duration 53 | }{ 54 | // The zero duration. 55 | {&durpb.Duration{0, 0}, true, true, 0}, 56 | // Some ordinary non-zero durations. 57 | {&durpb.Duration{100, 0}, true, true, 100 * time.Second}, 58 | {&durpb.Duration{-100, 0}, true, true, -100 * time.Second}, 59 | {&durpb.Duration{100, 987}, true, true, 100*time.Second + 987}, 60 | {&durpb.Duration{-100, -987}, true, true, -(100*time.Second + 987)}, 61 | // The largest duration representable in Go. 62 | {&durpb.Duration{maxGoSeconds, int32(math.MaxInt64 - 1e9*maxGoSeconds)}, true, true, math.MaxInt64}, 63 | // The smallest duration representable in Go. 64 | {&durpb.Duration{minGoSeconds, int32(math.MinInt64 - 1e9*minGoSeconds)}, true, true, math.MinInt64}, 65 | {nil, false, false, 0}, 66 | {&durpb.Duration{-100, 987}, false, false, 0}, 67 | {&durpb.Duration{100, -987}, false, false, 0}, 68 | {&durpb.Duration{math.MinInt64, 0}, false, false, 0}, 69 | {&durpb.Duration{math.MaxInt64, 0}, false, false, 0}, 70 | // The largest valid duration. 71 | {&durpb.Duration{maxSeconds, 1e9 - 1}, true, false, 0}, 72 | // The smallest valid duration. 73 | {&durpb.Duration{minSeconds, -(1e9 - 1)}, true, false, 0}, 74 | // The smallest invalid duration above the valid range. 75 | {&durpb.Duration{maxSeconds + 1, 0}, false, false, 0}, 76 | // The largest invalid duration below the valid range. 77 | {&durpb.Duration{minSeconds - 1, -(1e9 - 1)}, false, false, 0}, 78 | // One nanosecond past the largest duration representable in Go. 79 | {&durpb.Duration{maxGoSeconds, int32(math.MaxInt64-1e9*maxGoSeconds) + 1}, true, false, 0}, 80 | // One nanosecond past the smallest duration representable in Go. 81 | {&durpb.Duration{minGoSeconds, int32(math.MinInt64-1e9*minGoSeconds) - 1}, true, false, 0}, 82 | // One second past the largest duration representable in Go. 83 | {&durpb.Duration{maxGoSeconds + 1, int32(math.MaxInt64 - 1e9*maxGoSeconds)}, true, false, 0}, 84 | // One second past the smallest duration representable in Go. 85 | {&durpb.Duration{minGoSeconds - 1, int32(math.MinInt64 - 1e9*minGoSeconds)}, true, false, 0}, 86 | } 87 | 88 | func TestValidateDuration(t *testing.T) { 89 | for _, test := range durationTests { 90 | err := validateDuration(test.proto) 91 | gotValid := (err == nil) 92 | if gotValid != test.isValid { 93 | t.Errorf("validateDuration(%v) = %t, want %t", test.proto, gotValid, test.isValid) 94 | } 95 | } 96 | } 97 | 98 | func TestDuration(t *testing.T) { 99 | for _, test := range durationTests { 100 | got, err := Duration(test.proto) 101 | gotOK := (err == nil) 102 | wantOK := test.isValid && test.inRange 103 | if gotOK != wantOK { 104 | t.Errorf("Duration(%v) ok = %t, want %t", test.proto, gotOK, wantOK) 105 | } 106 | if err == nil && got != test.dur { 107 | t.Errorf("Duration(%v) = %v, want %v", test.proto, got, test.dur) 108 | } 109 | } 110 | } 111 | 112 | func TestDurationProto(t *testing.T) { 113 | for _, test := range durationTests { 114 | if test.isValid && test.inRange { 115 | got := DurationProto(test.dur) 116 | if !proto.Equal(got, test.proto) { 117 | t.Errorf("DurationProto(%v) = %v, want %v", test.dur, got, test.proto) 118 | } 119 | } 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /ptypes/any/any.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | syntax = "proto3"; 32 | 33 | package google.protobuf; 34 | 35 | option csharp_namespace = "Google.Protobuf.WellKnownTypes"; 36 | option go_package = "github.com/golang/protobuf/ptypes/any"; 37 | option java_package = "com.google.protobuf"; 38 | option java_outer_classname = "AnyProto"; 39 | option java_multiple_files = true; 40 | option java_generate_equals_and_hash = true; 41 | option objc_class_prefix = "GPB"; 42 | 43 | // `Any` contains an arbitrary serialized protocol buffer message along with a 44 | // URL that describes the type of the serialized message. 45 | // 46 | // Protobuf library provides support to pack/unpack Any values in the form 47 | // of utility functions or additional generated methods of the Any type. 48 | // 49 | // Example 1: Pack and unpack a message in C++. 50 | // 51 | // Foo foo = ...; 52 | // Any any; 53 | // any.PackFrom(foo); 54 | // ... 55 | // if (any.UnpackTo(&foo)) { 56 | // ... 57 | // } 58 | // 59 | // Example 2: Pack and unpack a message in Java. 60 | // 61 | // Foo foo = ...; 62 | // Any any = Any.pack(foo); 63 | // ... 64 | // if (any.is(Foo.class)) { 65 | // foo = any.unpack(Foo.class); 66 | // } 67 | // 68 | // The pack methods provided by protobuf library will by default use 69 | // 'type.googleapis.com/full.type.name' as the type URL and the unpack 70 | // methods only use the fully qualified type name after the last '/' 71 | // in the type URL, for example "foo.bar.com/x/y.z" will yield type 72 | // name "y.z". 73 | // 74 | // 75 | // JSON 76 | // ==== 77 | // The JSON representation of an `Any` value uses the regular 78 | // representation of the deserialized, embedded message, with an 79 | // additional field `@type` which contains the type URL. Example: 80 | // 81 | // package google.profile; 82 | // message Person { 83 | // string first_name = 1; 84 | // string last_name = 2; 85 | // } 86 | // 87 | // { 88 | // "@type": "type.googleapis.com/google.profile.Person", 89 | // "firstName": , 90 | // "lastName": 91 | // } 92 | // 93 | // If the embedded message type is well-known and has a custom JSON 94 | // representation, that representation will be embedded adding a field 95 | // `value` which holds the custom JSON in addition to the `@type` 96 | // field. Example (for message [google.protobuf.Duration][]): 97 | // 98 | // { 99 | // "@type": "type.googleapis.com/google.protobuf.Duration", 100 | // "value": "1.212s" 101 | // } 102 | // 103 | message Any { 104 | // A URL/resource name whose content describes the type of the 105 | // serialized protocol buffer message. 106 | // 107 | // For URLs which use the schema `http`, `https`, or no schema, the 108 | // following restrictions and interpretations apply: 109 | // 110 | // * If no schema is provided, `https` is assumed. 111 | // * The last segment of the URL's path must represent the fully 112 | // qualified name of the type (as in `path/google.protobuf.Duration`). 113 | // The name should be in a canonical form (e.g., leading "." is 114 | // not accepted). 115 | // * An HTTP GET on the URL must yield a [google.protobuf.Type][] 116 | // value in binary format, or produce an error. 117 | // * Applications are allowed to cache lookup results based on the 118 | // URL, or have them precompiled into a binary to avoid any 119 | // lookup. Therefore, binary compatibility needs to be preserved 120 | // on changes to types. (Use versioned type names to manage 121 | // breaking changes.) 122 | // 123 | // Schemas other than `http`, `https` (or the empty schema) might be 124 | // used with implementation specific semantics. 125 | // 126 | string type_url = 1; 127 | 128 | // Must be a valid serialized protocol buffer of the above specified type. 129 | bytes value = 2; 130 | } 131 | -------------------------------------------------------------------------------- /ptypes/timestamp_test.go: -------------------------------------------------------------------------------- 1 | // Go support for Protocol Buffers - Google's data interchange format 2 | // 3 | // Copyright 2016 The Go Authors. All rights reserved. 4 | // https://github.com/golang/protobuf 5 | // 6 | // Redistribution and use in source and binary forms, with or without 7 | // modification, are permitted provided that the following conditions are 8 | // met: 9 | // 10 | // * Redistributions of source code must retain the above copyright 11 | // notice, this list of conditions and the following disclaimer. 12 | // * Redistributions in binary form must reproduce the above 13 | // copyright notice, this list of conditions and the following disclaimer 14 | // in the documentation and/or other materials provided with the 15 | // distribution. 16 | // * Neither the name of Google Inc. nor the names of its 17 | // contributors may be used to endorse or promote products derived from 18 | // this software without specific prior written permission. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | package ptypes 33 | 34 | import ( 35 | "math" 36 | "testing" 37 | "time" 38 | 39 | "github.com/golang/protobuf/proto" 40 | tspb "github.com/golang/protobuf/ptypes/timestamp" 41 | ) 42 | 43 | var tests = []struct { 44 | ts *tspb.Timestamp 45 | valid bool 46 | t time.Time 47 | }{ 48 | // The timestamp representing the Unix epoch date. 49 | {&tspb.Timestamp{0, 0}, true, utcDate(1970, 1, 1)}, 50 | // The smallest representable timestamp. 51 | {&tspb.Timestamp{math.MinInt64, math.MinInt32}, false, 52 | time.Unix(math.MinInt64, math.MinInt32).UTC()}, 53 | // The smallest representable timestamp with non-negative nanos. 54 | {&tspb.Timestamp{math.MinInt64, 0}, false, time.Unix(math.MinInt64, 0).UTC()}, 55 | // The earliest valid timestamp. 56 | {&tspb.Timestamp{minValidSeconds, 0}, true, utcDate(1, 1, 1)}, 57 | //"0001-01-01T00:00:00Z"}, 58 | // The largest representable timestamp. 59 | {&tspb.Timestamp{math.MaxInt64, math.MaxInt32}, false, 60 | time.Unix(math.MaxInt64, math.MaxInt32).UTC()}, 61 | // The largest representable timestamp with nanos in range. 62 | {&tspb.Timestamp{math.MaxInt64, 1e9 - 1}, false, 63 | time.Unix(math.MaxInt64, 1e9-1).UTC()}, 64 | // The largest valid timestamp. 65 | {&tspb.Timestamp{maxValidSeconds - 1, 1e9 - 1}, true, 66 | time.Date(9999, 12, 31, 23, 59, 59, 1e9-1, time.UTC)}, 67 | // The smallest invalid timestamp that is larger than the valid range. 68 | {&tspb.Timestamp{maxValidSeconds, 0}, false, time.Unix(maxValidSeconds, 0).UTC()}, 69 | // A date before the epoch. 70 | {&tspb.Timestamp{-281836800, 0}, true, utcDate(1961, 1, 26)}, 71 | // A date after the epoch. 72 | {&tspb.Timestamp{1296000000, 0}, true, utcDate(2011, 1, 26)}, 73 | // A date after the epoch, in the middle of the day. 74 | {&tspb.Timestamp{1296012345, 940483}, true, 75 | time.Date(2011, 1, 26, 3, 25, 45, 940483, time.UTC)}, 76 | } 77 | 78 | func TestValidateTimestamp(t *testing.T) { 79 | for _, s := range tests { 80 | got := validateTimestamp(s.ts) 81 | if (got == nil) != s.valid { 82 | t.Errorf("validateTimestamp(%v) = %v, want %v", s.ts, got, s.valid) 83 | } 84 | } 85 | } 86 | 87 | func TestTimestamp(t *testing.T) { 88 | for _, s := range tests { 89 | got, err := Timestamp(s.ts) 90 | if (err == nil) != s.valid { 91 | t.Errorf("Timestamp(%v) error = %v, but valid = %t", s.ts, err, s.valid) 92 | } else if s.valid && got != s.t { 93 | t.Errorf("Timestamp(%v) = %v, want %v", s.ts, got, s.t) 94 | } 95 | } 96 | // Special case: a nil Timestamp is an error, but returns the 0 Unix time. 97 | got, err := Timestamp(nil) 98 | want := time.Unix(0, 0).UTC() 99 | if got != want { 100 | t.Errorf("Timestamp(nil) = %v, want %v", got, want) 101 | } 102 | if err == nil { 103 | t.Errorf("Timestamp(nil) error = nil, expected error") 104 | } 105 | } 106 | 107 | func TestTimestampProto(t *testing.T) { 108 | for _, s := range tests { 109 | got, err := TimestampProto(s.t) 110 | if (err == nil) != s.valid { 111 | t.Errorf("TimestampProto(%v) error = %v, but valid = %t", s.t, err, s.valid) 112 | } else if s.valid && !proto.Equal(got, s.ts) { 113 | t.Errorf("TimestampProto(%v) = %v, want %v", s.t, got, s.ts) 114 | } 115 | } 116 | // No corresponding special case here: no time.Time results in a nil Timestamp. 117 | } 118 | 119 | func TestTimestampString(t *testing.T) { 120 | for _, test := range []struct { 121 | ts *tspb.Timestamp 122 | want string 123 | }{ 124 | // Not much testing needed because presumably time.Format is 125 | // well-tested. 126 | {&tspb.Timestamp{0, 0}, "1970-01-01T00:00:00Z"}, 127 | {&tspb.Timestamp{minValidSeconds - 1, 0}, "(timestamp: seconds:-62135596801 before 0001-01-01)"}, 128 | } { 129 | got := TimestampString(test.ts) 130 | if got != test.want { 131 | t.Errorf("TimestampString(%v) = %q, want %q", test.ts, got, test.want) 132 | } 133 | } 134 | } 135 | 136 | func utcDate(year, month, day int) time.Time { 137 | return time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC) 138 | } 139 | -------------------------------------------------------------------------------- /ptypes/timestamp/timestamp.pb.go: -------------------------------------------------------------------------------- 1 | // Code generated by protoc-gen-go. 2 | // source: github.com/golang/protobuf/ptypes/timestamp/timestamp.proto 3 | // DO NOT EDIT! 4 | 5 | /* 6 | Package timestamp is a generated protocol buffer package. 7 | 8 | It is generated from these files: 9 | github.com/golang/protobuf/ptypes/timestamp/timestamp.proto 10 | 11 | It has these top-level messages: 12 | Timestamp 13 | */ 14 | package timestamp 15 | 16 | import proto "github.com/golang/protobuf/proto" 17 | import fmt "fmt" 18 | import math "math" 19 | 20 | // Reference imports to suppress errors if they are not otherwise used. 21 | var _ = proto.Marshal 22 | var _ = fmt.Errorf 23 | var _ = math.Inf 24 | 25 | // This is a compile-time assertion to ensure that this generated file 26 | // is compatible with the proto package it is being compiled against. 27 | // A compilation error at this line likely means your copy of the 28 | // proto package needs to be updated. 29 | const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package 30 | 31 | // A Timestamp represents a point in time independent of any time zone 32 | // or calendar, represented as seconds and fractions of seconds at 33 | // nanosecond resolution in UTC Epoch time. It is encoded using the 34 | // Proleptic Gregorian Calendar which extends the Gregorian calendar 35 | // backwards to year one. It is encoded assuming all minutes are 60 36 | // seconds long, i.e. leap seconds are "smeared" so that no leap second 37 | // table is needed for interpretation. Range is from 38 | // 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z. 39 | // By restricting to that range, we ensure that we can convert to 40 | // and from RFC 3339 date strings. 41 | // See [https://www.ietf.org/rfc/rfc3339.txt](https://www.ietf.org/rfc/rfc3339.txt). 42 | // 43 | // Example 1: Compute Timestamp from POSIX `time()`. 44 | // 45 | // Timestamp timestamp; 46 | // timestamp.set_seconds(time(NULL)); 47 | // timestamp.set_nanos(0); 48 | // 49 | // Example 2: Compute Timestamp from POSIX `gettimeofday()`. 50 | // 51 | // struct timeval tv; 52 | // gettimeofday(&tv, NULL); 53 | // 54 | // Timestamp timestamp; 55 | // timestamp.set_seconds(tv.tv_sec); 56 | // timestamp.set_nanos(tv.tv_usec * 1000); 57 | // 58 | // Example 3: Compute Timestamp from Win32 `GetSystemTimeAsFileTime()`. 59 | // 60 | // FILETIME ft; 61 | // GetSystemTimeAsFileTime(&ft); 62 | // UINT64 ticks = (((UINT64)ft.dwHighDateTime) << 32) | ft.dwLowDateTime; 63 | // 64 | // // A Windows tick is 100 nanoseconds. Windows epoch 1601-01-01T00:00:00Z 65 | // // is 11644473600 seconds before Unix epoch 1970-01-01T00:00:00Z. 66 | // Timestamp timestamp; 67 | // timestamp.set_seconds((INT64) ((ticks / 10000000) - 11644473600LL)); 68 | // timestamp.set_nanos((INT32) ((ticks % 10000000) * 100)); 69 | // 70 | // Example 4: Compute Timestamp from Java `System.currentTimeMillis()`. 71 | // 72 | // long millis = System.currentTimeMillis(); 73 | // 74 | // Timestamp timestamp = Timestamp.newBuilder().setSeconds(millis / 1000) 75 | // .setNanos((int) ((millis % 1000) * 1000000)).build(); 76 | // 77 | // 78 | // Example 5: Compute Timestamp from current time in Python. 79 | // 80 | // now = time.time() 81 | // seconds = int(now) 82 | // nanos = int((now - seconds) * 10**9) 83 | // timestamp = Timestamp(seconds=seconds, nanos=nanos) 84 | // 85 | // 86 | type Timestamp struct { 87 | // Represents seconds of UTC time since Unix epoch 88 | // 1970-01-01T00:00:00Z. Must be from from 0001-01-01T00:00:00Z to 89 | // 9999-12-31T23:59:59Z inclusive. 90 | Seconds int64 `protobuf:"varint,1,opt,name=seconds" json:"seconds,omitempty"` 91 | // Non-negative fractions of a second at nanosecond resolution. Negative 92 | // second values with fractions must still have non-negative nanos values 93 | // that count forward in time. Must be from 0 to 999,999,999 94 | // inclusive. 95 | Nanos int32 `protobuf:"varint,2,opt,name=nanos" json:"nanos,omitempty"` 96 | } 97 | 98 | func (m *Timestamp) Reset() { *m = Timestamp{} } 99 | func (m *Timestamp) String() string { return proto.CompactTextString(m) } 100 | func (*Timestamp) ProtoMessage() {} 101 | func (*Timestamp) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} } 102 | func (*Timestamp) XXX_WellKnownType() string { return "Timestamp" } 103 | 104 | func init() { 105 | proto.RegisterType((*Timestamp)(nil), "google.protobuf.Timestamp") 106 | } 107 | 108 | func init() { 109 | proto.RegisterFile("github.com/golang/protobuf/ptypes/timestamp/timestamp.proto", fileDescriptor0) 110 | } 111 | 112 | var fileDescriptor0 = []byte{ 113 | // 192 bytes of a gzipped FileDescriptorProto 114 | 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0xb2, 0x4e, 0xcf, 0x2c, 0xc9, 115 | 0x28, 0x4d, 0xd2, 0x4b, 0xce, 0xcf, 0xd5, 0x4f, 0xcf, 0xcf, 0x49, 0xcc, 0x4b, 0xd7, 0x2f, 0x28, 116 | 0xca, 0x2f, 0xc9, 0x4f, 0x2a, 0x4d, 0xd3, 0x2f, 0x28, 0xa9, 0x2c, 0x48, 0x2d, 0xd6, 0x2f, 0xc9, 117 | 0xcc, 0x4d, 0x2d, 0x2e, 0x49, 0xcc, 0x2d, 0x40, 0xb0, 0xf4, 0xc0, 0x6a, 0x84, 0xf8, 0xd3, 0xf3, 118 | 0xf3, 0xd3, 0x73, 0x52, 0xf5, 0x60, 0x3a, 0x94, 0xac, 0xb9, 0x38, 0x43, 0x60, 0x6a, 0x84, 0x24, 119 | 0xb8, 0xd8, 0x8b, 0x53, 0x93, 0xf3, 0xf3, 0x52, 0x8a, 0x25, 0x18, 0x15, 0x18, 0x35, 0x98, 0x83, 120 | 0x60, 0x5c, 0x21, 0x11, 0x2e, 0xd6, 0xbc, 0xc4, 0xbc, 0xfc, 0x62, 0x09, 0x26, 0xa0, 0x38, 0x6b, 121 | 0x10, 0x84, 0xe3, 0xd4, 0xc8, 0xc8, 0x25, 0x0c, 0x74, 0x86, 0x1e, 0x9a, 0xa1, 0x4e, 0x7c, 0x70, 122 | 0x23, 0x03, 0x40, 0x42, 0x01, 0x8c, 0x51, 0xda, 0x24, 0x38, 0x7a, 0x01, 0x23, 0xe3, 0x0f, 0x46, 123 | 0xc6, 0x45, 0x4c, 0xcc, 0xee, 0x01, 0x4e, 0xab, 0x98, 0xe4, 0xdc, 0x21, 0x86, 0x07, 0x40, 0x95, 124 | 0xeb, 0x85, 0xa7, 0xe6, 0xe4, 0x78, 0xe7, 0xe5, 0x97, 0xe7, 0x85, 0x80, 0xb4, 0x25, 0xb1, 0x81, 125 | 0xcd, 0x31, 0x06, 0x04, 0x00, 0x00, 0xff, 0xff, 0x17, 0x5f, 0xb7, 0xdc, 0x17, 0x01, 0x00, 0x00, 126 | } 127 | -------------------------------------------------------------------------------- /ptypes/any/any.pb.go: -------------------------------------------------------------------------------- 1 | // Code generated by protoc-gen-go. 2 | // source: github.com/golang/protobuf/ptypes/any/any.proto 3 | // DO NOT EDIT! 4 | 5 | /* 6 | Package any is a generated protocol buffer package. 7 | 8 | It is generated from these files: 9 | github.com/golang/protobuf/ptypes/any/any.proto 10 | 11 | It has these top-level messages: 12 | Any 13 | */ 14 | package any 15 | 16 | import proto "github.com/golang/protobuf/proto" 17 | import fmt "fmt" 18 | import math "math" 19 | 20 | // Reference imports to suppress errors if they are not otherwise used. 21 | var _ = proto.Marshal 22 | var _ = fmt.Errorf 23 | var _ = math.Inf 24 | 25 | // This is a compile-time assertion to ensure that this generated file 26 | // is compatible with the proto package it is being compiled against. 27 | // A compilation error at this line likely means your copy of the 28 | // proto package needs to be updated. 29 | const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package 30 | 31 | // `Any` contains an arbitrary serialized protocol buffer message along with a 32 | // URL that describes the type of the serialized message. 33 | // 34 | // Protobuf library provides support to pack/unpack Any values in the form 35 | // of utility functions or additional generated methods of the Any type. 36 | // 37 | // Example 1: Pack and unpack a message in C++. 38 | // 39 | // Foo foo = ...; 40 | // Any any; 41 | // any.PackFrom(foo); 42 | // ... 43 | // if (any.UnpackTo(&foo)) { 44 | // ... 45 | // } 46 | // 47 | // Example 2: Pack and unpack a message in Java. 48 | // 49 | // Foo foo = ...; 50 | // Any any = Any.pack(foo); 51 | // ... 52 | // if (any.is(Foo.class)) { 53 | // foo = any.unpack(Foo.class); 54 | // } 55 | // 56 | // The pack methods provided by protobuf library will by default use 57 | // 'type.googleapis.com/full.type.name' as the type URL and the unpack 58 | // methods only use the fully qualified type name after the last '/' 59 | // in the type URL, for example "foo.bar.com/x/y.z" will yield type 60 | // name "y.z". 61 | // 62 | // 63 | // JSON 64 | // ==== 65 | // The JSON representation of an `Any` value uses the regular 66 | // representation of the deserialized, embedded message, with an 67 | // additional field `@type` which contains the type URL. Example: 68 | // 69 | // package google.profile; 70 | // message Person { 71 | // string first_name = 1; 72 | // string last_name = 2; 73 | // } 74 | // 75 | // { 76 | // "@type": "type.googleapis.com/google.profile.Person", 77 | // "firstName": , 78 | // "lastName": 79 | // } 80 | // 81 | // If the embedded message type is well-known and has a custom JSON 82 | // representation, that representation will be embedded adding a field 83 | // `value` which holds the custom JSON in addition to the `@type` 84 | // field. Example (for message [google.protobuf.Duration][]): 85 | // 86 | // { 87 | // "@type": "type.googleapis.com/google.protobuf.Duration", 88 | // "value": "1.212s" 89 | // } 90 | // 91 | type Any struct { 92 | // A URL/resource name whose content describes the type of the 93 | // serialized protocol buffer message. 94 | // 95 | // For URLs which use the schema `http`, `https`, or no schema, the 96 | // following restrictions and interpretations apply: 97 | // 98 | // * If no schema is provided, `https` is assumed. 99 | // * The last segment of the URL's path must represent the fully 100 | // qualified name of the type (as in `path/google.protobuf.Duration`). 101 | // The name should be in a canonical form (e.g., leading "." is 102 | // not accepted). 103 | // * An HTTP GET on the URL must yield a [google.protobuf.Type][] 104 | // value in binary format, or produce an error. 105 | // * Applications are allowed to cache lookup results based on the 106 | // URL, or have them precompiled into a binary to avoid any 107 | // lookup. Therefore, binary compatibility needs to be preserved 108 | // on changes to types. (Use versioned type names to manage 109 | // breaking changes.) 110 | // 111 | // Schemas other than `http`, `https` (or the empty schema) might be 112 | // used with implementation specific semantics. 113 | // 114 | TypeUrl string `protobuf:"bytes,1,opt,name=type_url,json=typeUrl" json:"type_url,omitempty"` 115 | // Must be a valid serialized protocol buffer of the above specified type. 116 | Value []byte `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty"` 117 | } 118 | 119 | func (m *Any) Reset() { *m = Any{} } 120 | func (m *Any) String() string { return proto.CompactTextString(m) } 121 | func (*Any) ProtoMessage() {} 122 | func (*Any) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} } 123 | func (*Any) XXX_WellKnownType() string { return "Any" } 124 | 125 | func init() { 126 | proto.RegisterType((*Any)(nil), "google.protobuf.Any") 127 | } 128 | 129 | func init() { proto.RegisterFile("github.com/golang/protobuf/ptypes/any/any.proto", fileDescriptor0) } 130 | 131 | var fileDescriptor0 = []byte{ 132 | // 184 bytes of a gzipped FileDescriptorProto 133 | 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0xd2, 0x4f, 0xcf, 0x2c, 0xc9, 134 | 0x28, 0x4d, 0xd2, 0x4b, 0xce, 0xcf, 0xd5, 0x4f, 0xcf, 0xcf, 0x49, 0xcc, 0x4b, 0xd7, 0x2f, 0x28, 135 | 0xca, 0x2f, 0xc9, 0x4f, 0x2a, 0x4d, 0xd3, 0x2f, 0x28, 0xa9, 0x2c, 0x48, 0x2d, 0xd6, 0x4f, 0xcc, 136 | 0xab, 0x04, 0x61, 0x3d, 0xb0, 0xb8, 0x10, 0x7f, 0x7a, 0x7e, 0x7e, 0x7a, 0x4e, 0xaa, 0x1e, 0x4c, 137 | 0x95, 0x92, 0x19, 0x17, 0xb3, 0x63, 0x5e, 0xa5, 0x90, 0x24, 0x17, 0x07, 0x48, 0x79, 0x7c, 0x69, 138 | 0x51, 0x8e, 0x04, 0xa3, 0x02, 0xa3, 0x06, 0x67, 0x10, 0x3b, 0x88, 0x1f, 0x5a, 0x94, 0x23, 0x24, 139 | 0xc2, 0xc5, 0x5a, 0x96, 0x98, 0x53, 0x9a, 0x2a, 0xc1, 0x04, 0x14, 0xe7, 0x09, 0x82, 0x70, 0x9c, 140 | 0x8a, 0xb8, 0x84, 0x81, 0x96, 0xea, 0xa1, 0x19, 0xe7, 0xc4, 0x01, 0x34, 0x2c, 0x00, 0xc4, 0x09, 141 | 0x60, 0x8c, 0x52, 0x25, 0xca, 0x71, 0x0b, 0x18, 0x19, 0x17, 0x31, 0x31, 0xbb, 0x07, 0x38, 0xad, 142 | 0x62, 0x92, 0x73, 0x87, 0x98, 0x16, 0x00, 0x55, 0xa5, 0x17, 0x9e, 0x9a, 0x93, 0xe3, 0x9d, 0x97, 143 | 0x5f, 0x9e, 0x17, 0x02, 0x52, 0x9d, 0xc4, 0x06, 0xd6, 0x6e, 0x0c, 0x08, 0x00, 0x00, 0xff, 0xff, 144 | 0xc6, 0x4d, 0x03, 0x23, 0xf6, 0x00, 0x00, 0x00, 145 | } 146 | -------------------------------------------------------------------------------- /jsonpb/jsonpb_test_proto/more_test_objects.pb.go: -------------------------------------------------------------------------------- 1 | // Code generated by protoc-gen-go. 2 | // source: more_test_objects.proto 3 | // DO NOT EDIT! 4 | 5 | /* 6 | Package jsonpb is a generated protocol buffer package. 7 | 8 | It is generated from these files: 9 | more_test_objects.proto 10 | test_objects.proto 11 | 12 | It has these top-level messages: 13 | Simple3 14 | Mappy 15 | Simple 16 | Repeats 17 | Widget 18 | Maps 19 | MsgWithOneof 20 | Real 21 | Complex 22 | MsgWithListVal 23 | KnownTypes 24 | */ 25 | package jsonpb 26 | 27 | import proto "github.com/golang/protobuf/proto" 28 | import fmt "fmt" 29 | import math "math" 30 | 31 | // Reference imports to suppress errors if they are not otherwise used. 32 | var _ = proto.Marshal 33 | var _ = fmt.Errorf 34 | var _ = math.Inf 35 | 36 | // This is a compile-time assertion to ensure that this generated file 37 | // is compatible with the proto package it is being compiled against. 38 | // A compilation error at this line likely means your copy of the 39 | // proto package needs to be updated. 40 | const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package 41 | 42 | type Numeral int32 43 | 44 | const ( 45 | Numeral_UNKNOWN Numeral = 0 46 | Numeral_ARABIC Numeral = 1 47 | Numeral_ROMAN Numeral = 2 48 | ) 49 | 50 | var Numeral_name = map[int32]string{ 51 | 0: "UNKNOWN", 52 | 1: "ARABIC", 53 | 2: "ROMAN", 54 | } 55 | var Numeral_value = map[string]int32{ 56 | "UNKNOWN": 0, 57 | "ARABIC": 1, 58 | "ROMAN": 2, 59 | } 60 | 61 | func (x Numeral) String() string { 62 | return proto.EnumName(Numeral_name, int32(x)) 63 | } 64 | func (Numeral) EnumDescriptor() ([]byte, []int) { return fileDescriptor0, []int{0} } 65 | 66 | type Simple3 struct { 67 | Dub float64 `protobuf:"fixed64,1,opt,name=dub" json:"dub,omitempty"` 68 | } 69 | 70 | func (m *Simple3) Reset() { *m = Simple3{} } 71 | func (m *Simple3) String() string { return proto.CompactTextString(m) } 72 | func (*Simple3) ProtoMessage() {} 73 | func (*Simple3) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} } 74 | 75 | type Mappy struct { 76 | Nummy map[int64]int32 `protobuf:"bytes,1,rep,name=nummy" json:"nummy,omitempty" protobuf_key:"varint,1,opt,name=key" protobuf_val:"varint,2,opt,name=value"` 77 | Strry map[string]string `protobuf:"bytes,2,rep,name=strry" json:"strry,omitempty" protobuf_key:"bytes,1,opt,name=key" protobuf_val:"bytes,2,opt,name=value"` 78 | Objjy map[int32]*Simple3 `protobuf:"bytes,3,rep,name=objjy" json:"objjy,omitempty" protobuf_key:"varint,1,opt,name=key" protobuf_val:"bytes,2,opt,name=value"` 79 | Buggy map[int64]string `protobuf:"bytes,4,rep,name=buggy" json:"buggy,omitempty" protobuf_key:"varint,1,opt,name=key" protobuf_val:"bytes,2,opt,name=value"` 80 | Booly map[bool]bool `protobuf:"bytes,5,rep,name=booly" json:"booly,omitempty" protobuf_key:"varint,1,opt,name=key" protobuf_val:"varint,2,opt,name=value"` 81 | Enumy map[string]Numeral `protobuf:"bytes,6,rep,name=enumy" json:"enumy,omitempty" protobuf_key:"bytes,1,opt,name=key" protobuf_val:"varint,2,opt,name=value,enum=jsonpb.Numeral"` 82 | } 83 | 84 | func (m *Mappy) Reset() { *m = Mappy{} } 85 | func (m *Mappy) String() string { return proto.CompactTextString(m) } 86 | func (*Mappy) ProtoMessage() {} 87 | func (*Mappy) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} } 88 | 89 | func (m *Mappy) GetNummy() map[int64]int32 { 90 | if m != nil { 91 | return m.Nummy 92 | } 93 | return nil 94 | } 95 | 96 | func (m *Mappy) GetStrry() map[string]string { 97 | if m != nil { 98 | return m.Strry 99 | } 100 | return nil 101 | } 102 | 103 | func (m *Mappy) GetObjjy() map[int32]*Simple3 { 104 | if m != nil { 105 | return m.Objjy 106 | } 107 | return nil 108 | } 109 | 110 | func (m *Mappy) GetBuggy() map[int64]string { 111 | if m != nil { 112 | return m.Buggy 113 | } 114 | return nil 115 | } 116 | 117 | func (m *Mappy) GetBooly() map[bool]bool { 118 | if m != nil { 119 | return m.Booly 120 | } 121 | return nil 122 | } 123 | 124 | func (m *Mappy) GetEnumy() map[string]Numeral { 125 | if m != nil { 126 | return m.Enumy 127 | } 128 | return nil 129 | } 130 | 131 | func init() { 132 | proto.RegisterType((*Simple3)(nil), "jsonpb.Simple3") 133 | proto.RegisterType((*Mappy)(nil), "jsonpb.Mappy") 134 | proto.RegisterEnum("jsonpb.Numeral", Numeral_name, Numeral_value) 135 | } 136 | 137 | func init() { proto.RegisterFile("more_test_objects.proto", fileDescriptor0) } 138 | 139 | var fileDescriptor0 = []byte{ 140 | // 357 bytes of a gzipped FileDescriptorProto 141 | 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x84, 0x93, 0xcd, 0x4a, 0xf3, 0x40, 142 | 0x18, 0x85, 0xbf, 0x34, 0xdf, 0xa4, 0xcd, 0x5b, 0xd0, 0x30, 0x08, 0x0e, 0xba, 0x91, 0x82, 0x50, 143 | 0x04, 0xb3, 0x68, 0x37, 0xc5, 0x5d, 0x2b, 0x5d, 0x14, 0x69, 0x0a, 0x29, 0xe2, 0xb2, 0x34, 0x3a, 144 | 0x14, 0x6b, 0xd2, 0x09, 0xf9, 0x11, 0xe6, 0x82, 0xbc, 0x4f, 0xf3, 0x4e, 0x52, 0x33, 0x96, 0x01, 145 | 0x77, 0xd3, 0x9e, 0xe7, 0x09, 0x67, 0x4e, 0x08, 0x5c, 0x26, 0x22, 0xe3, 0x9b, 0x82, 0xe7, 0xc5, 146 | 0x46, 0x44, 0x7b, 0xfe, 0x5a, 0xe4, 0x7e, 0x9a, 0x89, 0x42, 0x50, 0x67, 0x9f, 0x8b, 0x43, 0x1a, 147 | 0x0d, 0xae, 0xa1, 0xbb, 0x7e, 0x4f, 0xd2, 0x98, 0x8f, 0xa9, 0x07, 0xf6, 0x5b, 0x19, 0x31, 0xeb, 148 | 0xc6, 0x1a, 0x5a, 0x21, 0x1e, 0x07, 0x5f, 0x04, 0xc8, 0x72, 0x9b, 0xa6, 0x92, 0xfa, 0x40, 0x0e, 149 | 0x65, 0x92, 0xc8, 0x2a, 0xb5, 0x87, 0xfd, 0x11, 0xf3, 0x6b, 0xdd, 0x57, 0xa9, 0x1f, 0x60, 0x34, 150 | 0x3f, 0x14, 0x99, 0x0c, 0x6b, 0x0c, 0xf9, 0xbc, 0xc8, 0x32, 0xc9, 0x3a, 0x26, 0x7e, 0x8d, 0x51, 151 | 0xc3, 0x2b, 0x0c, 0xf9, 0xaa, 0xdf, 0x5e, 0x32, 0xdb, 0xc4, 0xaf, 0x30, 0x6a, 0x78, 0x85, 0x21, 152 | 0x1f, 0x95, 0xbb, 0x9d, 0x64, 0xff, 0x4d, 0xfc, 0x0c, 0xa3, 0x86, 0x57, 0x98, 0xe2, 0x85, 0x88, 153 | 0x25, 0x23, 0x46, 0x1e, 0xa3, 0x23, 0x8f, 0x67, 0xe4, 0x79, 0x75, 0x13, 0xc9, 0x1c, 0x13, 0x3f, 154 | 0xc7, 0xa8, 0xe1, 0x15, 0x76, 0x35, 0x01, 0x68, 0x47, 0xc0, 0x25, 0x3f, 0xb8, 0x54, 0x4b, 0xda, 155 | 0x21, 0x1e, 0xe9, 0x05, 0x90, 0xcf, 0x6d, 0x5c, 0xf2, 0x6a, 0x0f, 0x6b, 0x48, 0xc2, 0xfa, 0xc7, 156 | 0x43, 0x67, 0x62, 0xa1, 0xd9, 0xce, 0xa1, 0x9b, 0xae, 0xc1, 0x74, 0x75, 0x73, 0x01, 0xd0, 0x0e, 157 | 0xa3, 0x9b, 0xa4, 0x36, 0x6f, 0x75, 0xb3, 0x3f, 0x3a, 0x3f, 0xde, 0xa1, 0x79, 0xdf, 0x27, 0x25, 158 | 0xda, 0xcd, 0xfe, 0xaa, 0xef, 0x9e, 0x9a, 0x3f, 0xeb, 0xe9, 0x66, 0xcf, 0x60, 0xf6, 0x4e, 0xea, 159 | 0xb7, 0x3b, 0x1a, 0x2e, 0xfe, 0xab, 0xfe, 0x59, 0x5b, 0xbf, 0xda, 0x99, 0x67, 0xdb, 0x58, 0x7b, 160 | 0xd4, 0xdd, 0x3d, 0x74, 0x9b, 0x7f, 0x69, 0x1f, 0xba, 0xcf, 0xc1, 0x53, 0xb0, 0x7a, 0x09, 0xbc, 161 | 0x7f, 0x14, 0xc0, 0x99, 0x86, 0xd3, 0xd9, 0xe2, 0xd1, 0xb3, 0xa8, 0x0b, 0x24, 0x5c, 0x2d, 0xa7, 162 | 0x81, 0xd7, 0x89, 0x1c, 0xf5, 0x09, 0x8c, 0xbf, 0x03, 0x00, 0x00, 0xff, 0xff, 0x3d, 0x04, 0xff, 163 | 0x62, 0x1d, 0x03, 0x00, 0x00, 164 | } 165 | -------------------------------------------------------------------------------- /protoc-gen-go/testdata/extension_test.go: -------------------------------------------------------------------------------- 1 | // Go support for Protocol Buffers - Google's data interchange format 2 | // 3 | // Copyright 2010 The Go Authors. All rights reserved. 4 | // https://github.com/golang/protobuf 5 | // 6 | // Redistribution and use in source and binary forms, with or without 7 | // modification, are permitted provided that the following conditions are 8 | // met: 9 | // 10 | // * Redistributions of source code must retain the above copyright 11 | // notice, this list of conditions and the following disclaimer. 12 | // * Redistributions in binary form must reproduce the above 13 | // copyright notice, this list of conditions and the following disclaimer 14 | // in the documentation and/or other materials provided with the 15 | // distribution. 16 | // * Neither the name of Google Inc. nor the names of its 17 | // contributors may be used to endorse or promote products derived from 18 | // this software without specific prior written permission. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | // Test that we can use protocol buffers that use extensions. 33 | 34 | package testdata 35 | 36 | /* 37 | 38 | import ( 39 | "bytes" 40 | "regexp" 41 | "testing" 42 | 43 | "github.com/golang/protobuf/proto" 44 | base "extension_base.pb" 45 | user "extension_user.pb" 46 | ) 47 | 48 | func TestSingleFieldExtension(t *testing.T) { 49 | bm := &base.BaseMessage{ 50 | Height: proto.Int32(178), 51 | } 52 | 53 | // Use extension within scope of another type. 54 | vol := proto.Uint32(11) 55 | err := proto.SetExtension(bm, user.E_LoudMessage_Volume, vol) 56 | if err != nil { 57 | t.Fatal("Failed setting extension:", err) 58 | } 59 | buf, err := proto.Marshal(bm) 60 | if err != nil { 61 | t.Fatal("Failed encoding message with extension:", err) 62 | } 63 | bm_new := new(base.BaseMessage) 64 | if err := proto.Unmarshal(buf, bm_new); err != nil { 65 | t.Fatal("Failed decoding message with extension:", err) 66 | } 67 | if !proto.HasExtension(bm_new, user.E_LoudMessage_Volume) { 68 | t.Fatal("Decoded message didn't contain extension.") 69 | } 70 | vol_out, err := proto.GetExtension(bm_new, user.E_LoudMessage_Volume) 71 | if err != nil { 72 | t.Fatal("Failed getting extension:", err) 73 | } 74 | if v := vol_out.(*uint32); *v != *vol { 75 | t.Errorf("vol_out = %v, expected %v", *v, *vol) 76 | } 77 | proto.ClearExtension(bm_new, user.E_LoudMessage_Volume) 78 | if proto.HasExtension(bm_new, user.E_LoudMessage_Volume) { 79 | t.Fatal("Failed clearing extension.") 80 | } 81 | } 82 | 83 | func TestMessageExtension(t *testing.T) { 84 | bm := &base.BaseMessage{ 85 | Height: proto.Int32(179), 86 | } 87 | 88 | // Use extension that is itself a message. 89 | um := &user.UserMessage{ 90 | Name: proto.String("Dave"), 91 | Rank: proto.String("Major"), 92 | } 93 | err := proto.SetExtension(bm, user.E_LoginMessage_UserMessage, um) 94 | if err != nil { 95 | t.Fatal("Failed setting extension:", err) 96 | } 97 | buf, err := proto.Marshal(bm) 98 | if err != nil { 99 | t.Fatal("Failed encoding message with extension:", err) 100 | } 101 | bm_new := new(base.BaseMessage) 102 | if err := proto.Unmarshal(buf, bm_new); err != nil { 103 | t.Fatal("Failed decoding message with extension:", err) 104 | } 105 | if !proto.HasExtension(bm_new, user.E_LoginMessage_UserMessage) { 106 | t.Fatal("Decoded message didn't contain extension.") 107 | } 108 | um_out, err := proto.GetExtension(bm_new, user.E_LoginMessage_UserMessage) 109 | if err != nil { 110 | t.Fatal("Failed getting extension:", err) 111 | } 112 | if n := um_out.(*user.UserMessage).Name; *n != *um.Name { 113 | t.Errorf("um_out.Name = %q, expected %q", *n, *um.Name) 114 | } 115 | if r := um_out.(*user.UserMessage).Rank; *r != *um.Rank { 116 | t.Errorf("um_out.Rank = %q, expected %q", *r, *um.Rank) 117 | } 118 | proto.ClearExtension(bm_new, user.E_LoginMessage_UserMessage) 119 | if proto.HasExtension(bm_new, user.E_LoginMessage_UserMessage) { 120 | t.Fatal("Failed clearing extension.") 121 | } 122 | } 123 | 124 | func TestTopLevelExtension(t *testing.T) { 125 | bm := &base.BaseMessage{ 126 | Height: proto.Int32(179), 127 | } 128 | 129 | width := proto.Int32(17) 130 | err := proto.SetExtension(bm, user.E_Width, width) 131 | if err != nil { 132 | t.Fatal("Failed setting extension:", err) 133 | } 134 | buf, err := proto.Marshal(bm) 135 | if err != nil { 136 | t.Fatal("Failed encoding message with extension:", err) 137 | } 138 | bm_new := new(base.BaseMessage) 139 | if err := proto.Unmarshal(buf, bm_new); err != nil { 140 | t.Fatal("Failed decoding message with extension:", err) 141 | } 142 | if !proto.HasExtension(bm_new, user.E_Width) { 143 | t.Fatal("Decoded message didn't contain extension.") 144 | } 145 | width_out, err := proto.GetExtension(bm_new, user.E_Width) 146 | if err != nil { 147 | t.Fatal("Failed getting extension:", err) 148 | } 149 | if w := width_out.(*int32); *w != *width { 150 | t.Errorf("width_out = %v, expected %v", *w, *width) 151 | } 152 | proto.ClearExtension(bm_new, user.E_Width) 153 | if proto.HasExtension(bm_new, user.E_Width) { 154 | t.Fatal("Failed clearing extension.") 155 | } 156 | } 157 | 158 | func TestMessageSetWireFormat(t *testing.T) { 159 | osm := new(base.OldStyleMessage) 160 | osp := &user.OldStyleParcel{ 161 | Name: proto.String("Dave"), 162 | Height: proto.Int32(178), 163 | } 164 | 165 | err := proto.SetExtension(osm, user.E_OldStyleParcel_MessageSetExtension, osp) 166 | if err != nil { 167 | t.Fatal("Failed setting extension:", err) 168 | } 169 | 170 | buf, err := proto.Marshal(osm) 171 | if err != nil { 172 | t.Fatal("Failed encoding message:", err) 173 | } 174 | 175 | // Data generated from Python implementation. 176 | expected := []byte{ 177 | 11, 16, 209, 15, 26, 9, 10, 4, 68, 97, 118, 101, 16, 178, 1, 12, 178 | } 179 | 180 | if !bytes.Equal(expected, buf) { 181 | t.Errorf("Encoding mismatch.\nwant %+v\n got %+v", expected, buf) 182 | } 183 | 184 | // Check that it is restored correctly. 185 | osm = new(base.OldStyleMessage) 186 | if err := proto.Unmarshal(buf, osm); err != nil { 187 | t.Fatal("Failed decoding message:", err) 188 | } 189 | osp_out, err := proto.GetExtension(osm, user.E_OldStyleParcel_MessageSetExtension) 190 | if err != nil { 191 | t.Fatal("Failed getting extension:", err) 192 | } 193 | osp = osp_out.(*user.OldStyleParcel) 194 | if *osp.Name != "Dave" || *osp.Height != 178 { 195 | t.Errorf("Retrieved extension from decoded message is not correct: %+v", osp) 196 | } 197 | } 198 | 199 | func main() { 200 | // simpler than rigging up gotest 201 | testing.Main(regexp.MatchString, []testing.InternalTest{ 202 | {"TestSingleFieldExtension", TestSingleFieldExtension}, 203 | {"TestMessageExtension", TestMessageExtension}, 204 | {"TestTopLevelExtension", TestTopLevelExtension}, 205 | }, 206 | []testing.InternalBenchmark{}, 207 | []testing.InternalExample{}) 208 | } 209 | 210 | */ 211 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Go support for Protocol Buffers 2 | 3 | Google's data interchange format. 4 | Copyright 2010 The Go Authors. 5 | https://github.com/golang/protobuf 6 | 7 | This package and the code it generates requires at least Go 1.4. 8 | 9 | This software implements Go bindings for protocol buffers. For 10 | information about protocol buffers themselves, see 11 | https://developers.google.com/protocol-buffers/ 12 | 13 | ## Installation ## 14 | 15 | To use this software, you must: 16 | - Install the standard C++ implementation of protocol buffers from 17 | https://developers.google.com/protocol-buffers/ 18 | - Of course, install the Go compiler and tools from 19 | https://golang.org/ 20 | See 21 | https://golang.org/doc/install 22 | for details or, if you are using gccgo, follow the instructions at 23 | https://golang.org/doc/install/gccgo 24 | - Grab the code from the repository and install the proto package. 25 | The simplest way is to run `go get -u github.com/golang/protobuf/{proto,protoc-gen-go}`. 26 | The compiler plugin, protoc-gen-go, will be installed in $GOBIN, 27 | defaulting to $GOPATH/bin. It must be in your $PATH for the protocol 28 | compiler, protoc, to find it. 29 | 30 | This software has two parts: a 'protocol compiler plugin' that 31 | generates Go source files that, once compiled, can access and manage 32 | protocol buffers; and a library that implements run-time support for 33 | encoding (marshaling), decoding (unmarshaling), and accessing protocol 34 | buffers. 35 | 36 | There is support for gRPC in Go using protocol buffers. 37 | See the note at the bottom of this file for details. 38 | 39 | There are no insertion points in the plugin. 40 | 41 | 42 | ## Using protocol buffers with Go ## 43 | 44 | Once the software is installed, there are two steps to using it. 45 | First you must compile the protocol buffer definitions and then import 46 | them, with the support library, into your program. 47 | 48 | To compile the protocol buffer definition, run protoc with the --go_out 49 | parameter set to the directory you want to output the Go code to. 50 | 51 | protoc --go_out=. *.proto 52 | 53 | The generated files will be suffixed .pb.go. See the Test code below 54 | for an example using such a file. 55 | 56 | 57 | The package comment for the proto library contains text describing 58 | the interface provided in Go for protocol buffers. Here is an edited 59 | version. 60 | 61 | ========== 62 | 63 | The proto package converts data structures to and from the 64 | wire format of protocol buffers. It works in concert with the 65 | Go source code generated for .proto files by the protocol compiler. 66 | 67 | A summary of the properties of the protocol buffer interface 68 | for a protocol buffer variable v: 69 | 70 | - Names are turned from camel_case to CamelCase for export. 71 | - There are no methods on v to set fields; just treat 72 | them as structure fields. 73 | - There are getters that return a field's value if set, 74 | and return the field's default value if unset. 75 | The getters work even if the receiver is a nil message. 76 | - The zero value for a struct is its correct initialization state. 77 | All desired fields must be set before marshaling. 78 | - A Reset() method will restore a protobuf struct to its zero state. 79 | - Non-repeated fields are pointers to the values; nil means unset. 80 | That is, optional or required field int32 f becomes F *int32. 81 | - Repeated fields are slices. 82 | - Helper functions are available to aid the setting of fields. 83 | Helpers for getting values are superseded by the 84 | GetFoo methods and their use is deprecated. 85 | msg.Foo = proto.String("hello") // set field 86 | - Constants are defined to hold the default values of all fields that 87 | have them. They have the form Default_StructName_FieldName. 88 | Because the getter methods handle defaulted values, 89 | direct use of these constants should be rare. 90 | - Enums are given type names and maps from names to values. 91 | Enum values are prefixed with the enum's type name. Enum types have 92 | a String method, and a Enum method to assist in message construction. 93 | - Nested groups and enums have type names prefixed with the name of 94 | the surrounding message type. 95 | - Extensions are given descriptor names that start with E_, 96 | followed by an underscore-delimited list of the nested messages 97 | that contain it (if any) followed by the CamelCased name of the 98 | extension field itself. HasExtension, ClearExtension, GetExtension 99 | and SetExtension are functions for manipulating extensions. 100 | - Oneof field sets are given a single field in their message, 101 | with distinguished wrapper types for each possible field value. 102 | - Marshal and Unmarshal are functions to encode and decode the wire format. 103 | 104 | When the .proto file specifies `syntax="proto3"`, there are some differences: 105 | 106 | - Non-repeated fields of non-message type are values instead of pointers. 107 | - Getters are only generated for message and oneof fields. 108 | - Enum types do not get an Enum method. 109 | 110 | Consider file test.proto, containing 111 | 112 | ```proto 113 | package example; 114 | 115 | enum FOO { X = 17; }; 116 | 117 | message Test { 118 | required string label = 1; 119 | optional int32 type = 2 [default=77]; 120 | repeated int64 reps = 3; 121 | optional group OptionalGroup = 4 { 122 | required string RequiredField = 5; 123 | } 124 | } 125 | ``` 126 | 127 | To create and play with a Test object from the example package, 128 | 129 | ```go 130 | package main 131 | 132 | import ( 133 | "log" 134 | 135 | "github.com/golang/protobuf/proto" 136 | "path/to/example" 137 | ) 138 | 139 | func main() { 140 | test := &example.Test { 141 | Label: proto.String("hello"), 142 | Type: proto.Int32(17), 143 | Reps: []int64{1, 2, 3}, 144 | Optionalgroup: &example.Test_OptionalGroup { 145 | RequiredField: proto.String("good bye"), 146 | }, 147 | } 148 | data, err := proto.Marshal(test) 149 | if err != nil { 150 | log.Fatal("marshaling error: ", err) 151 | } 152 | newTest := &example.Test{} 153 | err = proto.Unmarshal(data, newTest) 154 | if err != nil { 155 | log.Fatal("unmarshaling error: ", err) 156 | } 157 | // Now test and newTest contain the same data. 158 | if test.GetLabel() != newTest.GetLabel() { 159 | log.Fatalf("data mismatch %q != %q", test.GetLabel(), newTest.GetLabel()) 160 | } 161 | // etc. 162 | } 163 | ``` 164 | 165 | ## Parameters ## 166 | 167 | To pass extra parameters to the plugin, use a comma-separated 168 | parameter list separated from the output directory by a colon: 169 | 170 | 171 | protoc --go_out=plugins=grpc,import_path=mypackage:. *.proto 172 | 173 | 174 | - `import_prefix=xxx` - a prefix that is added onto the beginning of 175 | all imports. Useful for things like generating protos in a 176 | subdirectory, or regenerating vendored protobufs in-place. 177 | - `import_path=foo/bar` - used as the package if no input files 178 | declare `go_package`. If it contains slashes, everything up to the 179 | rightmost slash is ignored. 180 | - `plugins=plugin1+plugin2` - specifies the list of sub-plugins to 181 | load. The only plugin in this repo is `grpc`. 182 | - `Mfoo/bar.proto=quux/shme` - declares that foo/bar.proto is 183 | associated with Go package quux/shme. This is subject to the 184 | import_prefix parameter. 185 | 186 | ## gRPC Support ## 187 | 188 | If a proto file specifies RPC services, protoc-gen-go can be instructed to 189 | generate code compatible with gRPC (http://www.grpc.io/). To do this, pass 190 | the `plugins` parameter to protoc-gen-go; the usual way is to insert it into 191 | the --go_out argument to protoc: 192 | 193 | protoc --go_out=plugins=grpc:. *.proto 194 | 195 | ## Plugins ## 196 | 197 | The `protoc-gen-go/generator` package exposes a plugin interface, 198 | which is used by the gRPC code generation. This interface is not 199 | supported and is subject to incompatible changes without notice. 200 | -------------------------------------------------------------------------------- /proto/clone.go: -------------------------------------------------------------------------------- 1 | // Go support for Protocol Buffers - Google's data interchange format 2 | // 3 | // Copyright 2011 The Go Authors. All rights reserved. 4 | // https://github.com/golang/protobuf 5 | // 6 | // Redistribution and use in source and binary forms, with or without 7 | // modification, are permitted provided that the following conditions are 8 | // met: 9 | // 10 | // * Redistributions of source code must retain the above copyright 11 | // notice, this list of conditions and the following disclaimer. 12 | // * Redistributions in binary form must reproduce the above 13 | // copyright notice, this list of conditions and the following disclaimer 14 | // in the documentation and/or other materials provided with the 15 | // distribution. 16 | // * Neither the name of Google Inc. nor the names of its 17 | // contributors may be used to endorse or promote products derived from 18 | // this software without specific prior written permission. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | // Protocol buffer deep copy and merge. 33 | // TODO: RawMessage. 34 | 35 | package proto 36 | 37 | import ( 38 | "log" 39 | "reflect" 40 | "strings" 41 | ) 42 | 43 | // Clone returns a deep copy of a protocol buffer. 44 | func Clone(pb Message) Message { 45 | in := reflect.ValueOf(pb) 46 | if in.IsNil() { 47 | return pb 48 | } 49 | 50 | out := reflect.New(in.Type().Elem()) 51 | // out is empty so a merge is a deep copy. 52 | mergeStruct(out.Elem(), in.Elem()) 53 | return out.Interface().(Message) 54 | } 55 | 56 | // Merge merges src into dst. 57 | // Required and optional fields that are set in src will be set to that value in dst. 58 | // Elements of repeated fields will be appended. 59 | // Merge panics if src and dst are not the same type, or if dst is nil. 60 | func Merge(dst, src Message) { 61 | in := reflect.ValueOf(src) 62 | out := reflect.ValueOf(dst) 63 | if out.IsNil() { 64 | panic("proto: nil destination") 65 | } 66 | if in.Type() != out.Type() { 67 | // Explicit test prior to mergeStruct so that mistyped nils will fail 68 | panic("proto: type mismatch") 69 | } 70 | if in.IsNil() { 71 | // Merging nil into non-nil is a quiet no-op 72 | return 73 | } 74 | mergeStruct(out.Elem(), in.Elem()) 75 | } 76 | 77 | func mergeStruct(out, in reflect.Value) { 78 | sprop := GetProperties(in.Type()) 79 | for i := 0; i < in.NumField(); i++ { 80 | f := in.Type().Field(i) 81 | if strings.HasPrefix(f.Name, "XXX_") { 82 | continue 83 | } 84 | mergeAny(out.Field(i), in.Field(i), false, sprop.Prop[i]) 85 | } 86 | 87 | if emIn, ok := extendable(in.Addr().Interface()); ok { 88 | emOut, _ := extendable(out.Addr().Interface()) 89 | mIn, muIn := emIn.extensionsRead() 90 | if mIn != nil { 91 | mOut := emOut.extensionsWrite() 92 | muIn.Lock() 93 | mergeExtension(mOut, mIn) 94 | muIn.Unlock() 95 | } 96 | } 97 | 98 | uf := in.FieldByName("XXX_unrecognized") 99 | if !uf.IsValid() { 100 | return 101 | } 102 | uin := uf.Bytes() 103 | if len(uin) > 0 { 104 | out.FieldByName("XXX_unrecognized").SetBytes(append([]byte(nil), uin...)) 105 | } 106 | } 107 | 108 | // mergeAny performs a merge between two values of the same type. 109 | // viaPtr indicates whether the values were indirected through a pointer (implying proto2). 110 | // prop is set if this is a struct field (it may be nil). 111 | func mergeAny(out, in reflect.Value, viaPtr bool, prop *Properties) { 112 | if in.Type() == protoMessageType { 113 | if !in.IsNil() { 114 | if out.IsNil() { 115 | out.Set(reflect.ValueOf(Clone(in.Interface().(Message)))) 116 | } else { 117 | Merge(out.Interface().(Message), in.Interface().(Message)) 118 | } 119 | } 120 | return 121 | } 122 | switch in.Kind() { 123 | case reflect.Bool, reflect.Float32, reflect.Float64, reflect.Int32, reflect.Int64, 124 | reflect.String, reflect.Uint32, reflect.Uint64: 125 | if !viaPtr && isProto3Zero(in) { 126 | return 127 | } 128 | out.Set(in) 129 | case reflect.Interface: 130 | // Probably a oneof field; copy non-nil values. 131 | if in.IsNil() { 132 | return 133 | } 134 | // Allocate destination if it is not set, or set to a different type. 135 | // Otherwise we will merge as normal. 136 | if out.IsNil() || out.Elem().Type() != in.Elem().Type() { 137 | out.Set(reflect.New(in.Elem().Elem().Type())) // interface -> *T -> T -> new(T) 138 | } 139 | mergeAny(out.Elem(), in.Elem(), false, nil) 140 | case reflect.Map: 141 | if in.Len() == 0 { 142 | return 143 | } 144 | if out.IsNil() { 145 | out.Set(reflect.MakeMap(in.Type())) 146 | } 147 | // For maps with value types of *T or []byte we need to deep copy each value. 148 | elemKind := in.Type().Elem().Kind() 149 | for _, key := range in.MapKeys() { 150 | var val reflect.Value 151 | switch elemKind { 152 | case reflect.Ptr: 153 | val = reflect.New(in.Type().Elem().Elem()) 154 | mergeAny(val, in.MapIndex(key), false, nil) 155 | case reflect.Slice: 156 | val = in.MapIndex(key) 157 | val = reflect.ValueOf(append([]byte{}, val.Bytes()...)) 158 | default: 159 | val = in.MapIndex(key) 160 | } 161 | out.SetMapIndex(key, val) 162 | } 163 | case reflect.Ptr: 164 | if in.IsNil() { 165 | return 166 | } 167 | if out.IsNil() { 168 | out.Set(reflect.New(in.Elem().Type())) 169 | } 170 | mergeAny(out.Elem(), in.Elem(), true, nil) 171 | case reflect.Slice: 172 | if in.IsNil() { 173 | return 174 | } 175 | if in.Type().Elem().Kind() == reflect.Uint8 { 176 | // []byte is a scalar bytes field, not a repeated field. 177 | 178 | // Edge case: if this is in a proto3 message, a zero length 179 | // bytes field is considered the zero value, and should not 180 | // be merged. 181 | if prop != nil && prop.proto3 && in.Len() == 0 { 182 | return 183 | } 184 | 185 | // Make a deep copy. 186 | // Append to []byte{} instead of []byte(nil) so that we never end up 187 | // with a nil result. 188 | out.SetBytes(append([]byte{}, in.Bytes()...)) 189 | return 190 | } 191 | n := in.Len() 192 | if out.IsNil() { 193 | out.Set(reflect.MakeSlice(in.Type(), 0, n)) 194 | } 195 | switch in.Type().Elem().Kind() { 196 | case reflect.Bool, reflect.Float32, reflect.Float64, reflect.Int32, reflect.Int64, 197 | reflect.String, reflect.Uint32, reflect.Uint64: 198 | out.Set(reflect.AppendSlice(out, in)) 199 | default: 200 | for i := 0; i < n; i++ { 201 | x := reflect.Indirect(reflect.New(in.Type().Elem())) 202 | mergeAny(x, in.Index(i), false, nil) 203 | out.Set(reflect.Append(out, x)) 204 | } 205 | } 206 | case reflect.Struct: 207 | mergeStruct(out, in) 208 | default: 209 | // unknown type, so not a protocol buffer 210 | log.Printf("proto: don't know how to copy %v", in) 211 | } 212 | } 213 | 214 | func mergeExtension(out, in map[int32]Extension) { 215 | for extNum, eIn := range in { 216 | eOut := Extension{desc: eIn.desc} 217 | if eIn.value != nil { 218 | v := reflect.New(reflect.TypeOf(eIn.value)).Elem() 219 | mergeAny(v, reflect.ValueOf(eIn.value), false, nil) 220 | eOut.value = v.Interface() 221 | } 222 | if eIn.enc != nil { 223 | eOut.enc = make([]byte, len(eIn.enc)) 224 | copy(eOut.enc, eIn.enc) 225 | } 226 | 227 | out[extNum] = eOut 228 | } 229 | } 230 | -------------------------------------------------------------------------------- /proto/size_test.go: -------------------------------------------------------------------------------- 1 | // Go support for Protocol Buffers - Google's data interchange format 2 | // 3 | // Copyright 2012 The Go Authors. All rights reserved. 4 | // https://github.com/golang/protobuf 5 | // 6 | // Redistribution and use in source and binary forms, with or without 7 | // modification, are permitted provided that the following conditions are 8 | // met: 9 | // 10 | // * Redistributions of source code must retain the above copyright 11 | // notice, this list of conditions and the following disclaimer. 12 | // * Redistributions in binary form must reproduce the above 13 | // copyright notice, this list of conditions and the following disclaimer 14 | // in the documentation and/or other materials provided with the 15 | // distribution. 16 | // * Neither the name of Google Inc. nor the names of its 17 | // contributors may be used to endorse or promote products derived from 18 | // this software without specific prior written permission. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | package proto_test 33 | 34 | import ( 35 | "log" 36 | "strings" 37 | "testing" 38 | 39 | . "github.com/golang/protobuf/proto" 40 | proto3pb "github.com/golang/protobuf/proto/proto3_proto" 41 | pb "github.com/golang/protobuf/proto/testdata" 42 | ) 43 | 44 | var messageWithExtension1 = &pb.MyMessage{Count: Int32(7)} 45 | 46 | // messageWithExtension2 is in equal_test.go. 47 | var messageWithExtension3 = &pb.MyMessage{Count: Int32(8)} 48 | 49 | func init() { 50 | if err := SetExtension(messageWithExtension1, pb.E_Ext_More, &pb.Ext{Data: String("Abbott")}); err != nil { 51 | log.Panicf("SetExtension: %v", err) 52 | } 53 | if err := SetExtension(messageWithExtension3, pb.E_Ext_More, &pb.Ext{Data: String("Costello")}); err != nil { 54 | log.Panicf("SetExtension: %v", err) 55 | } 56 | 57 | // Force messageWithExtension3 to have the extension encoded. 58 | Marshal(messageWithExtension3) 59 | 60 | } 61 | 62 | var SizeTests = []struct { 63 | desc string 64 | pb Message 65 | }{ 66 | {"empty", &pb.OtherMessage{}}, 67 | // Basic types. 68 | {"bool", &pb.Defaults{F_Bool: Bool(true)}}, 69 | {"int32", &pb.Defaults{F_Int32: Int32(12)}}, 70 | {"negative int32", &pb.Defaults{F_Int32: Int32(-1)}}, 71 | {"small int64", &pb.Defaults{F_Int64: Int64(1)}}, 72 | {"big int64", &pb.Defaults{F_Int64: Int64(1 << 20)}}, 73 | {"negative int64", &pb.Defaults{F_Int64: Int64(-1)}}, 74 | {"fixed32", &pb.Defaults{F_Fixed32: Uint32(71)}}, 75 | {"fixed64", &pb.Defaults{F_Fixed64: Uint64(72)}}, 76 | {"uint32", &pb.Defaults{F_Uint32: Uint32(123)}}, 77 | {"uint64", &pb.Defaults{F_Uint64: Uint64(124)}}, 78 | {"float", &pb.Defaults{F_Float: Float32(12.6)}}, 79 | {"double", &pb.Defaults{F_Double: Float64(13.9)}}, 80 | {"string", &pb.Defaults{F_String: String("niles")}}, 81 | {"bytes", &pb.Defaults{F_Bytes: []byte("wowsa")}}, 82 | {"bytes, empty", &pb.Defaults{F_Bytes: []byte{}}}, 83 | {"sint32", &pb.Defaults{F_Sint32: Int32(65)}}, 84 | {"sint64", &pb.Defaults{F_Sint64: Int64(67)}}, 85 | {"enum", &pb.Defaults{F_Enum: pb.Defaults_BLUE.Enum()}}, 86 | // Repeated. 87 | {"empty repeated bool", &pb.MoreRepeated{Bools: []bool{}}}, 88 | {"repeated bool", &pb.MoreRepeated{Bools: []bool{false, true, true, false}}}, 89 | {"packed repeated bool", &pb.MoreRepeated{BoolsPacked: []bool{false, true, true, false, true, true, true}}}, 90 | {"repeated int32", &pb.MoreRepeated{Ints: []int32{1, 12203, 1729, -1}}}, 91 | {"repeated int32 packed", &pb.MoreRepeated{IntsPacked: []int32{1, 12203, 1729}}}, 92 | {"repeated int64 packed", &pb.MoreRepeated{Int64SPacked: []int64{ 93 | // Need enough large numbers to verify that the header is counting the number of bytes 94 | // for the field, not the number of elements. 95 | 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 96 | 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 97 | }}}, 98 | {"repeated string", &pb.MoreRepeated{Strings: []string{"r", "ken", "gri"}}}, 99 | {"repeated fixed", &pb.MoreRepeated{Fixeds: []uint32{1, 2, 3, 4}}}, 100 | // Nested. 101 | {"nested", &pb.OldMessage{Nested: &pb.OldMessage_Nested{Name: String("whatever")}}}, 102 | {"group", &pb.GroupOld{G: &pb.GroupOld_G{X: Int32(12345)}}}, 103 | // Other things. 104 | {"unrecognized", &pb.MoreRepeated{XXX_unrecognized: []byte{13<<3 | 0, 4}}}, 105 | {"extension (unencoded)", messageWithExtension1}, 106 | {"extension (encoded)", messageWithExtension3}, 107 | // proto3 message 108 | {"proto3 empty", &proto3pb.Message{}}, 109 | {"proto3 bool", &proto3pb.Message{TrueScotsman: true}}, 110 | {"proto3 int64", &proto3pb.Message{ResultCount: 1}}, 111 | {"proto3 uint32", &proto3pb.Message{HeightInCm: 123}}, 112 | {"proto3 float", &proto3pb.Message{Score: 12.6}}, 113 | {"proto3 string", &proto3pb.Message{Name: "Snezana"}}, 114 | {"proto3 bytes", &proto3pb.Message{Data: []byte("wowsa")}}, 115 | {"proto3 bytes, empty", &proto3pb.Message{Data: []byte{}}}, 116 | {"proto3 enum", &proto3pb.Message{Hilarity: proto3pb.Message_PUNS}}, 117 | {"proto3 map field with empty bytes", &proto3pb.MessageWithMap{ByteMapping: map[bool][]byte{false: []byte{}}}}, 118 | 119 | {"map field", &pb.MessageWithMap{NameMapping: map[int32]string{1: "Rob", 7: "Andrew"}}}, 120 | {"map field with message", &pb.MessageWithMap{MsgMapping: map[int64]*pb.FloatingPoint{0x7001: &pb.FloatingPoint{F: Float64(2.0)}}}}, 121 | {"map field with bytes", &pb.MessageWithMap{ByteMapping: map[bool][]byte{true: []byte("this time for sure")}}}, 122 | {"map field with empty bytes", &pb.MessageWithMap{ByteMapping: map[bool][]byte{true: []byte{}}}}, 123 | 124 | {"map field with big entry", &pb.MessageWithMap{NameMapping: map[int32]string{8: strings.Repeat("x", 125)}}}, 125 | {"map field with big key and val", &pb.MessageWithMap{StrToStr: map[string]string{strings.Repeat("x", 70): strings.Repeat("y", 70)}}}, 126 | {"map field with big numeric key", &pb.MessageWithMap{NameMapping: map[int32]string{0xf00d: "om nom nom"}}}, 127 | 128 | {"oneof not set", &pb.Oneof{}}, 129 | {"oneof bool", &pb.Oneof{Union: &pb.Oneof_F_Bool{true}}}, 130 | {"oneof zero int32", &pb.Oneof{Union: &pb.Oneof_F_Int32{0}}}, 131 | {"oneof big int32", &pb.Oneof{Union: &pb.Oneof_F_Int32{1 << 20}}}, 132 | {"oneof int64", &pb.Oneof{Union: &pb.Oneof_F_Int64{42}}}, 133 | {"oneof fixed32", &pb.Oneof{Union: &pb.Oneof_F_Fixed32{43}}}, 134 | {"oneof fixed64", &pb.Oneof{Union: &pb.Oneof_F_Fixed64{44}}}, 135 | {"oneof uint32", &pb.Oneof{Union: &pb.Oneof_F_Uint32{45}}}, 136 | {"oneof uint64", &pb.Oneof{Union: &pb.Oneof_F_Uint64{46}}}, 137 | {"oneof float", &pb.Oneof{Union: &pb.Oneof_F_Float{47.1}}}, 138 | {"oneof double", &pb.Oneof{Union: &pb.Oneof_F_Double{48.9}}}, 139 | {"oneof string", &pb.Oneof{Union: &pb.Oneof_F_String{"Rhythmic Fman"}}}, 140 | {"oneof bytes", &pb.Oneof{Union: &pb.Oneof_F_Bytes{[]byte("let go")}}}, 141 | {"oneof sint32", &pb.Oneof{Union: &pb.Oneof_F_Sint32{50}}}, 142 | {"oneof sint64", &pb.Oneof{Union: &pb.Oneof_F_Sint64{51}}}, 143 | {"oneof enum", &pb.Oneof{Union: &pb.Oneof_F_Enum{pb.MyMessage_BLUE}}}, 144 | {"message for oneof", &pb.GoTestField{Label: String("k"), Type: String("v")}}, 145 | {"oneof message", &pb.Oneof{Union: &pb.Oneof_F_Message{&pb.GoTestField{Label: String("k"), Type: String("v")}}}}, 146 | {"oneof group", &pb.Oneof{Union: &pb.Oneof_FGroup{&pb.Oneof_F_Group{X: Int32(52)}}}}, 147 | {"oneof largest tag", &pb.Oneof{Union: &pb.Oneof_F_Largest_Tag{1}}}, 148 | {"multiple oneofs", &pb.Oneof{Union: &pb.Oneof_F_Int32{1}, Tormato: &pb.Oneof_Value{2}}}, 149 | } 150 | 151 | func TestSize(t *testing.T) { 152 | for _, tc := range SizeTests { 153 | size := Size(tc.pb) 154 | b, err := Marshal(tc.pb) 155 | if err != nil { 156 | t.Errorf("%v: Marshal failed: %v", tc.desc, err) 157 | continue 158 | } 159 | if size != len(b) { 160 | t.Errorf("%v: Size(%v) = %d, want %d", tc.desc, tc.pb, size, len(b)) 161 | t.Logf("%v: bytes: %#v", tc.desc, b) 162 | } 163 | } 164 | } 165 | -------------------------------------------------------------------------------- /proto/equal_test.go: -------------------------------------------------------------------------------- 1 | // Go support for Protocol Buffers - Google's data interchange format 2 | // 3 | // Copyright 2011 The Go Authors. All rights reserved. 4 | // https://github.com/golang/protobuf 5 | // 6 | // Redistribution and use in source and binary forms, with or without 7 | // modification, are permitted provided that the following conditions are 8 | // met: 9 | // 10 | // * Redistributions of source code must retain the above copyright 11 | // notice, this list of conditions and the following disclaimer. 12 | // * Redistributions in binary form must reproduce the above 13 | // copyright notice, this list of conditions and the following disclaimer 14 | // in the documentation and/or other materials provided with the 15 | // distribution. 16 | // * Neither the name of Google Inc. nor the names of its 17 | // contributors may be used to endorse or promote products derived from 18 | // this software without specific prior written permission. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | package proto_test 33 | 34 | import ( 35 | "testing" 36 | 37 | . "github.com/golang/protobuf/proto" 38 | proto3pb "github.com/golang/protobuf/proto/proto3_proto" 39 | pb "github.com/golang/protobuf/proto/testdata" 40 | ) 41 | 42 | // Four identical base messages. 43 | // The init function adds extensions to some of them. 44 | var messageWithoutExtension = &pb.MyMessage{Count: Int32(7)} 45 | var messageWithExtension1a = &pb.MyMessage{Count: Int32(7)} 46 | var messageWithExtension1b = &pb.MyMessage{Count: Int32(7)} 47 | var messageWithExtension2 = &pb.MyMessage{Count: Int32(7)} 48 | 49 | // Two messages with non-message extensions. 50 | var messageWithInt32Extension1 = &pb.MyMessage{Count: Int32(8)} 51 | var messageWithInt32Extension2 = &pb.MyMessage{Count: Int32(8)} 52 | 53 | func init() { 54 | ext1 := &pb.Ext{Data: String("Kirk")} 55 | ext2 := &pb.Ext{Data: String("Picard")} 56 | 57 | // messageWithExtension1a has ext1, but never marshals it. 58 | if err := SetExtension(messageWithExtension1a, pb.E_Ext_More, ext1); err != nil { 59 | panic("SetExtension on 1a failed: " + err.Error()) 60 | } 61 | 62 | // messageWithExtension1b is the unmarshaled form of messageWithExtension1a. 63 | if err := SetExtension(messageWithExtension1b, pb.E_Ext_More, ext1); err != nil { 64 | panic("SetExtension on 1b failed: " + err.Error()) 65 | } 66 | buf, err := Marshal(messageWithExtension1b) 67 | if err != nil { 68 | panic("Marshal of 1b failed: " + err.Error()) 69 | } 70 | messageWithExtension1b.Reset() 71 | if err := Unmarshal(buf, messageWithExtension1b); err != nil { 72 | panic("Unmarshal of 1b failed: " + err.Error()) 73 | } 74 | 75 | // messageWithExtension2 has ext2. 76 | if err := SetExtension(messageWithExtension2, pb.E_Ext_More, ext2); err != nil { 77 | panic("SetExtension on 2 failed: " + err.Error()) 78 | } 79 | 80 | if err := SetExtension(messageWithInt32Extension1, pb.E_Ext_Number, Int32(23)); err != nil { 81 | panic("SetExtension on Int32-1 failed: " + err.Error()) 82 | } 83 | if err := SetExtension(messageWithInt32Extension1, pb.E_Ext_Number, Int32(24)); err != nil { 84 | panic("SetExtension on Int32-2 failed: " + err.Error()) 85 | } 86 | } 87 | 88 | var EqualTests = []struct { 89 | desc string 90 | a, b Message 91 | exp bool 92 | }{ 93 | {"different types", &pb.GoEnum{}, &pb.GoTestField{}, false}, 94 | {"equal empty", &pb.GoEnum{}, &pb.GoEnum{}, true}, 95 | {"nil vs nil", nil, nil, true}, 96 | {"typed nil vs typed nil", (*pb.GoEnum)(nil), (*pb.GoEnum)(nil), true}, 97 | {"typed nil vs empty", (*pb.GoEnum)(nil), &pb.GoEnum{}, false}, 98 | {"different typed nil", (*pb.GoEnum)(nil), (*pb.GoTestField)(nil), false}, 99 | 100 | {"one set field, one unset field", &pb.GoTestField{Label: String("foo")}, &pb.GoTestField{}, false}, 101 | {"one set field zero, one unset field", &pb.GoTest{Param: Int32(0)}, &pb.GoTest{}, false}, 102 | {"different set fields", &pb.GoTestField{Label: String("foo")}, &pb.GoTestField{Label: String("bar")}, false}, 103 | {"equal set", &pb.GoTestField{Label: String("foo")}, &pb.GoTestField{Label: String("foo")}, true}, 104 | 105 | {"repeated, one set", &pb.GoTest{F_Int32Repeated: []int32{2, 3}}, &pb.GoTest{}, false}, 106 | {"repeated, different length", &pb.GoTest{F_Int32Repeated: []int32{2, 3}}, &pb.GoTest{F_Int32Repeated: []int32{2}}, false}, 107 | {"repeated, different value", &pb.GoTest{F_Int32Repeated: []int32{2}}, &pb.GoTest{F_Int32Repeated: []int32{3}}, false}, 108 | {"repeated, equal", &pb.GoTest{F_Int32Repeated: []int32{2, 4}}, &pb.GoTest{F_Int32Repeated: []int32{2, 4}}, true}, 109 | {"repeated, nil equal nil", &pb.GoTest{F_Int32Repeated: nil}, &pb.GoTest{F_Int32Repeated: nil}, true}, 110 | {"repeated, nil equal empty", &pb.GoTest{F_Int32Repeated: nil}, &pb.GoTest{F_Int32Repeated: []int32{}}, true}, 111 | {"repeated, empty equal nil", &pb.GoTest{F_Int32Repeated: []int32{}}, &pb.GoTest{F_Int32Repeated: nil}, true}, 112 | 113 | { 114 | "nested, different", 115 | &pb.GoTest{RequiredField: &pb.GoTestField{Label: String("foo")}}, 116 | &pb.GoTest{RequiredField: &pb.GoTestField{Label: String("bar")}}, 117 | false, 118 | }, 119 | { 120 | "nested, equal", 121 | &pb.GoTest{RequiredField: &pb.GoTestField{Label: String("wow")}}, 122 | &pb.GoTest{RequiredField: &pb.GoTestField{Label: String("wow")}}, 123 | true, 124 | }, 125 | 126 | {"bytes", &pb.OtherMessage{Value: []byte("foo")}, &pb.OtherMessage{Value: []byte("foo")}, true}, 127 | {"bytes, empty", &pb.OtherMessage{Value: []byte{}}, &pb.OtherMessage{Value: []byte{}}, true}, 128 | {"bytes, empty vs nil", &pb.OtherMessage{Value: []byte{}}, &pb.OtherMessage{Value: nil}, false}, 129 | { 130 | "repeated bytes", 131 | &pb.MyMessage{RepBytes: [][]byte{[]byte("sham"), []byte("wow")}}, 132 | &pb.MyMessage{RepBytes: [][]byte{[]byte("sham"), []byte("wow")}}, 133 | true, 134 | }, 135 | // In proto3, []byte{} and []byte(nil) are equal. 136 | {"proto3 bytes, empty vs nil", &proto3pb.Message{Data: []byte{}}, &proto3pb.Message{Data: nil}, true}, 137 | 138 | {"extension vs. no extension", messageWithoutExtension, messageWithExtension1a, false}, 139 | {"extension vs. same extension", messageWithExtension1a, messageWithExtension1b, true}, 140 | {"extension vs. different extension", messageWithExtension1a, messageWithExtension2, false}, 141 | 142 | {"int32 extension vs. itself", messageWithInt32Extension1, messageWithInt32Extension1, true}, 143 | {"int32 extension vs. a different int32", messageWithInt32Extension1, messageWithInt32Extension2, false}, 144 | 145 | { 146 | "message with group", 147 | &pb.MyMessage{ 148 | Count: Int32(1), 149 | Somegroup: &pb.MyMessage_SomeGroup{ 150 | GroupField: Int32(5), 151 | }, 152 | }, 153 | &pb.MyMessage{ 154 | Count: Int32(1), 155 | Somegroup: &pb.MyMessage_SomeGroup{ 156 | GroupField: Int32(5), 157 | }, 158 | }, 159 | true, 160 | }, 161 | 162 | { 163 | "map same", 164 | &pb.MessageWithMap{NameMapping: map[int32]string{1: "Ken"}}, 165 | &pb.MessageWithMap{NameMapping: map[int32]string{1: "Ken"}}, 166 | true, 167 | }, 168 | { 169 | "map different entry", 170 | &pb.MessageWithMap{NameMapping: map[int32]string{1: "Ken"}}, 171 | &pb.MessageWithMap{NameMapping: map[int32]string{2: "Rob"}}, 172 | false, 173 | }, 174 | { 175 | "map different key only", 176 | &pb.MessageWithMap{NameMapping: map[int32]string{1: "Ken"}}, 177 | &pb.MessageWithMap{NameMapping: map[int32]string{2: "Ken"}}, 178 | false, 179 | }, 180 | { 181 | "map different value only", 182 | &pb.MessageWithMap{NameMapping: map[int32]string{1: "Ken"}}, 183 | &pb.MessageWithMap{NameMapping: map[int32]string{1: "Rob"}}, 184 | false, 185 | }, 186 | { 187 | "oneof same", 188 | &pb.Communique{Union: &pb.Communique_Number{41}}, 189 | &pb.Communique{Union: &pb.Communique_Number{41}}, 190 | true, 191 | }, 192 | { 193 | "oneof one nil", 194 | &pb.Communique{Union: &pb.Communique_Number{41}}, 195 | &pb.Communique{}, 196 | false, 197 | }, 198 | { 199 | "oneof different", 200 | &pb.Communique{Union: &pb.Communique_Number{41}}, 201 | &pb.Communique{Union: &pb.Communique_Name{"Bobby Tables"}}, 202 | false, 203 | }, 204 | } 205 | 206 | func TestEqual(t *testing.T) { 207 | for _, tc := range EqualTests { 208 | if res := Equal(tc.a, tc.b); res != tc.exp { 209 | t.Errorf("%v: Equal(%v, %v) = %v, want %v", tc.desc, tc.a, tc.b, res, tc.exp) 210 | } 211 | } 212 | } 213 | --------------------------------------------------------------------------------