Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Ignore: Sketching a new visitor-pattern #32628

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions pkg/util/walker/demo/default_visitor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package demo

import (
"context"
"fmt"
"reflect"
"strings"
"testing"
)

func TestTree(t *testing.T) {
x := &Bar{
foo: Foo{
val: "Hello",
},
fooPtr: &Foo{
val: "World!",
},
}

depth := 0
var w strings.Builder
v := &StatementVisitorBase{
DefaultPre: func(ctx StatementContext, x Statement) (b bool, e error) {
for i := 0; i < depth; i++ {
if _, err := w.WriteString(" "); err != nil {
return false, nil
}
}
w.WriteString(fmt.Sprintf("Name: %s; Type: %s\n", x.Name(), reflect.TypeOf(x)))
depth++
return true, nil
},
DefaultPost: func(ctx StatementContext, x Statement) error {
depth--
return nil
},
}
if x2, dirty, err := WalkStatement(context.Background(), x, v); err != nil {
t.Fatal(err)
} else {
t.Logf("%v %+v %+v", dirty, x, x2)
}
t.Logf("Output:\n%s", w.String())
}
55 changes: 55 additions & 0 deletions pkg/util/walker/demo/demo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package demo

// REVIEWERS: Start reading this file first, then check out
// generated_api.go for the bulk of the user-visible API.

import (
"time"

"github.com/cockroachdb/cockroach/pkg/util/walker"
)

// The user starts by defining a common base interface for their
// visitable types. It is permissible for a package to define
// multiple base types, such as `Statement` and `Expr`. Any symbols
// derived from a visitable interface will have the interface's name
// included for disambiguation, so prefer shorter names.
type Statement interface {
// The generator looks for this magic type.
walker.Interface

// This method isn't special in any way, we just need some way to
// distinguish types defined in this package as being assignable
// to Statement. An `isStatement()` marker would also be just fine.
Name() string
}

// Foo is a "pure value" type which contains no pointers or slices.
type Foo struct {
val string
}

// Because *Foo implements statement, the user will get *Foo in
// the visitor API.
func (*Foo) Name() string { return "Foo" }

type Bar struct {
foo Foo
fooPtr *Foo

quux Quux
quuxPtr *Quux

fooSlice []Foo
fooPtrSlice []*Foo
}

func (*Bar) Name() string { return "Bar" }

type Quux struct {
now time.Time
}

// Quux implements the Statement interface with a value receiver,
// so the user will also see it by-value.
func (Quux) Name() string { return "Quux" }
111 changes: 111 additions & 0 deletions pkg/util/walker/demo/generated_api.go

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

Loading