https://github.com/berquerant/mkvisitor
Code generator to help implement the visitor pattern.
https://github.com/berquerant/mkvisitor
go
Last synced: about 1 year ago
JSON representation
Code generator to help implement the visitor pattern.
- Host: GitHub
- URL: https://github.com/berquerant/mkvisitor
- Owner: berquerant
- License: mit
- Created: 2021-10-05T14:52:59.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2025-04-20T07:35:27.000Z (about 1 year ago)
- Last Synced: 2025-04-20T08:38:00.691Z (about 1 year ago)
- Topics: go
- Language: Go
- Homepage:
- Size: 96.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# mkvisitor
Given
```go
package example
type (
Node struct{}
Leaf struct{}
)
```
run `mkvisitor -type "Node,Leaf"` then generate
```go
package example
import "fmt"
type Visitor interface {
VisitNode(*Node)
VisitLeaf(*Leaf)
}
func (s *Node) Accept(v Visitor) { v.VisitNode(s) }
func (s *Leaf) Accept(v Visitor) { v.VisitLeaf(s) }
type VisitorDefault struct{}
func (s *VisitorDefault) VisitNode(_ *Node) {}
func (s *VisitorDefault) VisitLeaf(_ *Leaf) {}
func VisitSwitch(visitor Visitor, v interface{}) {
switch v := v.(type) {
case *Node:
visitor.VisitNode(v)
case *Leaf:
visitor.VisitLeaf(v)
default:
panic(fmt.Sprintf("VisitSwitch cannot switch %#v", v))
}
}
```
in visitor_mkvisitor.go in the same directory.