github.com/cosmos/cosmos-proto@v1.0.0-beta.3/generator/generatedfile.go (about) 1 // Copyright (c) 2021 PlanetScale Inc. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package generator 6 7 import ( 8 "fmt" 9 "google.golang.org/protobuf/compiler/protogen" 10 "google.golang.org/protobuf/reflect/protoreflect" 11 ) 12 13 type GeneratedFile struct { 14 *protogen.GeneratedFile 15 Ext *Extensions 16 LocalPackages map[string]bool 17 } 18 19 func (p *GeneratedFile) Ident(path, ident string) string { 20 return p.QualifiedGoIdent(protogen.GoImportPath(path).Ident(ident)) 21 } 22 23 func (p *GeneratedFile) FieldGoType(field *protogen.Field) (goType string, pointer bool) { 24 if field.Desc.IsWeak() { 25 return "struct{}", false 26 } 27 28 pointer = field.Desc.HasPresence() 29 switch field.Desc.Kind() { 30 case protoreflect.BoolKind: 31 goType = "bool" 32 case protoreflect.EnumKind: 33 goType = p.QualifiedGoIdent(field.Enum.GoIdent) 34 case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind: 35 goType = "int32" 36 case protoreflect.Uint32Kind, protoreflect.Fixed32Kind: 37 goType = "uint32" 38 case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind: 39 goType = "int64" 40 case protoreflect.Uint64Kind, protoreflect.Fixed64Kind: 41 goType = "uint64" 42 case protoreflect.FloatKind: 43 goType = "float32" 44 case protoreflect.DoubleKind: 45 goType = "float64" 46 case protoreflect.StringKind: 47 goType = "string" 48 case protoreflect.BytesKind: 49 goType = "[]byte" 50 pointer = false // rely on nullability of slices for presence 51 case protoreflect.MessageKind, protoreflect.GroupKind: 52 goType = "*" + p.QualifiedGoIdent(field.Message.GoIdent) 53 pointer = false // pointer captured as part of the type 54 } 55 switch { 56 case field.Desc.IsList(): 57 return "[]" + goType, false 58 case field.Desc.IsMap(): 59 keyType, _ := p.FieldGoType(field.Message.Fields[0]) 60 valType, _ := p.FieldGoType(field.Message.Fields[1]) 61 return fmt.Sprintf("map[%v]%v", keyType, valType), false 62 } 63 return goType, pointer 64 } 65 66 func (p *GeneratedFile) IsLocalMessage(message *protogen.Message) bool { 67 pkg := string(message.Desc.ParentFile().Package()) 68 return p.LocalPackages[pkg] 69 }