Skip to content

Commit

Permalink
protobuf
Browse files Browse the repository at this point in the history
  • Loading branch information
pierrre committed Jan 30, 2025
1 parent dc0189a commit bcdc19f
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 0 deletions.
10 changes: 10 additions & 0 deletions ext/protobuf/_assertauto/Test.json
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}"
}
}
]
}
52 changes: 52 additions & 0 deletions ext/protobuf/protobuf.go
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
}
23 changes: 23 additions & 0 deletions ext/protobuf/protobuf_test.go
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)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/pierrre/assert v0.6.6
github.com/pierrre/errors v0.10.1
github.com/pierrre/go-libs v0.10.4
google.golang.org/protobuf v1.36.3
)

require github.com/pierrre/compare v1.4.13 // indirect
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/pierrre/assert v0.6.6 h1:NTuGmUjgYnLzLfIbsSw4ExRwdxRe1W33RznZRI4lQRI=
github.com/pierrre/assert v0.6.6/go.mod h1:sXsSQfPNeL4Nvd/+pa0OkD7WEIVXMHhOC1MJj0sx694=
github.com/pierrre/compare v1.4.13 h1:b6gi3OgN1emmD1Ly37m+B/Pbq6tac+w3lNGT5xu4I10=
Expand All @@ -6,3 +8,7 @@ github.com/pierrre/errors v0.10.1 h1:kSwceHaLh/yQ4MfJ7buh/HK5/pl+CZYQmb+ailJ9gmI
github.com/pierrre/errors v0.10.1/go.mod h1:IAGTUdSAq0X50i9MGEVXwhGAyRqXqC3/8qYSxaWFGks=
github.com/pierrre/go-libs v0.10.4 h1:ty5bNcT02BvyX825PRC35oHdAVMHcvfybK2LEsHJ+H8=
github.com/pierrre/go-libs v0.10.4/go.mod h1:Bd2rkKVvjMWABSeFwRHJfou1eKZPFTfL4N1YdICq5z4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.36.3 h1:82DV7MYdb8anAVi3qge1wSnMDrnKK7ebr+I0hHRN1BU=
google.golang.org/protobuf v1.36.3/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=

0 comments on commit bcdc19f

Please sign in to comment.