Skip to content

Commit

Permalink
Only skip the calling filter in `FilterPrinter.Fallback().
Browse files Browse the repository at this point in the history
  • Loading branch information
jmalloc committed Aug 7, 2022
1 parent 0f9316f commit 69e7349
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 55 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ The format is based on [Keep a Changelog], and this project adheres to
[keep a changelog]: https://keepachangelog.com/en/1.0.0/
[semantic versioning]: https://semver.org/spec/v2.0.0.html

## [0.4.5] - 2022-08-07

### Fixed

- `StringerFilter` now takes precedence over all other filters
- `FilterPrinter.Fallback()` now only skips the filter it was called from

## [0.4.4] - 2022-08-07

### Added
Expand Down
9 changes: 5 additions & 4 deletions filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,17 @@ type FilterPrinter interface {

type filterPrinter struct {
*visitor
value Value
currentFilter uintptr
value Value
}

func (p filterPrinter) Fallback(w io.Writer, c Config) {
p.leave(p.value)

vis := &visitor{
config: c,
ignoreFilters: true,
recursionSet: p.recursionSet,
config: c,
skipFilter: p.currentFilter,
recursionSet: p.recursionSet,
}

vis.Write(w, p.value)
Expand Down
20 changes: 14 additions & 6 deletions filterprotobuf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ func TestPrinter_ProtobufFilter(t *testing.T) {
NestedA: "foo",
NestedB: []byte("<bytes>"),
},
Stringer: &fixtures.Stringer{
Value: "<stringer>",
},
}

// Trigger population of internal state to make sure it does not
Expand All @@ -29,14 +32,15 @@ func TestPrinter_ProtobufFilter(t *testing.T) {
actual := dapper.Format(m)
expected := strings.Join([]string{
`*github.com/dogmatiq/dapper/internal/fixtures.Message{`,
` Str: "hello"`,
` Enum: 1`,
` Nested: {`,
` Str: "hello"`,
` Enum: 1`,
` Nested: {`,
` NestedA: "foo"`,
` NestedB: {`,
` 00000000 3c 62 79 74 65 73 3e |<bytes>|`,
` }`,
` }`,
` Stringer: [<stringer>]`,
`}`,
}, "\n")

Expand All @@ -62,6 +66,9 @@ func TestPrinter_ProtobufFilter(t *testing.T) {
"it renders a protocol buffers message properly when nested within a regular struct", func(t *testing.T) {
m := &fixtures.Message{
Str: "hello",
Stringer: &fixtures.Stringer{
Value: "<stringer>",
},
}

outerStruct := struct {
Expand All @@ -74,9 +81,10 @@ func TestPrinter_ProtobufFilter(t *testing.T) {
`{`,
` foo: "hi"`,
` bar: *github.com/dogmatiq/dapper/internal/fixtures.Message{`,
` Str: "hello"`,
` Enum: 0`,
` Nested: nil`,
` Str: "hello"`,
` Enum: 0`,
` Nested: nil`,
` Stringer: [<stringer>]`,
` }`,
`}`,
}, "\n")
Expand Down
11 changes: 6 additions & 5 deletions filterstringer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@ func StringerFilter(
return nil
}

s := v.Value.Interface().(Stringer).DapperString()
if s == "" {
return nil
}

if v.IsAmbiguousType() {
must.WriteString(w, p.FormatTypeName(v))
must.WriteByte(w, ' ')
}

must.Fprintf(
w,
"[%s]",
v.Value.Interface().(Stringer).DapperString(),
)
must.Fprintf(w, "[%s]", s)

return nil
}
7 changes: 7 additions & 0 deletions internal/fixtures/protostub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package fixtures

// DapperString is used to ensure that dapper.Stringer takes precedence over
// other filters (such as the protobuf filter).
func (x *Stringer) DapperString() string {
return x.GetValue()
}
119 changes: 97 additions & 22 deletions internal/fixtures/protostub.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions internal/fixtures/protostub.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ message Message {
string str = 1;
Enum enum = 2;
Nested nested = 3;
Stringer stringer = 4;
}

message Nested {
string nested_a = 1;
bytes nested_b = 2;
}

message Stringer{
string value = 1;
}

enum Enum {
UNKNOWN = 0;
FOO = 1;
Expand Down
2 changes: 1 addition & 1 deletion map.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (vis *visitor) visitMap(w io.Writer, v Value) {
Map: v,
KeyType: v.DynamicType.Key(),
ValueType: v.DynamicType.Elem(),
Printer: &filterPrinter{vis, v},
Printer: &filterPrinter{vis, 0, v},
Indent: vis.config.Indent,
}

Expand Down
2 changes: 1 addition & 1 deletion printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ func (p *Printer) Format(v interface{}) string {
var DefaultPrinter = Printer{
Config: Config{
Filters: []Filter{
StringerFilter,
DurationFilter,
ProtobufFilter,
ReflectTypeFilter,
StringerFilter,
SyncFilter,
TimeFilter,
},
Expand Down
Loading

0 comments on commit 69e7349

Please sign in to comment.