-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"entries": [ | ||
{ | ||
"equal": { | ||
"type": "string", | ||
"value": "[*wrapperspb.StringValue] {\n\tvalue: [string] (len=4) \"test\",\n}" | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package protobuf | ||
|
||
import ( | ||
"reflect" | ||
|
||
"github.com/pierrre/pretty" | ||
"github.com/pierrre/pretty/internal/indent" | ||
"github.com/pierrre/pretty/internal/must" | ||
"github.com/pierrre/pretty/internal/write" | ||
"google.golang.org/protobuf/reflect/protoreflect" | ||
) | ||
|
||
type ValueWriter struct { | ||
pretty.ValueWriter | ||
} | ||
|
||
// WriteValue implements [pretty.ValueWriter]. | ||
func (vw *ValueWriter) WriteValue(st *pretty.State, v reflect.Value) bool { | ||
if !v.CanInterface() { | ||
return false | ||
} | ||
pm, ok := v.Interface().(protoreflect.ProtoMessage) | ||
if !ok { | ||
return false | ||
} | ||
m := pm.ProtoReflect() | ||
defer st.SetRestoreKnownType(false)() // We want to show the types of fields and values. | ||
write.MustString(st.Writer, "{") | ||
fs := m.Descriptor().Fields() | ||
l := fs.Len() | ||
hasFields := false | ||
st.IndentLevel++ | ||
for i := range l { | ||
fd := fs.Get(i) | ||
if !hasFields { | ||
write.MustString(st.Writer, "\n") | ||
hasFields = true | ||
} | ||
indent.MustWrite(st.Writer, st.IndentString, st.IndentLevel) | ||
fd.Name() | ||
write.MustString(st.Writer, string(fd.Name())) | ||
write.MustString(st.Writer, ": ") | ||
must.Handle(vw.ValueWriter.WriteValue(st, reflect.ValueOf(m.Get(fd).Interface()))) | ||
write.MustString(st.Writer, ",\n") | ||
} | ||
st.IndentLevel-- | ||
if hasFields { | ||
indent.MustWrite(st.Writer, st.IndentString, st.IndentLevel) | ||
} | ||
write.MustString(st.Writer, "}") | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package protobuf | ||
|
||
import ( | ||
"slices" | ||
"testing" | ||
|
||
"github.com/pierrre/assert/assertauto" | ||
"github.com/pierrre/pretty" | ||
"google.golang.org/protobuf/types/known/wrapperspb" | ||
) | ||
|
||
func init() { | ||
pretty.DefaultCommonValueWriter.ConfigureTest() | ||
pretty.DefaultCommonValueWriter.ValueWriters = slices.Insert(pretty.DefaultCommonValueWriter.ValueWriters, 0, pretty.ValueWriter(&ValueWriter{ | ||
ValueWriter: pretty.DefaultCommonValueWriter, | ||
})) | ||
} | ||
|
||
func Test(t *testing.T) { | ||
v := wrapperspb.String("test") | ||
s := pretty.String(v) | ||
assertauto.Equal(t, s) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters