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 29package vanity 30 31import descriptor "github.com/gogo/protobuf/protoc-gen-gogo/descriptor" 32 33func ForEachFile(files []*descriptor.FileDescriptorProto, f func(file *descriptor.FileDescriptorProto)) { 34 for _, file := range files { 35 f(file) 36 } 37} 38 39func OnlyProto2(files []*descriptor.FileDescriptorProto) []*descriptor.FileDescriptorProto { 40 outs := make([]*descriptor.FileDescriptorProto, 0, len(files)) 41 for i, file := range files { 42 if file.GetSyntax() == "proto3" { 43 continue 44 } 45 outs = append(outs, files[i]) 46 } 47 return outs 48} 49 50func OnlyProto3(files []*descriptor.FileDescriptorProto) []*descriptor.FileDescriptorProto { 51 outs := make([]*descriptor.FileDescriptorProto, 0, len(files)) 52 for i, file := range files { 53 if file.GetSyntax() != "proto3" { 54 continue 55 } 56 outs = append(outs, files[i]) 57 } 58 return outs 59} 60 61func ForEachMessageInFiles(files []*descriptor.FileDescriptorProto, f func(msg *descriptor.DescriptorProto)) { 62 for _, file := range files { 63 ForEachMessage(file.MessageType, f) 64 } 65} 66 67func ForEachMessage(msgs []*descriptor.DescriptorProto, f func(msg *descriptor.DescriptorProto)) { 68 for _, msg := range msgs { 69 f(msg) 70 ForEachMessage(msg.NestedType, f) 71 } 72} 73 74func ForEachFieldInFilesExcludingExtensions(files []*descriptor.FileDescriptorProto, f func(field *descriptor.FieldDescriptorProto)) { 75 for _, file := range files { 76 ForEachFieldExcludingExtensions(file.MessageType, f) 77 } 78} 79 80func ForEachFieldInFiles(files []*descriptor.FileDescriptorProto, f func(field *descriptor.FieldDescriptorProto)) { 81 for _, file := range files { 82 for _, ext := range file.Extension { 83 f(ext) 84 } 85 ForEachField(file.MessageType, f) 86 } 87} 88 89func ForEachFieldExcludingExtensions(msgs []*descriptor.DescriptorProto, f func(field *descriptor.FieldDescriptorProto)) { 90 for _, msg := range msgs { 91 for _, field := range msg.Field { 92 f(field) 93 } 94 ForEachField(msg.NestedType, f) 95 } 96} 97 98func ForEachField(msgs []*descriptor.DescriptorProto, f func(field *descriptor.FieldDescriptorProto)) { 99 for _, msg := range msgs { 100 for _, field := range msg.Field { 101 f(field) 102 } 103 for _, ext := range msg.Extension { 104 f(ext) 105 } 106 ForEachField(msg.NestedType, f) 107 } 108} 109 110func ForEachEnumInFiles(files []*descriptor.FileDescriptorProto, f func(enum *descriptor.EnumDescriptorProto)) { 111 for _, file := range files { 112 for _, enum := range file.EnumType { 113 f(enum) 114 } 115 } 116} 117 118func ForEachEnum(msgs []*descriptor.DescriptorProto, f func(field *descriptor.EnumDescriptorProto)) { 119 for _, msg := range msgs { 120 for _, field := range msg.EnumType { 121 f(field) 122 } 123 ForEachEnum(msg.NestedType, f) 124 } 125} 126