-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmessage_build_context.go
153 lines (122 loc) · 3.88 KB
/
message_build_context.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
Copyright 2015-2021 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"strconv"
"strings"
"github.com/gogo/protobuf/protoc-gen-gogo/descriptor"
"github.com/gogo/protobuf/protoc-gen-gogo/generator"
"github.com/stoewer/go-strcase"
)
const (
rootPackage = "." // indicates that a message belongs to the topmost package
)
// MessageBuildContext represents an utilty struct to pass metadata as a build functions parameter
type MessageBuildContext struct {
plugin *Plugin
imports *Imports
config *Config // Syntax shortcut
gen *generator.Generator
desc *generator.Descriptor
path string
namePath string
}
// NewMessageBuildContext creates new message build context
func NewMessageBuildContext(plugin *Plugin, desc *generator.Descriptor, path string) MessageBuildContext {
namePath := path
if path == "" {
namePath = desc.GetName()
}
// Split package name and all dots
i := strings.Index(path, ".")
if i > -1 {
namePath = namePath[i:]
}
namePath = strings.Replace(namePath, ".", "", -1)
return MessageBuildContext{plugin, plugin.GetImports(), plugin.GetConfig(), plugin.GetGenerator(), desc, path, namePath}
}
// GetGoType returns go type for a message
func (c MessageBuildContext) GetGoType() string {
name := c.desc.GetName()
if c.config.DefaultPackageName == "" {
return name
}
return c.config.DefaultPackageName + "." + name
}
// GetComment returns the message source code comment and it's raw text
func (c MessageBuildContext) GetComment() string {
p := c.desc.Path()
for _, l := range c.desc.File().GetSourceCodeInfo().GetLocation() {
if c.GetLocationPath(l) == p {
c := Comment(strings.Trim(l.GetLeadingComments(), "\n"))
return c.ToSingleLine()
}
}
return ""
}
// IsExcluded returns true if the message is not specified in the type list
func (c MessageBuildContext) IsExcluded() bool {
_, ok := c.config.Types[c.GetPath()]
return !ok
}
// GetName returns the message name
func (c MessageBuildContext) GetName() string {
return c.desc.GetName()
}
// GetPath returns the path to the message
func (c MessageBuildContext) GetPath() string {
if c.path == "" {
name := c.desc.GetName()
if c.desc.GoImportPath() == rootPackage {
return name
}
return c.desc.File().GetPackage() + "." + name
}
return c.path
}
// GetNamePath returns the path to the message
func (c MessageBuildContext) GetNamePath() string {
return c.namePath
}
// GetLocationPath returns the source line path for a given source code location.
func (c MessageBuildContext) GetLocationPath(l *descriptor.SourceCodeInfo_Location) string {
s := make([]string, len(l.GetPath()))
for i, v := range l.GetPath() {
s[i] = strconv.Itoa(int(v))
}
return strings.Join(s, ",")
}
// GetInjectedFields returns array of injected fields
func (c *MessageBuildContext) GetInjectedFields() []InjectedField {
v, ok := c.config.InjectedFields[c.GetPath()]
if ok {
return v
}
return []InjectedField{}
}
// GetOneOfNames returns the names of OneOf groups in this message
func (c *MessageBuildContext) GetOneOfNames() []string {
s := make([]string, len(c.desc.OneofDecl))
for i, d := range c.desc.OneofDecl {
name := d.GetName()
if name[0:1] == strings.ToLower(name[0:1]) {
name = strcase.UpperCamelCase(name)
}
s[i] = name
}
return s
}
// IsEmpty returns true if the message has no fields defined
func (c *MessageBuildContext) IsEmpty() bool {
return len(c.desc.GetField()) == 0
}