github.com/ronaksoft/rony@v0.16.26-0.20230807065236-1743dbfe6959/internal/codegen/util.go (about) 1 package codegen 2 3 import ( 4 "hash/crc32" 5 "hash/crc64" 6 "strings" 7 "text/template" 8 9 "google.golang.org/protobuf/reflect/protoreflect" 10 ) 11 12 /* 13 Creation Time: 2021 - Jan - 12 14 Created by: (ehsan) 15 Maintainers: 16 1. Ehsan N. Moosa (E2) 17 Auditor: Ehsan N. Moosa (E2) 18 Copyright Ronak Software Group 2020 19 */ 20 21 var ( 22 CrcBits = 64 23 CrcTab = crc64.MakeTable(crc64.ISO) 24 ) 25 26 // ZeroValue returns the equal zero value based on the input type 27 func ZeroValue(f protoreflect.FieldDescriptor) string { 28 switch f.Kind() { 29 case protoreflect.BoolKind: 30 return "false" 31 case protoreflect.StringKind: 32 return "\"\"" 33 case protoreflect.MessageKind: 34 return "nil" 35 case protoreflect.BytesKind: 36 return "nil" 37 default: 38 return "0" 39 } 40 } 41 42 func GoKind(d protoreflect.FieldDescriptor) string { 43 switch d.Kind() { 44 case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind: 45 return "int32" 46 case protoreflect.Uint32Kind, protoreflect.Fixed32Kind: 47 return "uint32" 48 case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind: 49 return "int64" 50 case protoreflect.Uint64Kind, protoreflect.Fixed64Kind: 51 return "uint64" 52 case protoreflect.DoubleKind: 53 return "float64" 54 case protoreflect.FloatKind: 55 return "float32" 56 case protoreflect.StringKind: 57 return "string" 58 case protoreflect.BytesKind: 59 return "[]byte" 60 case protoreflect.BoolKind: 61 return "bool" 62 case protoreflect.EnumKind: 63 return string(d.Enum().Name()) 64 } 65 66 return "unsupported" 67 } 68 69 func CqlKind(d protoreflect.FieldDescriptor) string { 70 switch d.Kind() { 71 case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Uint32Kind, protoreflect.Fixed32Kind: 72 return "int" 73 case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Uint64Kind, protoreflect.Fixed64Kind: 74 return "bigint" 75 case protoreflect.DoubleKind: 76 return "double" 77 case protoreflect.FloatKind: 78 return "float" 79 case protoreflect.BytesKind, protoreflect.StringKind: 80 return "blob" 81 case protoreflect.BoolKind: 82 return "boolean" 83 case protoreflect.EnumKind: 84 return "int" 85 } 86 87 return "unsupported" 88 } 89 90 func CrcHash(data []byte) uint64 { 91 if CrcBits == 64 { 92 return crc64.Checksum(data, CrcTab) 93 } else { 94 return uint64(crc32.Checksum(data, crc32.IEEETable)) 95 } 96 } 97 98 func ExecTemplate(t *template.Template, v interface{}) string { 99 sb := &strings.Builder{} 100 if err := t.Execute(sb, v); err != nil { 101 panic(err) 102 } 103 104 return sb.String() 105 }