github.com/gogo/protobuf@v1.3.2/vanity/file.go (about) 1 // Protocol Buffers for Go with Gadgets 2 // 3 // Copyright (c) 2015, The GoGo Authors. All rights reserved. 4 // http://github.com/gogo/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 // 17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29 package vanity 30 31 import ( 32 "path/filepath" 33 34 "github.com/gogo/protobuf/gogoproto" 35 "github.com/gogo/protobuf/proto" 36 descriptor "github.com/gogo/protobuf/protoc-gen-gogo/descriptor" 37 ) 38 39 func NotGoogleProtobufDescriptorProto(file *descriptor.FileDescriptorProto) bool { 40 // can not just check if file.GetName() == "google/protobuf/descriptor.proto" because we do not want to assume compile path 41 _, fileName := filepath.Split(file.GetName()) 42 return !(file.GetPackage() == "google.protobuf" && fileName == "descriptor.proto") 43 } 44 45 func FilterFiles(files []*descriptor.FileDescriptorProto, f func(file *descriptor.FileDescriptorProto) bool) []*descriptor.FileDescriptorProto { 46 filtered := make([]*descriptor.FileDescriptorProto, 0, len(files)) 47 for i := range files { 48 if !f(files[i]) { 49 continue 50 } 51 filtered = append(filtered, files[i]) 52 } 53 return filtered 54 } 55 56 func FileHasBoolExtension(file *descriptor.FileDescriptorProto, extension *proto.ExtensionDesc) bool { 57 if file.Options == nil { 58 return false 59 } 60 value, err := proto.GetExtension(file.Options, extension) 61 if err != nil { 62 return false 63 } 64 if value == nil { 65 return false 66 } 67 if value.(*bool) == nil { 68 return false 69 } 70 return true 71 } 72 73 func SetBoolFileOption(extension *proto.ExtensionDesc, value bool) func(file *descriptor.FileDescriptorProto) { 74 return func(file *descriptor.FileDescriptorProto) { 75 if FileHasBoolExtension(file, extension) { 76 return 77 } 78 if file.Options == nil { 79 file.Options = &descriptor.FileOptions{} 80 } 81 if err := proto.SetExtension(file.Options, extension, &value); err != nil { 82 panic(err) 83 } 84 } 85 } 86 87 func TurnOffGoGettersAll(file *descriptor.FileDescriptorProto) { 88 SetBoolFileOption(gogoproto.E_GoprotoGettersAll, false)(file) 89 } 90 91 func TurnOffGoEnumPrefixAll(file *descriptor.FileDescriptorProto) { 92 SetBoolFileOption(gogoproto.E_GoprotoEnumPrefixAll, false)(file) 93 } 94 95 func TurnOffGoStringerAll(file *descriptor.FileDescriptorProto) { 96 SetBoolFileOption(gogoproto.E_GoprotoStringerAll, false)(file) 97 } 98 99 func TurnOnVerboseEqualAll(file *descriptor.FileDescriptorProto) { 100 SetBoolFileOption(gogoproto.E_VerboseEqualAll, true)(file) 101 } 102 103 func TurnOnFaceAll(file *descriptor.FileDescriptorProto) { 104 SetBoolFileOption(gogoproto.E_FaceAll, true)(file) 105 } 106 107 func TurnOnGoStringAll(file *descriptor.FileDescriptorProto) { 108 SetBoolFileOption(gogoproto.E_GostringAll, true)(file) 109 } 110 111 func TurnOnPopulateAll(file *descriptor.FileDescriptorProto) { 112 SetBoolFileOption(gogoproto.E_PopulateAll, true)(file) 113 } 114 115 func TurnOnStringerAll(file *descriptor.FileDescriptorProto) { 116 SetBoolFileOption(gogoproto.E_StringerAll, true)(file) 117 } 118 119 func TurnOnEqualAll(file *descriptor.FileDescriptorProto) { 120 SetBoolFileOption(gogoproto.E_EqualAll, true)(file) 121 } 122 123 func TurnOnDescriptionAll(file *descriptor.FileDescriptorProto) { 124 SetBoolFileOption(gogoproto.E_DescriptionAll, true)(file) 125 } 126 127 func TurnOnTestGenAll(file *descriptor.FileDescriptorProto) { 128 SetBoolFileOption(gogoproto.E_TestgenAll, true)(file) 129 } 130 131 func TurnOnBenchGenAll(file *descriptor.FileDescriptorProto) { 132 SetBoolFileOption(gogoproto.E_BenchgenAll, true)(file) 133 } 134 135 func TurnOnMarshalerAll(file *descriptor.FileDescriptorProto) { 136 SetBoolFileOption(gogoproto.E_MarshalerAll, true)(file) 137 } 138 139 func TurnOnUnmarshalerAll(file *descriptor.FileDescriptorProto) { 140 SetBoolFileOption(gogoproto.E_UnmarshalerAll, true)(file) 141 } 142 143 func TurnOnStable_MarshalerAll(file *descriptor.FileDescriptorProto) { 144 SetBoolFileOption(gogoproto.E_StableMarshalerAll, true)(file) 145 } 146 147 func TurnOnSizerAll(file *descriptor.FileDescriptorProto) { 148 SetBoolFileOption(gogoproto.E_SizerAll, true)(file) 149 } 150 151 func TurnOffGoEnumStringerAll(file *descriptor.FileDescriptorProto) { 152 SetBoolFileOption(gogoproto.E_GoprotoEnumStringerAll, false)(file) 153 } 154 155 func TurnOnEnumStringerAll(file *descriptor.FileDescriptorProto) { 156 SetBoolFileOption(gogoproto.E_EnumStringerAll, true)(file) 157 } 158 159 func TurnOnUnsafeUnmarshalerAll(file *descriptor.FileDescriptorProto) { 160 SetBoolFileOption(gogoproto.E_UnsafeUnmarshalerAll, true)(file) 161 } 162 163 func TurnOnUnsafeMarshalerAll(file *descriptor.FileDescriptorProto) { 164 SetBoolFileOption(gogoproto.E_UnsafeMarshalerAll, true)(file) 165 } 166 167 func TurnOffGoExtensionsMapAll(file *descriptor.FileDescriptorProto) { 168 SetBoolFileOption(gogoproto.E_GoprotoExtensionsMapAll, false)(file) 169 } 170 171 func TurnOffGoUnrecognizedAll(file *descriptor.FileDescriptorProto) { 172 SetBoolFileOption(gogoproto.E_GoprotoUnrecognizedAll, false)(file) 173 } 174 175 func TurnOffGoUnkeyedAll(file *descriptor.FileDescriptorProto) { 176 SetBoolFileOption(gogoproto.E_GoprotoUnkeyedAll, false)(file) 177 } 178 179 func TurnOffGoSizecacheAll(file *descriptor.FileDescriptorProto) { 180 SetBoolFileOption(gogoproto.E_GoprotoSizecacheAll, false)(file) 181 } 182 183 func TurnOffGogoImport(file *descriptor.FileDescriptorProto) { 184 SetBoolFileOption(gogoproto.E_GogoprotoImport, false)(file) 185 } 186 187 func TurnOnCompareAll(file *descriptor.FileDescriptorProto) { 188 SetBoolFileOption(gogoproto.E_CompareAll, true)(file) 189 } 190 191 func TurnOnMessageNameAll(file *descriptor.FileDescriptorProto) { 192 SetBoolFileOption(gogoproto.E_MessagenameAll, true)(file) 193 } 194 195 func TurnOnGoRegistration(file *descriptor.FileDescriptorProto) { 196 SetBoolFileOption(gogoproto.E_GoprotoRegistration, true)(file) 197 }