Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 1 | // Copyright 2018 The Go Authors. 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 impl |
| 6 | |
| 7 | import ( |
| 8 | "fmt" |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 9 | "reflect" |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 10 | "strings" |
| 11 | "sync" |
| 12 | "unicode" |
| 13 | |
Damien Neil | 204f1c0 | 2018-10-23 15:03:38 -0700 | [diff] [blame] | 14 | protoV1 "github.com/golang/protobuf/proto" |
| 15 | descriptorV1 "github.com/golang/protobuf/protoc-gen-go/descriptor" |
Joe Tsai | 05828db | 2018-11-01 13:52:16 -0700 | [diff] [blame^] | 16 | ptag "github.com/golang/protobuf/v2/internal/encoding/tag" |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 17 | pref "github.com/golang/protobuf/v2/reflect/protoreflect" |
| 18 | ptype "github.com/golang/protobuf/v2/reflect/prototype" |
| 19 | ) |
| 20 | |
| 21 | var messageDescCache sync.Map // map[reflect.Type]protoreflect.MessageDescriptor |
| 22 | |
| 23 | // loadMessageDesc returns an MessageDescriptor derived from the Go type, |
| 24 | // which must be an *struct kind and not implement the v2 API already. |
| 25 | func loadMessageDesc(t reflect.Type) pref.MessageDescriptor { |
| 26 | return messageDescSet{}.Load(t) |
| 27 | } |
| 28 | |
| 29 | type messageDescSet struct { |
| 30 | visited map[reflect.Type]*ptype.StandaloneMessage |
| 31 | descs []*ptype.StandaloneMessage |
| 32 | types []reflect.Type |
| 33 | } |
| 34 | |
| 35 | func (ms messageDescSet) Load(t reflect.Type) pref.MessageDescriptor { |
| 36 | // Fast-path: check if a MessageDescriptor is cached for this concrete type. |
| 37 | if mi, ok := messageDescCache.Load(t); ok { |
| 38 | return mi.(pref.MessageDescriptor) |
| 39 | } |
| 40 | |
| 41 | // Slow-path: initialize MessageDescriptor from the Go type. |
| 42 | |
| 43 | // Processing t recursively populates descs and types with all sub-messages. |
| 44 | // The descriptor for the first type is guaranteed to be at the front. |
| 45 | ms.processMessage(t) |
| 46 | |
| 47 | // Within a proto file it is possible for cyclic dependencies to exist |
| 48 | // between multiple message types. When these cases arise, the set of |
| 49 | // message descriptors must be created together. |
| 50 | mds, err := ptype.NewMessages(ms.descs) |
| 51 | if err != nil { |
| 52 | panic(err) |
| 53 | } |
| 54 | for i, md := range mds { |
| 55 | // Protobuf semantics represents map entries under-the-hood as |
| 56 | // pseudo-messages (has a descriptor, but no generated Go type). |
| 57 | // Avoid caching these fake messages. |
| 58 | if t := ms.types[i]; t.Kind() != reflect.Map { |
| 59 | messageDescCache.Store(t, md) |
| 60 | } |
| 61 | } |
| 62 | return mds[0] |
| 63 | } |
| 64 | |
| 65 | func (ms *messageDescSet) processMessage(t reflect.Type) pref.MessageDescriptor { |
| 66 | // Fast-path: Obtain a placeholder if the message is already processed. |
| 67 | if m, ok := ms.visited[t]; ok { |
| 68 | return ptype.PlaceholderMessage(m.FullName) |
| 69 | } |
| 70 | |
| 71 | // Slow-path: Walk over the struct fields to derive the message descriptor. |
| 72 | if t.Kind() != reflect.Ptr && t.Elem().Kind() != reflect.Struct { |
| 73 | panic(fmt.Sprintf("got %v, want *struct kind", t)) |
| 74 | } |
| 75 | |
| 76 | // Derive name and syntax from the raw descriptor. |
| 77 | m := new(ptype.StandaloneMessage) |
| 78 | mv := reflect.New(t.Elem()).Interface() |
| 79 | if _, ok := mv.(pref.ProtoMessage); ok { |
| 80 | panic(fmt.Sprintf("%v already implements proto.Message", t)) |
| 81 | } |
| 82 | if md, ok := mv.(legacyMessage); ok { |
| 83 | b, idxs := md.Descriptor() |
| 84 | fd := loadFileDesc(b) |
| 85 | |
| 86 | // Derive syntax. |
| 87 | switch fd.GetSyntax() { |
| 88 | case "proto2", "": |
| 89 | m.Syntax = pref.Proto2 |
| 90 | case "proto3": |
| 91 | m.Syntax = pref.Proto3 |
| 92 | } |
| 93 | |
| 94 | // Derive full name. |
| 95 | md := fd.MessageType[idxs[0]] |
| 96 | m.FullName = pref.FullName(fd.GetPackage()).Append(pref.Name(md.GetName())) |
| 97 | for _, i := range idxs[1:] { |
| 98 | md = md.NestedType[i] |
| 99 | m.FullName = m.FullName.Append(pref.Name(md.GetName())) |
| 100 | } |
| 101 | } else { |
| 102 | // If the type does not implement legacyMessage, then the only way to |
| 103 | // obtain the full name is through the registry. However, this is |
| 104 | // unreliable as some generated messages register with a fork of |
| 105 | // golang/protobuf, so the registry may not have this information. |
| 106 | m.FullName = deriveFullName(t.Elem()) |
| 107 | m.Syntax = pref.Proto2 |
| 108 | |
| 109 | // Try to determine if the message is using proto3 by checking scalars. |
| 110 | for i := 0; i < t.Elem().NumField(); i++ { |
| 111 | f := t.Elem().Field(i) |
| 112 | if tag := f.Tag.Get("protobuf"); tag != "" { |
| 113 | switch f.Type.Kind() { |
| 114 | case reflect.Bool, reflect.Int32, reflect.Int64, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64, reflect.String: |
| 115 | m.Syntax = pref.Proto3 |
| 116 | } |
| 117 | for _, s := range strings.Split(tag, ",") { |
| 118 | if s == "proto3" { |
| 119 | m.Syntax = pref.Proto3 |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | ms.visit(m, t) |
| 126 | |
| 127 | // Obtain a list of oneof wrapper types. |
| 128 | var oneofWrappers []reflect.Type |
| 129 | if fn, ok := t.MethodByName("XXX_OneofFuncs"); ok { |
| 130 | vs := fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[3] |
| 131 | for _, v := range vs.Interface().([]interface{}) { |
| 132 | oneofWrappers = append(oneofWrappers, reflect.TypeOf(v)) |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | // Obtain a list of the extension ranges. |
| 137 | if fn, ok := t.MethodByName("ExtensionRangeArray"); ok { |
| 138 | vs := fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[0] |
| 139 | for i := 0; i < vs.Len(); i++ { |
| 140 | v := vs.Index(i) |
| 141 | m.ExtensionRanges = append(m.ExtensionRanges, [2]pref.FieldNumber{ |
| 142 | pref.FieldNumber(v.FieldByName("Start").Int()), |
| 143 | pref.FieldNumber(v.FieldByName("End").Int() + 1), |
| 144 | }) |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | // Derive the message fields by inspecting the struct fields. |
| 149 | for i := 0; i < t.Elem().NumField(); i++ { |
| 150 | f := t.Elem().Field(i) |
| 151 | if tag := f.Tag.Get("protobuf"); tag != "" { |
| 152 | tagKey := f.Tag.Get("protobuf_key") |
| 153 | tagVal := f.Tag.Get("protobuf_val") |
| 154 | m.Fields = append(m.Fields, ms.parseField(tag, tagKey, tagVal, f.Type, m)) |
| 155 | } |
| 156 | if tag := f.Tag.Get("protobuf_oneof"); tag != "" { |
| 157 | name := pref.Name(tag) |
| 158 | m.Oneofs = append(m.Oneofs, ptype.Oneof{Name: name}) |
| 159 | for _, t := range oneofWrappers { |
| 160 | if t.Implements(f.Type) { |
| 161 | f := t.Elem().Field(0) |
| 162 | if tag := f.Tag.Get("protobuf"); tag != "" { |
| 163 | ft := ms.parseField(tag, "", "", f.Type, m) |
| 164 | ft.OneofName = name |
| 165 | m.Fields = append(m.Fields, ft) |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | return ptype.PlaceholderMessage(m.FullName) |
| 173 | } |
| 174 | |
Joe Tsai | 05828db | 2018-11-01 13:52:16 -0700 | [diff] [blame^] | 175 | func (ms *messageDescSet) parseField(tag, tagKey, tagVal string, goType reflect.Type, parent *ptype.StandaloneMessage) ptype.Field { |
| 176 | t := goType |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 177 | isOptional := t.Kind() == reflect.Ptr && t.Elem().Kind() != reflect.Struct |
| 178 | isRepeated := t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8 |
| 179 | if isOptional || isRepeated { |
| 180 | t = t.Elem() |
| 181 | } |
Joe Tsai | 05828db | 2018-11-01 13:52:16 -0700 | [diff] [blame^] | 182 | f := ptag.Unmarshal(tag, t) |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 183 | |
| 184 | // Populate EnumType and MessageType. |
| 185 | if f.EnumType == nil && f.Kind == pref.EnumKind { |
| 186 | if ev, ok := reflect.Zero(t).Interface().(pref.ProtoEnum); ok { |
| 187 | f.EnumType = ev.ProtoReflect().Type() |
| 188 | } else { |
| 189 | f.EnumType = loadEnumDesc(t) |
| 190 | } |
| 191 | } |
| 192 | if f.MessageType == nil && (f.Kind == pref.MessageKind || f.Kind == pref.GroupKind) { |
| 193 | if mv, ok := reflect.Zero(t).Interface().(pref.ProtoMessage); ok { |
| 194 | f.MessageType = mv.ProtoReflect().Type() |
| 195 | } else if t.Kind() == reflect.Map { |
| 196 | m := &ptype.StandaloneMessage{ |
Damien Neil | 204f1c0 | 2018-10-23 15:03:38 -0700 | [diff] [blame] | 197 | Syntax: parent.Syntax, |
| 198 | FullName: parent.FullName.Append(mapEntryName(f.Name)), |
| 199 | Options: &descriptorV1.MessageOptions{MapEntry: protoV1.Bool(true)}, |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 200 | Fields: []ptype.Field{ |
| 201 | ms.parseField(tagKey, "", "", t.Key(), nil), |
| 202 | ms.parseField(tagVal, "", "", t.Elem(), nil), |
| 203 | }, |
| 204 | } |
| 205 | ms.visit(m, t) |
| 206 | f.MessageType = ptype.PlaceholderMessage(m.FullName) |
| 207 | } else if mv, ok := messageDescCache.Load(t); ok { |
| 208 | f.MessageType = mv.(pref.MessageDescriptor) |
| 209 | } else { |
| 210 | f.MessageType = ms.processMessage(t) |
| 211 | } |
| 212 | } |
| 213 | return f |
| 214 | } |
| 215 | |
| 216 | func (ms *messageDescSet) visit(m *ptype.StandaloneMessage, t reflect.Type) { |
| 217 | if ms.visited == nil { |
| 218 | ms.visited = make(map[reflect.Type]*ptype.StandaloneMessage) |
| 219 | } |
| 220 | if t.Kind() != reflect.Map { |
| 221 | ms.visited[t] = m |
| 222 | } |
| 223 | ms.descs = append(ms.descs, m) |
| 224 | ms.types = append(ms.types, t) |
| 225 | } |
| 226 | |
| 227 | // deriveFullName derives a fully qualified protobuf name for the given Go type |
| 228 | // The provided name is not guaranteed to be stable nor universally unique. |
| 229 | // It should be sufficiently unique within a program. |
| 230 | func deriveFullName(t reflect.Type) pref.FullName { |
| 231 | sanitize := func(r rune) rune { |
| 232 | switch { |
| 233 | case r == '/': |
| 234 | return '.' |
| 235 | case 'a' <= r && r <= 'z', 'A' <= r && r <= 'Z', '0' <= r && r <= '9': |
| 236 | return r |
| 237 | default: |
| 238 | return '_' |
| 239 | } |
| 240 | } |
| 241 | prefix := strings.Map(sanitize, t.PkgPath()) |
| 242 | suffix := strings.Map(sanitize, t.Name()) |
| 243 | if suffix == "" { |
| 244 | suffix = fmt.Sprintf("UnknownX%X", reflect.ValueOf(t).Pointer()) |
| 245 | } |
| 246 | |
| 247 | ss := append(strings.Split(prefix, "."), suffix) |
| 248 | for i, s := range ss { |
| 249 | if s == "" || ('0' <= s[0] && s[0] <= '9') { |
| 250 | ss[i] = "x" + s |
| 251 | } |
| 252 | } |
| 253 | return pref.FullName(strings.Join(ss, ".")) |
| 254 | } |
| 255 | |
| 256 | // mapEntryName derives the message name for a map field of a given name. |
| 257 | // This is identical to MapEntryName from parser.cc in the protoc source. |
| 258 | func mapEntryName(s pref.Name) pref.Name { |
| 259 | var b []byte |
| 260 | nextUpper := true |
| 261 | for i := 0; i < len(s); i++ { |
| 262 | if c := s[i]; c == '_' { |
| 263 | nextUpper = true |
| 264 | } else { |
| 265 | if nextUpper { |
| 266 | c = byte(unicode.ToUpper(rune(c))) |
| 267 | nextUpper = false |
| 268 | } |
| 269 | b = append(b, c) |
| 270 | } |
| 271 | } |
| 272 | return pref.Name(append(b, "Entry"...)) |
| 273 | } |