/
oneof.go
57 lines (47 loc) · 1.52 KB
/
oneof.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package internal
import (
"google.golang.org/protobuf/compiler/protogen"
"github.com/dane/protoc-gen-go-svc/internal/options"
)
// NewOneOf creates a `OneOf`. An error will be returned if the oneof
// cannot be created for any reason.
func NewOneOf(svc *Service, msg *Message, oneof *protogen.Oneof) (*Field, error) {
f := &Field{
IsPrivate: msg.IsPrivate,
IsLatest: msg.IsLatest,
IsOneOf: true,
IsRequired: options.IsRequiredOneOf(oneof),
IsDeprecated: options.IsDeprecatedOneOf(oneof),
Name: oneof.GoName,
Type: OneOfType,
}
// Assign the private oneof and next oneof. This is only done if the oneof
// isn't private since the private service, message, fields, etc. are the
// first in the chain.
if !f.IsPrivate {
fieldName := options.OneOfName(oneof)
var ok bool
// Latest and deprecated oneofs chain directly to the private service.
// They don't have a "next" service.
if !f.IsLatest && !f.IsDeprecated && !msg.IsDeprecated {
f.Next, ok = msg.Next.FieldByName[fieldName]
if !ok {
return nil, NewErrFieldNotFound(fieldName, msg.Next)
}
}
f.Private, ok = msg.Private.FieldByName[fieldName]
if !ok {
return nil, NewErrFieldNotFound(fieldName, msg.Private)
}
for _, field := range oneof.Fields {
msg := svc.MessageByName[messageKey(field.Message)]
f.Messages = append(f.Messages, msg)
}
}
rules, err := NewRules(f, options.OneOfValidate(oneof))
if err != nil {
return nil, NewErrCreateField(f, msg, err)
}
f.Rules = rules
return f, nil
}