| OLD | NEW |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package main | 5 package main |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "fmt" | 8 "fmt" |
| 9 "io" | 9 "io" |
| 10 "strings" | 10 "strings" |
| 11 | 11 |
| 12 "github.com/luci/luci-go/common/proto/google/descutil" |
| 13 |
| 12 "github.com/luci/luci-go/common/data/text/indented" | 14 "github.com/luci/luci-go/common/data/text/indented" |
| 13 » "github.com/luci/luci-go/common/proto/google/descriptor" | 15 » "google.golang.org/genproto/protobuf" |
| 14 ) | 16 ) |
| 15 | 17 |
| 16 // printer prints a proto3 definition from a description. | 18 // printer prints a proto3 definition from a description. |
| 17 // Does not support options. | 19 // Does not support options. |
| 18 type printer struct { | 20 type printer struct { |
| 19 // File is the file containing the desriptors being printed. | 21 // File is the file containing the desriptors being printed. |
| 20 // Used to relativize names and print comments. | 22 // Used to relativize names and print comments. |
| 21 File *descriptor.FileDescriptorProto | 23 File *descriptor.FileDescriptorProto |
| 22 Out indented.Writer | 24 Out indented.Writer |
| 23 | 25 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 // MaybeLeadingComments prints leading comments of the protobuf entity | 59 // MaybeLeadingComments prints leading comments of the protobuf entity |
| 58 // at path, if found. | 60 // at path, if found. |
| 59 // | 61 // |
| 60 // For path, see comment in SourceCodeInfo.Location message in | 62 // For path, see comment in SourceCodeInfo.Location message in |
| 61 // common/proto/google/descriptor/descriptor.proto. | 63 // common/proto/google/descriptor/descriptor.proto. |
| 62 func (p *printer) MaybeLeadingComments(path []int) { | 64 func (p *printer) MaybeLeadingComments(path []int) { |
| 63 if p.File == nil || p.File.SourceCodeInfo == nil || len(path) == 0 { | 65 if p.File == nil || p.File.SourceCodeInfo == nil || len(path) == 0 { |
| 64 return | 66 return |
| 65 } | 67 } |
| 66 | 68 |
| 67 » loc := p.File.SourceCodeInfo.FindLocation(path) | 69 » loc := descutil.FindLocation(p.File.SourceCodeInfo, path) |
| 68 if loc == nil { | 70 if loc == nil { |
| 69 return | 71 return |
| 70 } | 72 } |
| 71 | 73 |
| 72 comments := loc.GetLeadingComments() | 74 comments := loc.GetLeadingComments() |
| 73 // print comments, but insert "//" before each newline. | 75 // print comments, but insert "//" before each newline. |
| 74 for len(comments) > 0 { | 76 for len(comments) > 0 { |
| 75 var toPrint string | 77 var toPrint string |
| 76 if lineEnd := strings.Index(comments, "\n"); lineEnd >= 0 { | 78 if lineEnd := strings.Index(comments, "\n"); lineEnd >= 0 { |
| 77 toPrint = comments[:lineEnd+1] // includes newline | 79 toPrint = comments[:lineEnd+1] // includes newline |
| (...skipping 16 matching lines...) Expand all Loading... |
| 94 } | 96 } |
| 95 return name | 97 return name |
| 96 } | 98 } |
| 97 | 99 |
| 98 // Service prints a service definition. | 100 // Service prints a service definition. |
| 99 // If methodIndex != -1, only one method is printed. | 101 // If methodIndex != -1, only one method is printed. |
| 100 // If serviceIndex != -1, leading comments are printed if found. | 102 // If serviceIndex != -1, leading comments are printed if found. |
| 101 func (p *printer) Service(service *descriptor.ServiceDescriptorProto, serviceInd
ex, methodIndex int) { | 103 func (p *printer) Service(service *descriptor.ServiceDescriptorProto, serviceInd
ex, methodIndex int) { |
| 102 var path []int | 104 var path []int |
| 103 if serviceIndex != -1 { | 105 if serviceIndex != -1 { |
| 104 » » path = []int{descriptor.NumberFileDescriptorProto_Service, servi
ceIndex} | 106 » » path = []int{descutil.FileDescriptorProtoServiceTag, serviceInde
x} |
| 105 p.MaybeLeadingComments(path) | 107 p.MaybeLeadingComments(path) |
| 106 } | 108 } |
| 107 defer p.open("service %s", service.GetName())() | 109 defer p.open("service %s", service.GetName())() |
| 108 | 110 |
| 109 printMethod := func(i int) { | 111 printMethod := func(i int) { |
| 110 var methodPath []int | 112 var methodPath []int |
| 111 if path != nil { | 113 if path != nil { |
| 112 » » » methodPath = append(path, descriptor.NumberServiceDescri
ptorProto_Method, i) | 114 » » » methodPath = append(path, descutil.ServiceDescriptorProt
oMethodTag, i) |
| 113 } | 115 } |
| 114 p.Method(service.Method[i], methodPath) | 116 p.Method(service.Method[i], methodPath) |
| 115 } | 117 } |
| 116 | 118 |
| 117 if methodIndex < 0 { | 119 if methodIndex < 0 { |
| 118 for i := range service.Method { | 120 for i := range service.Method { |
| 119 printMethod(i) | 121 printMethod(i) |
| 120 } | 122 } |
| 121 } else { | 123 } else { |
| 122 printMethod(methodIndex) | 124 printMethod(methodIndex) |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 descriptor.FieldDescriptorProto_TYPE_SINT64: "sint64", | 161 descriptor.FieldDescriptorProto_TYPE_SINT64: "sint64", |
| 160 } | 162 } |
| 161 | 163 |
| 162 // Field prints a field definition. | 164 // Field prints a field definition. |
| 163 // | 165 // |
| 164 // If path is specified, leading comments are printed if found. | 166 // If path is specified, leading comments are printed if found. |
| 165 // See also comment in SourceCodeInfo.Location message in | 167 // See also comment in SourceCodeInfo.Location message in |
| 166 // common/proto/google/descriptor/descriptor.proto. | 168 // common/proto/google/descriptor/descriptor.proto. |
| 167 func (p *printer) Field(field *descriptor.FieldDescriptorProto, path []int) { | 169 func (p *printer) Field(field *descriptor.FieldDescriptorProto, path []int) { |
| 168 p.MaybeLeadingComments(path) | 170 p.MaybeLeadingComments(path) |
| 169 » if field.Repeated() { | 171 » if descutil.Repeated(field) { |
| 170 p.Printf("repeated ") | 172 p.Printf("repeated ") |
| 171 } | 173 } |
| 172 | 174 |
| 173 typeName := fieldTypeName[field.GetType()] | 175 typeName := fieldTypeName[field.GetType()] |
| 174 if typeName == "" { | 176 if typeName == "" { |
| 175 typeName = p.shorten(field.GetTypeName()) | 177 typeName = p.shorten(field.GetTypeName()) |
| 176 } | 178 } |
| 177 if typeName == "" { | 179 if typeName == "" { |
| 178 typeName = "<unsupported type>" | 180 typeName = "<unsupported type>" |
| 179 } | 181 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 190 defer p.open("message %s", msg.GetName())() | 192 defer p.open("message %s", msg.GetName())() |
| 191 | 193 |
| 192 for i := range msg.GetOneofDecl() { | 194 for i := range msg.GetOneofDecl() { |
| 193 p.OneOf(msg, i, path) | 195 p.OneOf(msg, i, path) |
| 194 } | 196 } |
| 195 | 197 |
| 196 for i, f := range msg.Field { | 198 for i, f := range msg.Field { |
| 197 if f.OneofIndex == nil { | 199 if f.OneofIndex == nil { |
| 198 var fieldPath []int | 200 var fieldPath []int |
| 199 if len(path) > 0 { | 201 if len(path) > 0 { |
| 200 » » » » fieldPath = append(path, descriptor.NumberDescri
ptorProto_Field, i) | 202 » » » » fieldPath = append(path, descutil.DescriptorProt
oFieldTag, i) |
| 201 } | 203 } |
| 202 p.Field(msg.Field[i], fieldPath) | 204 p.Field(msg.Field[i], fieldPath) |
| 203 } | 205 } |
| 204 } | 206 } |
| 205 } | 207 } |
| 206 | 208 |
| 207 // OneOf prints a oneof definition. | 209 // OneOf prints a oneof definition. |
| 208 // | 210 // |
| 209 // If path is specified, leading comments are printed if found. | 211 // If path is specified, leading comments are printed if found. |
| 210 // See also comment in SourceCodeInfo.Location message in | 212 // See also comment in SourceCodeInfo.Location message in |
| 211 // common/proto/google/descriptor/descriptor.proto. | 213 // common/proto/google/descriptor/descriptor.proto. |
| 212 func (p *printer) OneOf(msg *descriptor.DescriptorProto, oneOfIndex int, msgPath
[]int) { | 214 func (p *printer) OneOf(msg *descriptor.DescriptorProto, oneOfIndex int, msgPath
[]int) { |
| 213 of := msg.GetOneofDecl()[oneOfIndex] | 215 of := msg.GetOneofDecl()[oneOfIndex] |
| 214 if len(msgPath) > 0 { | 216 if len(msgPath) > 0 { |
| 215 » » p.MaybeLeadingComments(append(msgPath, descriptor.NumberDescript
orProto_OneOf, oneOfIndex)) | 217 » » p.MaybeLeadingComments(append(msgPath, descutil.DescriptorProtoO
neOfTag, oneOfIndex)) |
| 216 } | 218 } |
| 217 defer p.open("oneof %s", of.GetName())() | 219 defer p.open("oneof %s", of.GetName())() |
| 218 | 220 |
| 219 for i, f := range msg.Field { | 221 for i, f := range msg.Field { |
| 220 if f.OneofIndex != nil && int(f.GetOneofIndex()) == oneOfIndex { | 222 if f.OneofIndex != nil && int(f.GetOneofIndex()) == oneOfIndex { |
| 221 var fieldPath []int | 223 var fieldPath []int |
| 222 if len(msgPath) > 0 { | 224 if len(msgPath) > 0 { |
| 223 » » » » fieldPath = append(msgPath, descriptor.NumberDes
criptorProto_Field, i) | 225 » » » » fieldPath = append(msgPath, descutil.DescriptorP
rotoFieldTag, i) |
| 224 } | 226 } |
| 225 p.Field(msg.Field[i], fieldPath) | 227 p.Field(msg.Field[i], fieldPath) |
| 226 } | 228 } |
| 227 } | 229 } |
| 228 } | 230 } |
| 229 | 231 |
| 230 // Enum prints an enum definition. | 232 // Enum prints an enum definition. |
| 231 // | 233 // |
| 232 // If path is specified, leading comments are printed if found. | 234 // If path is specified, leading comments are printed if found. |
| 233 // See also comment in SourceCodeInfo.Location message in | 235 // See also comment in SourceCodeInfo.Location message in |
| 234 // common/proto/google/descriptor/descriptor.proto. | 236 // common/proto/google/descriptor/descriptor.proto. |
| 235 func (p *printer) Enum(enum *descriptor.EnumDescriptorProto, path []int) { | 237 func (p *printer) Enum(enum *descriptor.EnumDescriptorProto, path []int) { |
| 236 p.MaybeLeadingComments(path) | 238 p.MaybeLeadingComments(path) |
| 237 defer p.open("enum %s", enum.GetName())() | 239 defer p.open("enum %s", enum.GetName())() |
| 238 | 240 |
| 239 for i, v := range enum.Value { | 241 for i, v := range enum.Value { |
| 240 var valuePath []int | 242 var valuePath []int |
| 241 if len(path) > 0 { | 243 if len(path) > 0 { |
| 242 » » » valuePath = append(path, descriptor.NumberEnumDescriptor
Proto_Value, i) | 244 » » » valuePath = append(path, descutil.EnumDescriptorProtoVal
ueTag, i) |
| 243 } | 245 } |
| 244 p.EnumValue(v, valuePath) | 246 p.EnumValue(v, valuePath) |
| 245 } | 247 } |
| 246 } | 248 } |
| 247 | 249 |
| 248 // EnumValue prints an enum value definition. | 250 // EnumValue prints an enum value definition. |
| 249 // | 251 // |
| 250 // If path is specified, leading comments are printed if found. | 252 // If path is specified, leading comments are printed if found. |
| 251 // See also comment in SourceCodeInfo.Location message in | 253 // See also comment in SourceCodeInfo.Location message in |
| 252 // common/proto/google/descriptor/descriptor.proto. | 254 // common/proto/google/descriptor/descriptor.proto. |
| 253 func (p *printer) EnumValue(v *descriptor.EnumValueDescriptorProto, path []int)
{ | 255 func (p *printer) EnumValue(v *descriptor.EnumValueDescriptorProto, path []int)
{ |
| 254 p.MaybeLeadingComments(path) | 256 p.MaybeLeadingComments(path) |
| 255 p.Printf("%s = %d;\n", v.GetName(), v.GetNumber()) | 257 p.Printf("%s = %d;\n", v.GetName(), v.GetNumber()) |
| 256 } | 258 } |
| OLD | NEW |