adding desribe with json output
This commit is contained in:
parent
7e4045565f
commit
6056219845
|
|
@ -4,6 +4,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
|
@ -98,6 +99,8 @@ var (
|
||||||
Enable verbose output.`))
|
Enable verbose output.`))
|
||||||
serverName = flags.String("servername", "", prettify(`
|
serverName = flags.String("servername", "", prettify(`
|
||||||
Override server name when validating TLS certificate.`))
|
Override server name when validating TLS certificate.`))
|
||||||
|
describeFormat = flags.String("describe-format", "text", prettify(`
|
||||||
|
the format to output describe definitions.`))
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
@ -144,6 +147,28 @@ func init() {
|
||||||
|
|
||||||
type multiString []string
|
type multiString []string
|
||||||
|
|
||||||
|
type SymbolElementType string
|
||||||
|
|
||||||
|
var (
|
||||||
|
Message = SymbolElementType("Message")
|
||||||
|
Field = SymbolElementType("Field")
|
||||||
|
OneOf = SymbolElementType("OneOf")
|
||||||
|
Enum = SymbolElementType("Enum")
|
||||||
|
EnumValue = SymbolElementType("EnumValue")
|
||||||
|
Service = SymbolElementType("Service")
|
||||||
|
Method = SymbolElementType("Method")
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
type symbolDescription struct {
|
||||||
|
FullyQualifiedName string
|
||||||
|
ElementType SymbolElementType
|
||||||
|
elementTypeDescription string
|
||||||
|
ProtoDefinition string
|
||||||
|
Template string
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
func (s *multiString) String() string {
|
func (s *multiString) String() string {
|
||||||
return strings.Join(*s, ",")
|
return strings.Join(*s, ",")
|
||||||
}
|
}
|
||||||
|
|
@ -191,11 +216,14 @@ func main() {
|
||||||
fail(nil, "The -cert and -key arguments must be used together and both be present.")
|
fail(nil, "The -cert and -key arguments must be used together and both be present.")
|
||||||
}
|
}
|
||||||
if *format != "json" && *format != "text" {
|
if *format != "json" && *format != "text" {
|
||||||
fail(nil, "The -format option must be 'json' or 'text.")
|
fail(nil, "The -format option must be 'json' or 'text'.")
|
||||||
}
|
}
|
||||||
if *emitDefaults && *format != "json" {
|
if *emitDefaults && *format != "json" {
|
||||||
warn("The -emit-defaults is only used when using json format.")
|
warn("The -emit-defaults is only used when using json format.")
|
||||||
}
|
}
|
||||||
|
if *describeFormat != "text" && *describeFormat != "json" {
|
||||||
|
fail(nil, "The -describe-format option must be 'text' or 'json'.")
|
||||||
|
}
|
||||||
|
|
||||||
args := flags.Args()
|
args := flags.Args()
|
||||||
|
|
||||||
|
|
@ -381,6 +409,7 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if describe {
|
} else if describe {
|
||||||
|
var symbolMap = make(map[string]symbolDescription)
|
||||||
var symbols []string
|
var symbols []string
|
||||||
if symbol != "" {
|
if symbol != "" {
|
||||||
symbols = []string{symbol}
|
symbols = []string{symbol}
|
||||||
|
|
@ -406,17 +435,20 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
fqn := dsc.GetFullyQualifiedName()
|
fqn := dsc.GetFullyQualifiedName()
|
||||||
var elementType string
|
currentSymbol := symbolDescription{FullyQualifiedName:fqn}
|
||||||
|
|
||||||
|
var elementTypeDescription string
|
||||||
switch d := dsc.(type) {
|
switch d := dsc.(type) {
|
||||||
case *desc.MessageDescriptor:
|
case *desc.MessageDescriptor:
|
||||||
elementType = "a message"
|
currentSymbol.ElementType = Message
|
||||||
|
elementTypeDescription = "a message"
|
||||||
parent, ok := d.GetParent().(*desc.MessageDescriptor)
|
parent, ok := d.GetParent().(*desc.MessageDescriptor)
|
||||||
if ok {
|
if ok {
|
||||||
if d.IsMapEntry() {
|
if d.IsMapEntry() {
|
||||||
for _, f := range parent.GetFields() {
|
for _, f := range parent.GetFields() {
|
||||||
if f.IsMap() && f.GetMessageType() == d {
|
if f.IsMap() && f.GetMessageType() == d {
|
||||||
// found it: describe the map field instead
|
// found it: describe the map field instead
|
||||||
elementType = "the entry type for a map field"
|
elementTypeDescription = "the entry type for a map field"
|
||||||
dsc = f
|
dsc = f
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
@ -426,7 +458,7 @@ func main() {
|
||||||
for _, f := range parent.GetFields() {
|
for _, f := range parent.GetFields() {
|
||||||
if f.GetType() == descpb.FieldDescriptorProto_TYPE_GROUP && f.GetMessageType() == d {
|
if f.GetType() == descpb.FieldDescriptorProto_TYPE_GROUP && f.GetMessageType() == d {
|
||||||
// found it: describe the map field instead
|
// found it: describe the map field instead
|
||||||
elementType = "the type of a group field"
|
elementTypeDescription = "the type of a group field"
|
||||||
dsc = f
|
dsc = f
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
@ -434,22 +466,28 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case *desc.FieldDescriptor:
|
case *desc.FieldDescriptor:
|
||||||
elementType = "a field"
|
currentSymbol.ElementType = Field
|
||||||
|
elementTypeDescription = "a field"
|
||||||
if d.GetType() == descpb.FieldDescriptorProto_TYPE_GROUP {
|
if d.GetType() == descpb.FieldDescriptorProto_TYPE_GROUP {
|
||||||
elementType = "a group field"
|
elementTypeDescription = "a group field"
|
||||||
} else if d.IsExtension() {
|
} else if d.IsExtension() {
|
||||||
elementType = "an extension"
|
elementTypeDescription = "an extension"
|
||||||
}
|
}
|
||||||
case *desc.OneOfDescriptor:
|
case *desc.OneOfDescriptor:
|
||||||
elementType = "a one-of"
|
currentSymbol.ElementType = OneOf
|
||||||
|
elementTypeDescription = "a one-of"
|
||||||
case *desc.EnumDescriptor:
|
case *desc.EnumDescriptor:
|
||||||
elementType = "an enum"
|
currentSymbol.ElementType = Enum
|
||||||
|
elementTypeDescription = "an enum"
|
||||||
case *desc.EnumValueDescriptor:
|
case *desc.EnumValueDescriptor:
|
||||||
elementType = "an enum value"
|
currentSymbol.ElementType = EnumValue
|
||||||
|
elementTypeDescription = "an enum value"
|
||||||
case *desc.ServiceDescriptor:
|
case *desc.ServiceDescriptor:
|
||||||
elementType = "a service"
|
currentSymbol.ElementType = Service
|
||||||
|
elementTypeDescription = "a service"
|
||||||
case *desc.MethodDescriptor:
|
case *desc.MethodDescriptor:
|
||||||
elementType = "a method"
|
currentSymbol.ElementType = Method
|
||||||
|
elementTypeDescription = "a method"
|
||||||
default:
|
default:
|
||||||
err = fmt.Errorf("descriptor has unrecognized type %T", dsc)
|
err = fmt.Errorf("descriptor has unrecognized type %T", dsc)
|
||||||
fail(err, "Failed to describe symbol %q", s)
|
fail(err, "Failed to describe symbol %q", s)
|
||||||
|
|
@ -459,8 +497,9 @@ func main() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fail(err, "Failed to describe symbol %q", s)
|
fail(err, "Failed to describe symbol %q", s)
|
||||||
}
|
}
|
||||||
fmt.Printf("%s is %s:\n", fqn, elementType)
|
|
||||||
fmt.Println(txt)
|
currentSymbol.elementTypeDescription = elementTypeDescription
|
||||||
|
currentSymbol.ProtoDefinition = txt
|
||||||
|
|
||||||
if dsc, ok := dsc.(*desc.MessageDescriptor); ok && *msgTemplate {
|
if dsc, ok := dsc.(*desc.MessageDescriptor); ok && *msgTemplate {
|
||||||
// for messages, also show a template in JSON, to make it easier to
|
// for messages, also show a template in JSON, to make it easier to
|
||||||
|
|
@ -474,9 +513,25 @@ func main() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fail(err, "Failed to print template for message %s", s)
|
fail(err, "Failed to print template for message %s", s)
|
||||||
}
|
}
|
||||||
fmt.Println("\nMessage template:")
|
currentSymbol.Template = str
|
||||||
fmt.Println(str)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
symbolMap[fqn] = currentSymbol
|
||||||
|
}
|
||||||
|
|
||||||
|
if *describeFormat == "text" {
|
||||||
|
for _, data := range symbolMap {
|
||||||
|
fmt.Printf("%s is %s:\n", data.FullyQualifiedName, data.elementTypeDescription)
|
||||||
|
fmt.Println(data.ProtoDefinition)
|
||||||
|
if data.Template != "" {
|
||||||
|
fmt.Println("\nMessage template:")
|
||||||
|
fmt.Println(data.Template)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
data, _ := json.Marshal(symbolMap)
|
||||||
|
fmt.Println(string(data))
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue