MCPcopy
hub / github.com/protocolbuffers/protobuf-go / Example_formatText

Function Example_formatText

reflect/protorange/example_test.go:107–228  ·  view source on GitHub ↗

Implement a basic text formatter by ranging through all populated values in a message in depth-first order.

()

Source from the content-addressed store, hash-verified

105// Implement a basic text formatter by ranging through all populated values
106// in a message in depth-first order.
107func Example_formatText() {
108 m := &newspb.Article{
109 Author: "Brad Fitzpatrick",
110 Date: timestamppb.New(time.Date(2018, time.February, 16, 0, 0, 0, 0, time.UTC)),
111 Title: "Go 1.10 is released",
112 Content: "Happy Friday, happy weekend! Today the Go team is happy to announce the release of Go 1.10...",
113 Status: newspb.Article_PUBLISHED,
114 Tags: []string{"go1.10", "release"},
115 Attachments: []*anypb.Any{{
116 TypeUrl: "google.golang.org.KeyValueAttachment",
117 Value: mustMarshal(&newspb.KeyValueAttachment{
118 Name: "checksums.txt",
119 Data: map[string]string{
120 "go1.10.src.tar.gz": "07cbb9d0091b846c6aea40bf5bc0cea7",
121 "go1.10.darwin-amd64.pkg": "cbb38bb6ff6ea86279e01745984445bf",
122 "go1.10.linux-amd64.tar.gz": "6b3d0e4a5c77352cf4275573817f7566",
123 "go1.10.windows-amd64.msi": "57bda02030f58f5d2bf71943e1390123",
124 },
125 }),
126 }},
127 }
128
129 // Print a message in a humanly readable format.
130 var indent []byte
131 protorange.Options{
132 Stable: true,
133 }.Range(m.ProtoReflect(),
134 func(p protopath.Values) error {
135 // Print the key.
136 var fd protoreflect.FieldDescriptor
137 last := p.Index(-1)
138 beforeLast := p.Index(-2)
139 switch last.Step.Kind() {
140 case protopath.FieldAccessStep:
141 fd = last.Step.FieldDescriptor()
142 fmt.Printf("%s%s: ", indent, fd.Name())
143 case protopath.ListIndexStep:
144 fd = beforeLast.Step.FieldDescriptor() // lists always appear in the context of a repeated field
145 fmt.Printf("%s%d: ", indent, last.Step.ListIndex())
146 case protopath.MapIndexStep:
147 fd = beforeLast.Step.FieldDescriptor() // maps always appear in the context of a repeated field
148 fmt.Printf("%s%v: ", indent, last.Step.MapIndex().Interface())
149 case protopath.AnyExpandStep:
150 fmt.Printf("%s[%v]: ", indent, last.Value.Message().Descriptor().FullName())
151 case protopath.UnknownAccessStep:
152 fmt.Printf("%s?: ", indent)
153 }
154
155 // Starting printing the value.
156 switch v := last.Value.Interface().(type) {
157 case protoreflect.Message:
158 fmt.Printf("{\n")
159 indent = append(indent, '\t')
160 case protoreflect.List:
161 fmt.Printf("[\n")
162 indent = append(indent, '\t')
163 case protoreflect.Map:
164 fmt.Printf("{\n")

Callers

nothing calls this directly

Calls 15

ProtoReflectMethod · 0.95
EnumMethod · 0.95
NewFunction · 0.92
FieldDescriptorMethod · 0.80
ListIndexMethod · 0.80
MapIndexMethod · 0.80
mustMarshalFunction · 0.70
RangeMethod · 0.65
IndexMethod · 0.65
KindMethod · 0.65
NameMethod · 0.65
InterfaceMethod · 0.65

Tested by

no test coverage detected