diff --git a/README.md b/README.md index ff06dac5..46045c64 100644 --- a/README.md +++ b/README.md @@ -480,6 +480,7 @@ go test ./... - [x] Reduce number of generated defauls, to most essential ones: visitor - for interface, match functions, and schema for serialisation/deserialisation - [x] `mkunion` offers `include-extension` for backward compatibility with previous versions - [x] `mkunion` allows generation outside of `//go:generate` directive and now you can call it as `mkunion -i=you/path/to/file.go -i=you/path/to/other/file.go` +- [x] `mkunion` allows to generate code for multiple unions in one file. Use `--no-compact` to change behaviour to previous one ### V1.20.x - [ ] Exhaustive pattern matching checks during generation diff --git a/cmd/mkunion/main.go b/cmd/mkunion/main.go index a5d4e6d5..e39f1be4 100644 --- a/cmd/mkunion/main.go +++ b/cmd/mkunion/main.go @@ -3,6 +3,7 @@ package main import ( "bytes" "context" + "fmt" log "github.com/sirupsen/logrus" "github.com/urfave/cli/v2" "github.com/widmogrod/mkunion" @@ -62,8 +63,7 @@ func main() { Value: false, }, &cli.BoolFlag{ - Name: "compact-to-one-file", - Aliases: []string{"compact"}, + Name: "no-compact", Required: false, }, }, @@ -113,7 +113,8 @@ func main() { options := []mkunion.GenerateOption{ mkunion.WithPackageName(inferred.PackageName), } - if c.Bool("compact-to-one-file") { + + if !c.Bool("no-compact") { options = append(options, mkunion.WithBufferedImports()) } @@ -166,14 +167,32 @@ func main() { delete(generators, name) } - if c.Bool("compact-to-one-file") { + if c.Bool("no-compact") { + + for name, g := range generators { + b, err := g.Generate() + if err != nil { + return err + } + + fileName := baseName + "_" + mkunion.Program + "_" + strings.ToLower(visitor.Name) + "_" + name + ".go" + log.Infof("writing %s", fileName) + + err = os.WriteFile(path.Join(cwd, fileName), b, 0644) + if err != nil { + return err + } + } + } else { body := bytes.Buffer{} - for _, g := range generators { + for name, g := range generators { b, err := g.Generate() if err != nil { return err } + body.WriteString(fmt.Sprintf("//mkunion-extension:%s\n", name)) body.Write(b) + body.WriteString("\n") } header := bytes.Buffer{} @@ -190,21 +209,6 @@ func main() { if err != nil { return err } - } else { - for name, g := range generators { - b, err := g.Generate() - if err != nil { - return err - } - - fileName := baseName + "_" + mkunion.Program + "_" + strings.ToLower(visitor.Name) + "_" + name + ".go" - log.Infof("writing %s", fileName) - - err = os.WriteFile(path.Join(cwd, fileName), b, 0644) - if err != nil { - return err - } - } } } } diff --git a/schema_generator.go.tmpl b/schema_generator.go.tmpl index 74a98bd2..047b8494 100644 --- a/schema_generator.go.tmpl +++ b/schema_generator.go.tmpl @@ -1,8 +1,7 @@ {{ RenderHeader -}} {{ RenderImport "github.com/widmogrod/mkunion/x/schema" -}} -{{- $name := .Name }} - +{{- $name := .Name -}} func init() { schema.RegisterUnionTypes({{ $name }}SchemaDef()) } diff --git a/schema_generator_test.go b/schema_generator_test.go index 886ccf15..1a4d496b 100644 --- a/schema_generator_test.go +++ b/schema_generator_test.go @@ -15,8 +15,6 @@ package visitor import "github.com/widmogrod/mkunion/x/schema" - - func init() { schema.RegisterUnionTypes(VehicleSchemaDef()) } diff --git a/x/schema/location_mkunion_location_default_reducer.go b/x/schema/location_mkunion_location_default_reducer.go deleted file mode 100644 index d1e65deb..00000000 --- a/x/schema/location_mkunion_location_default_reducer.go +++ /dev/null @@ -1,44 +0,0 @@ -// Code generated by mkunion. DO NOT EDIT. -package schema - -var _ LocationReducer[any] = (*LocationDefaultReduction[any])(nil) - -type ( - LocationDefaultReduction[A any] struct { - PanicOnFallback bool - DefaultStopReduction bool - OnLocationField func(x *LocationField, agg A) (result A, stop bool) - OnLocationIndex func(x *LocationIndex, agg A) (result A, stop bool) - OnLocationAnything func(x *LocationAnything, agg A) (result A, stop bool) - } -) - -func (t *LocationDefaultReduction[A]) ReduceLocationField(x *LocationField, agg A) (result A, stop bool) { - if t.OnLocationField != nil { - return t.OnLocationField(x, agg) - } - if t.PanicOnFallback { - panic("no fallback allowed on undefined ReduceBranch") - } - return agg, t.DefaultStopReduction -} - -func (t *LocationDefaultReduction[A]) ReduceLocationIndex(x *LocationIndex, agg A) (result A, stop bool) { - if t.OnLocationIndex != nil { - return t.OnLocationIndex(x, agg) - } - if t.PanicOnFallback { - panic("no fallback allowed on undefined ReduceBranch") - } - return agg, t.DefaultStopReduction -} - -func (t *LocationDefaultReduction[A]) ReduceLocationAnything(x *LocationAnything, agg A) (result A, stop bool) { - if t.OnLocationAnything != nil { - return t.OnLocationAnything(x, agg) - } - if t.PanicOnFallback { - panic("no fallback allowed on undefined ReduceBranch") - } - return agg, t.DefaultStopReduction -} diff --git a/x/schema/location_mkunion_location_default_visitor.go b/x/schema/location_mkunion_location_default_visitor.go deleted file mode 100644 index 67d3ff60..00000000 --- a/x/schema/location_mkunion_location_default_visitor.go +++ /dev/null @@ -1,28 +0,0 @@ -// Code generated by mkunion. DO NOT EDIT. -package schema - -type LocationDefaultVisitor[A any] struct { - Default A - OnLocationField func(x *LocationField) A - OnLocationIndex func(x *LocationIndex) A - OnLocationAnything func(x *LocationAnything) A -} - -func (t *LocationDefaultVisitor[A]) VisitLocationField(v *LocationField) any { - if t.OnLocationField != nil { - return t.OnLocationField(v) - } - return t.Default -} -func (t *LocationDefaultVisitor[A]) VisitLocationIndex(v *LocationIndex) any { - if t.OnLocationIndex != nil { - return t.OnLocationIndex(v) - } - return t.Default -} -func (t *LocationDefaultVisitor[A]) VisitLocationAnything(v *LocationAnything) any { - if t.OnLocationAnything != nil { - return t.OnLocationAnything(v) - } - return t.Default -} diff --git a/x/schema/location_mkunion_location_reducer_bfs.go b/x/schema/location_mkunion_location_reducer_bfs.go deleted file mode 100644 index 1c8ed8ba..00000000 --- a/x/schema/location_mkunion_location_reducer_bfs.go +++ /dev/null @@ -1,88 +0,0 @@ -// Code generated by mkunion. DO NOT EDIT. -package schema - -var _ LocationVisitor = (*LocationBreadthFirstVisitor[any])(nil) - -type LocationBreadthFirstVisitor[A any] struct { - stop bool - result A - reduce LocationReducer[A] - - queue []Location - visited map[Location]bool - shouldExecute map[Location]bool -} - -func (d *LocationBreadthFirstVisitor[A]) VisitLocationField(v *LocationField) any { - d.queue = append(d.queue, v) - - if d.shouldExecute[v] { - d.shouldExecute[v] = false - d.result, d.stop = d.reduce.ReduceLocationField(v, d.result) - } else { - d.execute() - } - return nil -} - -func (d *LocationBreadthFirstVisitor[A]) VisitLocationIndex(v *LocationIndex) any { - d.queue = append(d.queue, v) - - if d.shouldExecute[v] { - d.shouldExecute[v] = false - d.result, d.stop = d.reduce.ReduceLocationIndex(v, d.result) - } else { - d.execute() - } - return nil -} - -func (d *LocationBreadthFirstVisitor[A]) VisitLocationAnything(v *LocationAnything) any { - d.queue = append(d.queue, v) - - if d.shouldExecute[v] { - d.shouldExecute[v] = false - d.result, d.stop = d.reduce.ReduceLocationAnything(v, d.result) - } else { - d.execute() - } - return nil -} - -func (d *LocationBreadthFirstVisitor[A]) execute() { - for len(d.queue) > 0 { - if d.stop { - return - } - - i := d.pop() - if d.visited[i] { - continue - } - d.visited[i] = true - d.shouldExecute[i] = true - i.AcceptLocation(d) - } - - return -} - -func (d *LocationBreadthFirstVisitor[A]) pop() Location { - i := d.queue[0] - d.queue = d.queue[1:] - return i -} - -func ReduceLocationBreadthFirst[A any](r LocationReducer[A], v Location, init A) A { - reducer := &LocationBreadthFirstVisitor[A]{ - result: init, - reduce: r, - queue: []Location{v}, - visited: make(map[Location]bool), - shouldExecute: make(map[Location]bool), - } - - _ = v.AcceptLocation(reducer) - - return reducer.result -} diff --git a/x/schema/location_mkunion_location_reducer_dfs.go b/x/schema/location_mkunion_location_reducer_dfs.go deleted file mode 100644 index 969f734d..00000000 --- a/x/schema/location_mkunion_location_reducer_dfs.go +++ /dev/null @@ -1,56 +0,0 @@ -// Code generated by mkunion. DO NOT EDIT. -package schema - -type ( - LocationReducer[A any] interface { - ReduceLocationField(x *LocationField, agg A) (result A, stop bool) - ReduceLocationIndex(x *LocationIndex, agg A) (result A, stop bool) - ReduceLocationAnything(x *LocationAnything, agg A) (result A, stop bool) - } -) - -type LocationDepthFirstVisitor[A any] struct { - stop bool - result A - reduce LocationReducer[A] -} - -var _ LocationVisitor = (*LocationDepthFirstVisitor[any])(nil) - -func (d *LocationDepthFirstVisitor[A]) VisitLocationField(v *LocationField) any { - d.result, d.stop = d.reduce.ReduceLocationField(v, d.result) - if d.stop { - return nil - } - - return nil -} - -func (d *LocationDepthFirstVisitor[A]) VisitLocationIndex(v *LocationIndex) any { - d.result, d.stop = d.reduce.ReduceLocationIndex(v, d.result) - if d.stop { - return nil - } - - return nil -} - -func (d *LocationDepthFirstVisitor[A]) VisitLocationAnything(v *LocationAnything) any { - d.result, d.stop = d.reduce.ReduceLocationAnything(v, d.result) - if d.stop { - return nil - } - - return nil -} - -func ReduceLocationDepthFirst[A any](r LocationReducer[A], v Location, init A) A { - reducer := &LocationDepthFirstVisitor[A]{ - result: init, - reduce: r, - } - - _ = v.AcceptLocation(reducer) - - return reducer.result -} diff --git a/x/schema/location_mkunion_location_visitor.go b/x/schema/location_mkunion_location_visitor.go deleted file mode 100644 index 75e85f39..00000000 --- a/x/schema/location_mkunion_location_visitor.go +++ /dev/null @@ -1,73 +0,0 @@ -// Code generated by mkunion. DO NOT EDIT. -package schema - -import ( - "github.com/widmogrod/mkunion/f" -) - -type LocationVisitor interface { - VisitLocationField(v *LocationField) any - VisitLocationIndex(v *LocationIndex) any - VisitLocationAnything(v *LocationAnything) any -} - -type Location interface { - AcceptLocation(g LocationVisitor) any -} - -func (r *LocationField) AcceptLocation(v LocationVisitor) any { return v.VisitLocationField(r) } -func (r *LocationIndex) AcceptLocation(v LocationVisitor) any { return v.VisitLocationIndex(r) } -func (r *LocationAnything) AcceptLocation(v LocationVisitor) any { return v.VisitLocationAnything(r) } - -var ( - _ Location = (*LocationField)(nil) - _ Location = (*LocationIndex)(nil) - _ Location = (*LocationAnything)(nil) -) - -func MatchLocation[TOut any]( - x Location, - f1 func(x *LocationField) TOut, - f2 func(x *LocationIndex) TOut, - f3 func(x *LocationAnything) TOut, - df func(x Location) TOut, -) TOut { - return f.Match3(x, f1, f2, f3, df) -} - -func MatchLocationR2[TOut1, TOut2 any]( - x Location, - f1 func(x *LocationField) (TOut1, TOut2), - f2 func(x *LocationIndex) (TOut1, TOut2), - f3 func(x *LocationAnything) (TOut1, TOut2), - df func(x Location) (TOut1, TOut2), -) (TOut1, TOut2) { - return f.Match3R2(x, f1, f2, f3, df) -} - -func MustMatchLocation[TOut any]( - x Location, - f1 func(x *LocationField) TOut, - f2 func(x *LocationIndex) TOut, - f3 func(x *LocationAnything) TOut, -) TOut { - return f.MustMatch3(x, f1, f2, f3) -} - -func MustMatchLocationR0( - x Location, - f1 func(x *LocationField), - f2 func(x *LocationIndex), - f3 func(x *LocationAnything), -) { - f.MustMatch3R0(x, f1, f2, f3) -} - -func MustMatchLocationR2[TOut1, TOut2 any]( - x Location, - f1 func(x *LocationField) (TOut1, TOut2), - f2 func(x *LocationIndex) (TOut1, TOut2), - f3 func(x *LocationAnything) (TOut1, TOut2), -) (TOut1, TOut2) { - return f.MustMatch3R2(x, f1, f2, f3) -} diff --git a/x/schema/model_mkunion_schema_default_reducer.go b/x/schema/model_mkunion_schema_default_reducer.go deleted file mode 100644 index 9171bfdb..00000000 --- a/x/schema/model_mkunion_schema_default_reducer.go +++ /dev/null @@ -1,88 +0,0 @@ -// Code generated by mkunion. DO NOT EDIT. -package schema - -var _ SchemaReducer[any] = (*SchemaDefaultReduction[any])(nil) - -type ( - SchemaDefaultReduction[A any] struct { - PanicOnFallback bool - DefaultStopReduction bool - OnNone func(x *None, agg A) (result A, stop bool) - OnBool func(x *Bool, agg A) (result A, stop bool) - OnNumber func(x *Number, agg A) (result A, stop bool) - OnString func(x *String, agg A) (result A, stop bool) - OnBinary func(x *Binary, agg A) (result A, stop bool) - OnList func(x *List, agg A) (result A, stop bool) - OnMap func(x *Map, agg A) (result A, stop bool) - } -) - -func (t *SchemaDefaultReduction[A]) ReduceNone(x *None, agg A) (result A, stop bool) { - if t.OnNone != nil { - return t.OnNone(x, agg) - } - if t.PanicOnFallback { - panic("no fallback allowed on undefined ReduceBranch") - } - return agg, t.DefaultStopReduction -} - -func (t *SchemaDefaultReduction[A]) ReduceBool(x *Bool, agg A) (result A, stop bool) { - if t.OnBool != nil { - return t.OnBool(x, agg) - } - if t.PanicOnFallback { - panic("no fallback allowed on undefined ReduceBranch") - } - return agg, t.DefaultStopReduction -} - -func (t *SchemaDefaultReduction[A]) ReduceNumber(x *Number, agg A) (result A, stop bool) { - if t.OnNumber != nil { - return t.OnNumber(x, agg) - } - if t.PanicOnFallback { - panic("no fallback allowed on undefined ReduceBranch") - } - return agg, t.DefaultStopReduction -} - -func (t *SchemaDefaultReduction[A]) ReduceString(x *String, agg A) (result A, stop bool) { - if t.OnString != nil { - return t.OnString(x, agg) - } - if t.PanicOnFallback { - panic("no fallback allowed on undefined ReduceBranch") - } - return agg, t.DefaultStopReduction -} - -func (t *SchemaDefaultReduction[A]) ReduceBinary(x *Binary, agg A) (result A, stop bool) { - if t.OnBinary != nil { - return t.OnBinary(x, agg) - } - if t.PanicOnFallback { - panic("no fallback allowed on undefined ReduceBranch") - } - return agg, t.DefaultStopReduction -} - -func (t *SchemaDefaultReduction[A]) ReduceList(x *List, agg A) (result A, stop bool) { - if t.OnList != nil { - return t.OnList(x, agg) - } - if t.PanicOnFallback { - panic("no fallback allowed on undefined ReduceBranch") - } - return agg, t.DefaultStopReduction -} - -func (t *SchemaDefaultReduction[A]) ReduceMap(x *Map, agg A) (result A, stop bool) { - if t.OnMap != nil { - return t.OnMap(x, agg) - } - if t.PanicOnFallback { - panic("no fallback allowed on undefined ReduceBranch") - } - return agg, t.DefaultStopReduction -} diff --git a/x/schema/model_mkunion_schema_default_visitor.go b/x/schema/model_mkunion_schema_default_visitor.go deleted file mode 100644 index d2d0bc3f..00000000 --- a/x/schema/model_mkunion_schema_default_visitor.go +++ /dev/null @@ -1,56 +0,0 @@ -// Code generated by mkunion. DO NOT EDIT. -package schema - -type SchemaDefaultVisitor[A any] struct { - Default A - OnNone func(x *None) A - OnBool func(x *Bool) A - OnNumber func(x *Number) A - OnString func(x *String) A - OnBinary func(x *Binary) A - OnList func(x *List) A - OnMap func(x *Map) A -} - -func (t *SchemaDefaultVisitor[A]) VisitNone(v *None) any { - if t.OnNone != nil { - return t.OnNone(v) - } - return t.Default -} -func (t *SchemaDefaultVisitor[A]) VisitBool(v *Bool) any { - if t.OnBool != nil { - return t.OnBool(v) - } - return t.Default -} -func (t *SchemaDefaultVisitor[A]) VisitNumber(v *Number) any { - if t.OnNumber != nil { - return t.OnNumber(v) - } - return t.Default -} -func (t *SchemaDefaultVisitor[A]) VisitString(v *String) any { - if t.OnString != nil { - return t.OnString(v) - } - return t.Default -} -func (t *SchemaDefaultVisitor[A]) VisitBinary(v *Binary) any { - if t.OnBinary != nil { - return t.OnBinary(v) - } - return t.Default -} -func (t *SchemaDefaultVisitor[A]) VisitList(v *List) any { - if t.OnList != nil { - return t.OnList(v) - } - return t.Default -} -func (t *SchemaDefaultVisitor[A]) VisitMap(v *Map) any { - if t.OnMap != nil { - return t.OnMap(v) - } - return t.Default -} diff --git a/x/schema/model_mkunion_schema_reducer_bfs.go b/x/schema/model_mkunion_schema_reducer_bfs.go deleted file mode 100644 index 588f0a63..00000000 --- a/x/schema/model_mkunion_schema_reducer_bfs.go +++ /dev/null @@ -1,139 +0,0 @@ -// Code generated by mkunion. DO NOT EDIT. -package schema - -var _ SchemaVisitor = (*SchemaBreadthFirstVisitor[any])(nil) - -type SchemaBreadthFirstVisitor[A any] struct { - stop bool - result A - reduce SchemaReducer[A] - - queue []Schema - visited map[Schema]bool - shouldExecute map[Schema]bool -} - -func (d *SchemaBreadthFirstVisitor[A]) VisitNone(v *None) any { - d.queue = append(d.queue, v) - - if d.shouldExecute[v] { - d.shouldExecute[v] = false - d.result, d.stop = d.reduce.ReduceNone(v, d.result) - } else { - d.execute() - } - return nil -} - -func (d *SchemaBreadthFirstVisitor[A]) VisitBool(v *Bool) any { - d.queue = append(d.queue, v) - - if d.shouldExecute[v] { - d.shouldExecute[v] = false - d.result, d.stop = d.reduce.ReduceBool(v, d.result) - } else { - d.execute() - } - return nil -} - -func (d *SchemaBreadthFirstVisitor[A]) VisitNumber(v *Number) any { - d.queue = append(d.queue, v) - - if d.shouldExecute[v] { - d.shouldExecute[v] = false - d.result, d.stop = d.reduce.ReduceNumber(v, d.result) - } else { - d.execute() - } - return nil -} - -func (d *SchemaBreadthFirstVisitor[A]) VisitString(v *String) any { - d.queue = append(d.queue, v) - - if d.shouldExecute[v] { - d.shouldExecute[v] = false - d.result, d.stop = d.reduce.ReduceString(v, d.result) - } else { - d.execute() - } - return nil -} - -func (d *SchemaBreadthFirstVisitor[A]) VisitBinary(v *Binary) any { - d.queue = append(d.queue, v) - - if d.shouldExecute[v] { - d.shouldExecute[v] = false - d.result, d.stop = d.reduce.ReduceBinary(v, d.result) - } else { - d.execute() - } - return nil -} - -func (d *SchemaBreadthFirstVisitor[A]) VisitList(v *List) any { - d.queue = append(d.queue, v) - for idx := range v.Items { - d.queue = append(d.queue, v.Items[idx]) - } - - if d.shouldExecute[v] { - d.shouldExecute[v] = false - d.result, d.stop = d.reduce.ReduceList(v, d.result) - } else { - d.execute() - } - return nil -} - -func (d *SchemaBreadthFirstVisitor[A]) VisitMap(v *Map) any { - d.queue = append(d.queue, v) - - if d.shouldExecute[v] { - d.shouldExecute[v] = false - d.result, d.stop = d.reduce.ReduceMap(v, d.result) - } else { - d.execute() - } - return nil -} - -func (d *SchemaBreadthFirstVisitor[A]) execute() { - for len(d.queue) > 0 { - if d.stop { - return - } - - i := d.pop() - if d.visited[i] { - continue - } - d.visited[i] = true - d.shouldExecute[i] = true - i.AcceptSchema(d) - } - - return -} - -func (d *SchemaBreadthFirstVisitor[A]) pop() Schema { - i := d.queue[0] - d.queue = d.queue[1:] - return i -} - -func ReduceSchemaBreadthFirst[A any](r SchemaReducer[A], v Schema, init A) A { - reducer := &SchemaBreadthFirstVisitor[A]{ - result: init, - reduce: r, - queue: []Schema{v}, - visited: make(map[Schema]bool), - shouldExecute: make(map[Schema]bool), - } - - _ = v.AcceptSchema(reducer) - - return reducer.result -} diff --git a/x/schema/model_mkunion_schema_reducer_dfs.go b/x/schema/model_mkunion_schema_reducer_dfs.go deleted file mode 100644 index 82b1de99..00000000 --- a/x/schema/model_mkunion_schema_reducer_dfs.go +++ /dev/null @@ -1,101 +0,0 @@ -// Code generated by mkunion. DO NOT EDIT. -package schema - -type ( - SchemaReducer[A any] interface { - ReduceNone(x *None, agg A) (result A, stop bool) - ReduceBool(x *Bool, agg A) (result A, stop bool) - ReduceNumber(x *Number, agg A) (result A, stop bool) - ReduceString(x *String, agg A) (result A, stop bool) - ReduceBinary(x *Binary, agg A) (result A, stop bool) - ReduceList(x *List, agg A) (result A, stop bool) - ReduceMap(x *Map, agg A) (result A, stop bool) - } -) - -type SchemaDepthFirstVisitor[A any] struct { - stop bool - result A - reduce SchemaReducer[A] -} - -var _ SchemaVisitor = (*SchemaDepthFirstVisitor[any])(nil) - -func (d *SchemaDepthFirstVisitor[A]) VisitNone(v *None) any { - d.result, d.stop = d.reduce.ReduceNone(v, d.result) - if d.stop { - return nil - } - - return nil -} - -func (d *SchemaDepthFirstVisitor[A]) VisitBool(v *Bool) any { - d.result, d.stop = d.reduce.ReduceBool(v, d.result) - if d.stop { - return nil - } - - return nil -} - -func (d *SchemaDepthFirstVisitor[A]) VisitNumber(v *Number) any { - d.result, d.stop = d.reduce.ReduceNumber(v, d.result) - if d.stop { - return nil - } - - return nil -} - -func (d *SchemaDepthFirstVisitor[A]) VisitString(v *String) any { - d.result, d.stop = d.reduce.ReduceString(v, d.result) - if d.stop { - return nil - } - - return nil -} - -func (d *SchemaDepthFirstVisitor[A]) VisitBinary(v *Binary) any { - d.result, d.stop = d.reduce.ReduceBinary(v, d.result) - if d.stop { - return nil - } - - return nil -} - -func (d *SchemaDepthFirstVisitor[A]) VisitList(v *List) any { - d.result, d.stop = d.reduce.ReduceList(v, d.result) - if d.stop { - return nil - } - for idx := range v.Items { - if _ = v.Items[idx].AcceptSchema(d); d.stop { - return nil - } - } - - return nil -} - -func (d *SchemaDepthFirstVisitor[A]) VisitMap(v *Map) any { - d.result, d.stop = d.reduce.ReduceMap(v, d.result) - if d.stop { - return nil - } - - return nil -} - -func ReduceSchemaDepthFirst[A any](r SchemaReducer[A], v Schema, init A) A { - reducer := &SchemaDepthFirstVisitor[A]{ - result: init, - reduce: r, - } - - _ = v.AcceptSchema(reducer) - - return reducer.result -} diff --git a/x/schema/model_mkunion_schema_visitor.go b/x/schema/model_mkunion_schema_visitor.go deleted file mode 100644 index 111d2981..00000000 --- a/x/schema/model_mkunion_schema_visitor.go +++ /dev/null @@ -1,105 +0,0 @@ -// Code generated by mkunion. DO NOT EDIT. -package schema - -import ( - "github.com/widmogrod/mkunion/f" -) - -type SchemaVisitor interface { - VisitNone(v *None) any - VisitBool(v *Bool) any - VisitNumber(v *Number) any - VisitString(v *String) any - VisitBinary(v *Binary) any - VisitList(v *List) any - VisitMap(v *Map) any -} - -type Schema interface { - AcceptSchema(g SchemaVisitor) any -} - -func (r *None) AcceptSchema(v SchemaVisitor) any { return v.VisitNone(r) } -func (r *Bool) AcceptSchema(v SchemaVisitor) any { return v.VisitBool(r) } -func (r *Number) AcceptSchema(v SchemaVisitor) any { return v.VisitNumber(r) } -func (r *String) AcceptSchema(v SchemaVisitor) any { return v.VisitString(r) } -func (r *Binary) AcceptSchema(v SchemaVisitor) any { return v.VisitBinary(r) } -func (r *List) AcceptSchema(v SchemaVisitor) any { return v.VisitList(r) } -func (r *Map) AcceptSchema(v SchemaVisitor) any { return v.VisitMap(r) } - -var ( - _ Schema = (*None)(nil) - _ Schema = (*Bool)(nil) - _ Schema = (*Number)(nil) - _ Schema = (*String)(nil) - _ Schema = (*Binary)(nil) - _ Schema = (*List)(nil) - _ Schema = (*Map)(nil) -) - -func MatchSchema[TOut any]( - x Schema, - f1 func(x *None) TOut, - f2 func(x *Bool) TOut, - f3 func(x *Number) TOut, - f4 func(x *String) TOut, - f5 func(x *Binary) TOut, - f6 func(x *List) TOut, - f7 func(x *Map) TOut, - df func(x Schema) TOut, -) TOut { - return f.Match7(x, f1, f2, f3, f4, f5, f6, f7, df) -} - -func MatchSchemaR2[TOut1, TOut2 any]( - x Schema, - f1 func(x *None) (TOut1, TOut2), - f2 func(x *Bool) (TOut1, TOut2), - f3 func(x *Number) (TOut1, TOut2), - f4 func(x *String) (TOut1, TOut2), - f5 func(x *Binary) (TOut1, TOut2), - f6 func(x *List) (TOut1, TOut2), - f7 func(x *Map) (TOut1, TOut2), - df func(x Schema) (TOut1, TOut2), -) (TOut1, TOut2) { - return f.Match7R2(x, f1, f2, f3, f4, f5, f6, f7, df) -} - -func MustMatchSchema[TOut any]( - x Schema, - f1 func(x *None) TOut, - f2 func(x *Bool) TOut, - f3 func(x *Number) TOut, - f4 func(x *String) TOut, - f5 func(x *Binary) TOut, - f6 func(x *List) TOut, - f7 func(x *Map) TOut, -) TOut { - return f.MustMatch7(x, f1, f2, f3, f4, f5, f6, f7) -} - -func MustMatchSchemaR0( - x Schema, - f1 func(x *None), - f2 func(x *Bool), - f3 func(x *Number), - f4 func(x *String), - f5 func(x *Binary), - f6 func(x *List), - f7 func(x *Map), -) { - f.MustMatch7R0(x, f1, f2, f3, f4, f5, f6, f7) -} - -func MustMatchSchemaR2[TOut1, TOut2 any]( - x Schema, - f1 func(x *None) (TOut1, TOut2), - f2 func(x *Bool) (TOut1, TOut2), - f3 func(x *Number) (TOut1, TOut2), - f4 func(x *String) (TOut1, TOut2), - f5 func(x *Binary) (TOut1, TOut2), - f6 func(x *List) (TOut1, TOut2), - f7 func(x *Map) (TOut1, TOut2), -) (TOut1, TOut2) { - return f.MustMatch7R2(x, f1, f2, f3, f4, f5, f6, f7) -} diff --git a/x/shape/shape_mkunion_guard_default_reducer.go b/x/shape/shape_mkunion_guard_default_reducer.go deleted file mode 100644 index 062d68e4..00000000 --- a/x/shape/shape_mkunion_guard_default_reducer.go +++ /dev/null @@ -1,55 +0,0 @@ -// Code generated by mkunion. DO NOT EDIT. -package shape - -var _ GuardReducer[any] = (*GuardDefaultReduction[any])(nil) - -type ( - GuardDefaultReduction[A any] struct { - PanicOnFallback bool - DefaultStopReduction bool - OnRegexp func(x *Regexp, agg A) (result A, stop bool) - OnBetween func(x *Between, agg A) (result A, stop bool) - OnAndGuard func(x *AndGuard, agg A) (result A, stop bool) - OnOrGuard func(x *OrGuard, agg A) (result A, stop bool) - } -) - -func (t *GuardDefaultReduction[A]) ReduceRegexp(x *Regexp, agg A) (result A, stop bool) { - if t.OnRegexp != nil { - return t.OnRegexp(x, agg) - } - if t.PanicOnFallback { - panic("no fallback allowed on undefined ReduceBranch") - } - return agg, t.DefaultStopReduction -} - -func (t *GuardDefaultReduction[A]) ReduceBetween(x *Between, agg A) (result A, stop bool) { - if t.OnBetween != nil { - return t.OnBetween(x, agg) - } - if t.PanicOnFallback { - panic("no fallback allowed on undefined ReduceBranch") - } - return agg, t.DefaultStopReduction -} - -func (t *GuardDefaultReduction[A]) ReduceAndGuard(x *AndGuard, agg A) (result A, stop bool) { - if t.OnAndGuard != nil { - return t.OnAndGuard(x, agg) - } - if t.PanicOnFallback { - panic("no fallback allowed on undefined ReduceBranch") - } - return agg, t.DefaultStopReduction -} - -func (t *GuardDefaultReduction[A]) ReduceOrGuard(x *OrGuard, agg A) (result A, stop bool) { - if t.OnOrGuard != nil { - return t.OnOrGuard(x, agg) - } - if t.PanicOnFallback { - panic("no fallback allowed on undefined ReduceBranch") - } - return agg, t.DefaultStopReduction -} diff --git a/x/shape/shape_mkunion_guard_default_visitor.go b/x/shape/shape_mkunion_guard_default_visitor.go deleted file mode 100644 index f96df6b8..00000000 --- a/x/shape/shape_mkunion_guard_default_visitor.go +++ /dev/null @@ -1,35 +0,0 @@ -// Code generated by mkunion. DO NOT EDIT. -package shape - -type GuardDefaultVisitor[A any] struct { - Default A - OnRegexp func(x *Regexp) A - OnBetween func(x *Between) A - OnAndGuard func(x *AndGuard) A - OnOrGuard func(x *OrGuard) A -} - -func (t *GuardDefaultVisitor[A]) VisitRegexp(v *Regexp) any { - if t.OnRegexp != nil { - return t.OnRegexp(v) - } - return t.Default -} -func (t *GuardDefaultVisitor[A]) VisitBetween(v *Between) any { - if t.OnBetween != nil { - return t.OnBetween(v) - } - return t.Default -} -func (t *GuardDefaultVisitor[A]) VisitAndGuard(v *AndGuard) any { - if t.OnAndGuard != nil { - return t.OnAndGuard(v) - } - return t.Default -} -func (t *GuardDefaultVisitor[A]) VisitOrGuard(v *OrGuard) any { - if t.OnOrGuard != nil { - return t.OnOrGuard(v) - } - return t.Default -} diff --git a/x/shape/shape_mkunion_guard_reducer_bfs.go b/x/shape/shape_mkunion_guard_reducer_bfs.go deleted file mode 100644 index a2b8bdb0..00000000 --- a/x/shape/shape_mkunion_guard_reducer_bfs.go +++ /dev/null @@ -1,106 +0,0 @@ -// Code generated by mkunion. DO NOT EDIT. -package shape - -var _ GuardVisitor = (*GuardBreadthFirstVisitor[any])(nil) - -type GuardBreadthFirstVisitor[A any] struct { - stop bool - result A - reduce GuardReducer[A] - - queue []Guard - visited map[Guard]bool - shouldExecute map[Guard]bool -} - -func (d *GuardBreadthFirstVisitor[A]) VisitRegexp(v *Regexp) any { - d.queue = append(d.queue, v) - - if d.shouldExecute[v] { - d.shouldExecute[v] = false - d.result, d.stop = d.reduce.ReduceRegexp(v, d.result) - } else { - d.execute() - } - return nil -} - -func (d *GuardBreadthFirstVisitor[A]) VisitBetween(v *Between) any { - d.queue = append(d.queue, v) - - if d.shouldExecute[v] { - d.shouldExecute[v] = false - d.result, d.stop = d.reduce.ReduceBetween(v, d.result) - } else { - d.execute() - } - return nil -} - -func (d *GuardBreadthFirstVisitor[A]) VisitAndGuard(v *AndGuard) any { - d.queue = append(d.queue, v) - for idx := range v.L { - d.queue = append(d.queue, v.L[idx]) - } - - if d.shouldExecute[v] { - d.shouldExecute[v] = false - d.result, d.stop = d.reduce.ReduceAndGuard(v, d.result) - } else { - d.execute() - } - return nil -} - -func (d *GuardBreadthFirstVisitor[A]) VisitOrGuard(v *OrGuard) any { - d.queue = append(d.queue, v) - for idx := range v.L { - d.queue = append(d.queue, v.L[idx]) - } - - if d.shouldExecute[v] { - d.shouldExecute[v] = false - d.result, d.stop = d.reduce.ReduceOrGuard(v, d.result) - } else { - d.execute() - } - return nil -} - -func (d *GuardBreadthFirstVisitor[A]) execute() { - for len(d.queue) > 0 { - if d.stop { - return - } - - i := d.pop() - if d.visited[i] { - continue - } - d.visited[i] = true - d.shouldExecute[i] = true - i.AcceptGuard(d) - } - - return -} - -func (d *GuardBreadthFirstVisitor[A]) pop() Guard { - i := d.queue[0] - d.queue = d.queue[1:] - return i -} - -func ReduceGuardBreadthFirst[A any](r GuardReducer[A], v Guard, init A) A { - reducer := &GuardBreadthFirstVisitor[A]{ - result: init, - reduce: r, - queue: []Guard{v}, - visited: make(map[Guard]bool), - shouldExecute: make(map[Guard]bool), - } - - _ = v.AcceptGuard(reducer) - - return reducer.result -} diff --git a/x/shape/shape_mkunion_guard_reducer_dfs.go b/x/shape/shape_mkunion_guard_reducer_dfs.go deleted file mode 100644 index 40371f74..00000000 --- a/x/shape/shape_mkunion_guard_reducer_dfs.go +++ /dev/null @@ -1,76 +0,0 @@ -// Code generated by mkunion. DO NOT EDIT. -package shape - -type ( - GuardReducer[A any] interface { - ReduceRegexp(x *Regexp, agg A) (result A, stop bool) - ReduceBetween(x *Between, agg A) (result A, stop bool) - ReduceAndGuard(x *AndGuard, agg A) (result A, stop bool) - ReduceOrGuard(x *OrGuard, agg A) (result A, stop bool) - } -) - -type GuardDepthFirstVisitor[A any] struct { - stop bool - result A - reduce GuardReducer[A] -} - -var _ GuardVisitor = (*GuardDepthFirstVisitor[any])(nil) - -func (d *GuardDepthFirstVisitor[A]) VisitRegexp(v *Regexp) any { - d.result, d.stop = d.reduce.ReduceRegexp(v, d.result) - if d.stop { - return nil - } - - return nil -} - -func (d *GuardDepthFirstVisitor[A]) VisitBetween(v *Between) any { - d.result, d.stop = d.reduce.ReduceBetween(v, d.result) - if d.stop { - return nil - } - - return nil -} - -func (d *GuardDepthFirstVisitor[A]) VisitAndGuard(v *AndGuard) any { - d.result, d.stop = d.reduce.ReduceAndGuard(v, d.result) - if d.stop { - return nil - } - for idx := range v.L { - if _ = v.L[idx].AcceptGuard(d); d.stop { - return nil - } - } - - return nil -} - -func (d *GuardDepthFirstVisitor[A]) VisitOrGuard(v *OrGuard) any { - d.result, d.stop = d.reduce.ReduceOrGuard(v, d.result) - if d.stop { - return nil - } - for idx := range v.L { - if _ = v.L[idx].AcceptGuard(d); d.stop { - return nil - } - } - - return nil -} - -func ReduceGuardDepthFirst[A any](r GuardReducer[A], v Guard, init A) A { - reducer := &GuardDepthFirstVisitor[A]{ - result: init, - reduce: r, - } - - _ = v.AcceptGuard(reducer) - - return reducer.result -} diff --git a/x/shape/shape_mkunion_guard_schema.go b/x/shape/shape_mkunion_guard_schema.go deleted file mode 100644 index 9dca904c..00000000 --- a/x/shape/shape_mkunion_guard_schema.go +++ /dev/null @@ -1,19 +0,0 @@ -// Code generated by mkunion. DO NOT EDIT. -package shape - -import ( - "github.com/widmogrod/mkunion/x/schema" -) - -func init() { - schema.RegisterUnionTypes(GuardSchemaDef()) -} - -func GuardSchemaDef() *schema.UnionVariants[Guard] { - return schema.MustDefineUnion[Guard]( - &Regexp{}, - &Between{}, - &AndGuard{}, - &OrGuard{}, - ) -} diff --git a/x/shape/shape_mkunion_guard_visitor.go b/x/shape/shape_mkunion_guard_visitor.go deleted file mode 100644 index 32e2e4c8..00000000 --- a/x/shape/shape_mkunion_guard_visitor.go +++ /dev/null @@ -1,81 +0,0 @@ -// Code generated by mkunion. DO NOT EDIT. -package shape - -import ( - "github.com/widmogrod/mkunion/f" -) - -type GuardVisitor interface { - VisitRegexp(v *Regexp) any - VisitBetween(v *Between) any - VisitAndGuard(v *AndGuard) any - VisitOrGuard(v *OrGuard) any -} - -type Guard interface { - AcceptGuard(g GuardVisitor) any -} - -func (r *Regexp) AcceptGuard(v GuardVisitor) any { return v.VisitRegexp(r) } -func (r *Between) AcceptGuard(v GuardVisitor) any { return v.VisitBetween(r) } -func (r *AndGuard) AcceptGuard(v GuardVisitor) any { return v.VisitAndGuard(r) } -func (r *OrGuard) AcceptGuard(v GuardVisitor) any { return v.VisitOrGuard(r) } - -var ( - _ Guard = (*Regexp)(nil) - _ Guard = (*Between)(nil) - _ Guard = (*AndGuard)(nil) - _ Guard = (*OrGuard)(nil) -) - -func MatchGuard[TOut any]( - x Guard, - f1 func(x *Regexp) TOut, - f2 func(x *Between) TOut, - f3 func(x *AndGuard) TOut, - f4 func(x *OrGuard) TOut, - df func(x Guard) TOut, -) TOut { - return f.Match4(x, f1, f2, f3, f4, df) -} - -func MatchGuardR2[TOut1, TOut2 any]( - x Guard, - f1 func(x *Regexp) (TOut1, TOut2), - f2 func(x *Between) (TOut1, TOut2), - f3 func(x *AndGuard) (TOut1, TOut2), - f4 func(x *OrGuard) (TOut1, TOut2), - df func(x Guard) (TOut1, TOut2), -) (TOut1, TOut2) { - return f.Match4R2(x, f1, f2, f3, f4, df) -} - -func MustMatchGuard[TOut any]( - x Guard, - f1 func(x *Regexp) TOut, - f2 func(x *Between) TOut, - f3 func(x *AndGuard) TOut, - f4 func(x *OrGuard) TOut, -) TOut { - return f.MustMatch4(x, f1, f2, f3, f4) -} - -func MustMatchGuardR0( - x Guard, - f1 func(x *Regexp), - f2 func(x *Between), - f3 func(x *AndGuard), - f4 func(x *OrGuard), -) { - f.MustMatch4R0(x, f1, f2, f3, f4) -} - -func MustMatchGuardR2[TOut1, TOut2 any]( - x Guard, - f1 func(x *Regexp) (TOut1, TOut2), - f2 func(x *Between) (TOut1, TOut2), - f3 func(x *AndGuard) (TOut1, TOut2), - f4 func(x *OrGuard) (TOut1, TOut2), -) (TOut1, TOut2) { - return f.MustMatch4R2(x, f1, f2, f3, f4) -} diff --git a/x/shape/shape_mkunion_shape_default_reducer.go b/x/shape/shape_mkunion_shape_default_reducer.go deleted file mode 100644 index d951c57a..00000000 --- a/x/shape/shape_mkunion_shape_default_reducer.go +++ /dev/null @@ -1,110 +0,0 @@ -// Code generated by mkunion. DO NOT EDIT. -package shape - -var _ ShapeReducer[any] = (*ShapeDefaultReduction[any])(nil) - -type ( - ShapeDefaultReduction[A any] struct { - PanicOnFallback bool - DefaultStopReduction bool - OnAny func(x *Any, agg A) (result A, stop bool) - OnRefName func(x *RefName, agg A) (result A, stop bool) - OnBooleanLike func(x *BooleanLike, agg A) (result A, stop bool) - OnStringLike func(x *StringLike, agg A) (result A, stop bool) - OnNumberLike func(x *NumberLike, agg A) (result A, stop bool) - OnListLike func(x *ListLike, agg A) (result A, stop bool) - OnMapLike func(x *MapLike, agg A) (result A, stop bool) - OnStructLike func(x *StructLike, agg A) (result A, stop bool) - OnUnionLike func(x *UnionLike, agg A) (result A, stop bool) - } -) - -func (t *ShapeDefaultReduction[A]) ReduceAny(x *Any, agg A) (result A, stop bool) { - if t.OnAny != nil { - return t.OnAny(x, agg) - } - if t.PanicOnFallback { - panic("no fallback allowed on undefined ReduceBranch") - } - return agg, t.DefaultStopReduction -} - -func (t *ShapeDefaultReduction[A]) ReduceRefName(x *RefName, agg A) (result A, stop bool) { - if t.OnRefName != nil { - return t.OnRefName(x, agg) - } - if t.PanicOnFallback { - panic("no fallback allowed on undefined ReduceBranch") - } - return agg, t.DefaultStopReduction -} - -func (t *ShapeDefaultReduction[A]) ReduceBooleanLike(x *BooleanLike, agg A) (result A, stop bool) { - if t.OnBooleanLike != nil { - return t.OnBooleanLike(x, agg) - } - if t.PanicOnFallback { - panic("no fallback allowed on undefined ReduceBranch") - } - return agg, t.DefaultStopReduction -} - -func (t *ShapeDefaultReduction[A]) ReduceStringLike(x *StringLike, agg A) (result A, stop bool) { - if t.OnStringLike != nil { - return t.OnStringLike(x, agg) - } - if t.PanicOnFallback { - panic("no fallback allowed on undefined ReduceBranch") - } - return agg, t.DefaultStopReduction -} - -func (t *ShapeDefaultReduction[A]) ReduceNumberLike(x *NumberLike, agg A) (result A, stop bool) { - if t.OnNumberLike != nil { - return t.OnNumberLike(x, agg) - } - if t.PanicOnFallback { - panic("no fallback allowed on undefined ReduceBranch") - } - return agg, t.DefaultStopReduction -} - -func (t *ShapeDefaultReduction[A]) ReduceListLike(x *ListLike, agg A) (result A, stop bool) { - if t.OnListLike != nil { - return t.OnListLike(x, agg) - } - if t.PanicOnFallback { - panic("no fallback allowed on undefined ReduceBranch") - } - return agg, t.DefaultStopReduction -} - -func (t *ShapeDefaultReduction[A]) ReduceMapLike(x *MapLike, agg A) (result A, stop bool) { - if t.OnMapLike != nil { - return t.OnMapLike(x, agg) - } - if t.PanicOnFallback { - panic("no fallback allowed on undefined ReduceBranch") - } - return agg, t.DefaultStopReduction -} - -func (t *ShapeDefaultReduction[A]) ReduceStructLike(x *StructLike, agg A) (result A, stop bool) { - if t.OnStructLike != nil { - return t.OnStructLike(x, agg) - } - if t.PanicOnFallback { - panic("no fallback allowed on undefined ReduceBranch") - } - return agg, t.DefaultStopReduction -} - -func (t *ShapeDefaultReduction[A]) ReduceUnionLike(x *UnionLike, agg A) (result A, stop bool) { - if t.OnUnionLike != nil { - return t.OnUnionLike(x, agg) - } - if t.PanicOnFallback { - panic("no fallback allowed on undefined ReduceBranch") - } - return agg, t.DefaultStopReduction -} diff --git a/x/shape/shape_mkunion_shape_default_visitor.go b/x/shape/shape_mkunion_shape_default_visitor.go deleted file mode 100644 index 8562d10e..00000000 --- a/x/shape/shape_mkunion_shape_default_visitor.go +++ /dev/null @@ -1,70 +0,0 @@ -// Code generated by mkunion. DO NOT EDIT. -package shape - -type ShapeDefaultVisitor[A any] struct { - Default A - OnAny func(x *Any) A - OnRefName func(x *RefName) A - OnBooleanLike func(x *BooleanLike) A - OnStringLike func(x *StringLike) A - OnNumberLike func(x *NumberLike) A - OnListLike func(x *ListLike) A - OnMapLike func(x *MapLike) A - OnStructLike func(x *StructLike) A - OnUnionLike func(x *UnionLike) A -} - -func (t *ShapeDefaultVisitor[A]) VisitAny(v *Any) any { - if t.OnAny != nil { - return t.OnAny(v) - } - return t.Default -} -func (t *ShapeDefaultVisitor[A]) VisitRefName(v *RefName) any { - if t.OnRefName != nil { - return t.OnRefName(v) - } - return t.Default -} -func (t *ShapeDefaultVisitor[A]) VisitBooleanLike(v *BooleanLike) any { - if t.OnBooleanLike != nil { - return t.OnBooleanLike(v) - } - return t.Default -} -func (t *ShapeDefaultVisitor[A]) VisitStringLike(v *StringLike) any { - if t.OnStringLike != nil { - return t.OnStringLike(v) - } - return t.Default -} -func (t *ShapeDefaultVisitor[A]) VisitNumberLike(v *NumberLike) any { - if t.OnNumberLike != nil { - return t.OnNumberLike(v) - } - return t.Default -} -func (t *ShapeDefaultVisitor[A]) VisitListLike(v *ListLike) any { - if t.OnListLike != nil { - return t.OnListLike(v) - } - return t.Default -} -func (t *ShapeDefaultVisitor[A]) VisitMapLike(v *MapLike) any { - if t.OnMapLike != nil { - return t.OnMapLike(v) - } - return t.Default -} -func (t *ShapeDefaultVisitor[A]) VisitStructLike(v *StructLike) any { - if t.OnStructLike != nil { - return t.OnStructLike(v) - } - return t.Default -} -func (t *ShapeDefaultVisitor[A]) VisitUnionLike(v *UnionLike) any { - if t.OnUnionLike != nil { - return t.OnUnionLike(v) - } - return t.Default -} diff --git a/x/shape/shape_mkunion_shape_reducer_bfs.go b/x/shape/shape_mkunion_shape_reducer_bfs.go deleted file mode 100644 index d4af2419..00000000 --- a/x/shape/shape_mkunion_shape_reducer_bfs.go +++ /dev/null @@ -1,166 +0,0 @@ -// Code generated by mkunion. DO NOT EDIT. -package shape - -var _ ShapeVisitor = (*ShapeBreadthFirstVisitor[any])(nil) - -type ShapeBreadthFirstVisitor[A any] struct { - stop bool - result A - reduce ShapeReducer[A] - - queue []Shape - visited map[Shape]bool - shouldExecute map[Shape]bool -} - -func (d *ShapeBreadthFirstVisitor[A]) VisitAny(v *Any) any { - d.queue = append(d.queue, v) - - if d.shouldExecute[v] { - d.shouldExecute[v] = false - d.result, d.stop = d.reduce.ReduceAny(v, d.result) - } else { - d.execute() - } - return nil -} - -func (d *ShapeBreadthFirstVisitor[A]) VisitRefName(v *RefName) any { - d.queue = append(d.queue, v) - - if d.shouldExecute[v] { - d.shouldExecute[v] = false - d.result, d.stop = d.reduce.ReduceRefName(v, d.result) - } else { - d.execute() - } - return nil -} - -func (d *ShapeBreadthFirstVisitor[A]) VisitBooleanLike(v *BooleanLike) any { - d.queue = append(d.queue, v) - - if d.shouldExecute[v] { - d.shouldExecute[v] = false - d.result, d.stop = d.reduce.ReduceBooleanLike(v, d.result) - } else { - d.execute() - } - return nil -} - -func (d *ShapeBreadthFirstVisitor[A]) VisitStringLike(v *StringLike) any { - d.queue = append(d.queue, v) - - if d.shouldExecute[v] { - d.shouldExecute[v] = false - d.result, d.stop = d.reduce.ReduceStringLike(v, d.result) - } else { - d.execute() - } - return nil -} - -func (d *ShapeBreadthFirstVisitor[A]) VisitNumberLike(v *NumberLike) any { - d.queue = append(d.queue, v) - - if d.shouldExecute[v] { - d.shouldExecute[v] = false - d.result, d.stop = d.reduce.ReduceNumberLike(v, d.result) - } else { - d.execute() - } - return nil -} - -func (d *ShapeBreadthFirstVisitor[A]) VisitListLike(v *ListLike) any { - d.queue = append(d.queue, v) - d.queue = append(d.queue, v.Element) - - if d.shouldExecute[v] { - d.shouldExecute[v] = false - d.result, d.stop = d.reduce.ReduceListLike(v, d.result) - } else { - d.execute() - } - return nil -} - -func (d *ShapeBreadthFirstVisitor[A]) VisitMapLike(v *MapLike) any { - d.queue = append(d.queue, v) - d.queue = append(d.queue, v.Key) - d.queue = append(d.queue, v.Val) - - if d.shouldExecute[v] { - d.shouldExecute[v] = false - d.result, d.stop = d.reduce.ReduceMapLike(v, d.result) - } else { - d.execute() - } - return nil -} - -func (d *ShapeBreadthFirstVisitor[A]) VisitStructLike(v *StructLike) any { - d.queue = append(d.queue, v) - - if d.shouldExecute[v] { - d.shouldExecute[v] = false - d.result, d.stop = d.reduce.ReduceStructLike(v, d.result) - } else { - d.execute() - } - return nil -} - -func (d *ShapeBreadthFirstVisitor[A]) VisitUnionLike(v *UnionLike) any { - d.queue = append(d.queue, v) - for idx := range v.Variant { - d.queue = append(d.queue, v.Variant[idx]) - } - - if d.shouldExecute[v] { - d.shouldExecute[v] = false - d.result, d.stop = d.reduce.ReduceUnionLike(v, d.result) - } else { - d.execute() - } - return nil -} - -func (d *ShapeBreadthFirstVisitor[A]) execute() { - for len(d.queue) > 0 { - if d.stop { - return - } - - i := d.pop() - if d.visited[i] { - continue - } - d.visited[i] = true - d.shouldExecute[i] = true - i.AcceptShape(d) - } - - return -} - -func (d *ShapeBreadthFirstVisitor[A]) pop() Shape { - i := d.queue[0] - d.queue = d.queue[1:] - return i -} - -func ReduceShapeBreadthFirst[A any](r ShapeReducer[A], v Shape, init A) A { - reducer := &ShapeBreadthFirstVisitor[A]{ - result: init, - reduce: r, - queue: []Shape{v}, - visited: make(map[Shape]bool), - shouldExecute: make(map[Shape]bool), - } - - _ = v.AcceptShape(reducer) - - return reducer.result -} diff --git a/x/shape/shape_mkunion_shape_reducer_dfs.go b/x/shape/shape_mkunion_shape_reducer_dfs.go deleted file mode 100644 index 76ce8c7e..00000000 --- a/x/shape/shape_mkunion_shape_reducer_dfs.go +++ /dev/null @@ -1,130 +0,0 @@ -// Code generated by mkunion. DO NOT EDIT. -package shape - -type ( - ShapeReducer[A any] interface { - ReduceAny(x *Any, agg A) (result A, stop bool) - ReduceRefName(x *RefName, agg A) (result A, stop bool) - ReduceBooleanLike(x *BooleanLike, agg A) (result A, stop bool) - ReduceStringLike(x *StringLike, agg A) (result A, stop bool) - ReduceNumberLike(x *NumberLike, agg A) (result A, stop bool) - ReduceListLike(x *ListLike, agg A) (result A, stop bool) - ReduceMapLike(x *MapLike, agg A) (result A, stop bool) - ReduceStructLike(x *StructLike, agg A) (result A, stop bool) - ReduceUnionLike(x *UnionLike, agg A) (result A, stop bool) - } -) - -type ShapeDepthFirstVisitor[A any] struct { - stop bool - result A - reduce ShapeReducer[A] -} - -var _ ShapeVisitor = (*ShapeDepthFirstVisitor[any])(nil) - -func (d *ShapeDepthFirstVisitor[A]) VisitAny(v *Any) any { - d.result, d.stop = d.reduce.ReduceAny(v, d.result) - if d.stop { - return nil - } - - return nil -} - -func (d *ShapeDepthFirstVisitor[A]) VisitRefName(v *RefName) any { - d.result, d.stop = d.reduce.ReduceRefName(v, d.result) - if d.stop { - return nil - } - - return nil -} - -func (d *ShapeDepthFirstVisitor[A]) VisitBooleanLike(v *BooleanLike) any { - d.result, d.stop = d.reduce.ReduceBooleanLike(v, d.result) - if d.stop { - return nil - } - - return nil -} - -func (d *ShapeDepthFirstVisitor[A]) VisitStringLike(v *StringLike) any { - d.result, d.stop = d.reduce.ReduceStringLike(v, d.result) - if d.stop { - return nil - } - - return nil -} - -func (d *ShapeDepthFirstVisitor[A]) VisitNumberLike(v *NumberLike) any { - d.result, d.stop = d.reduce.ReduceNumberLike(v, d.result) - if d.stop { - return nil - } - - return nil -} - -func (d *ShapeDepthFirstVisitor[A]) VisitListLike(v *ListLike) any { - d.result, d.stop = d.reduce.ReduceListLike(v, d.result) - if d.stop { - return nil - } - if _ = v.Element.AcceptShape(d); d.stop { - return nil - } - - return nil -} - -func (d *ShapeDepthFirstVisitor[A]) VisitMapLike(v *MapLike) any { - d.result, d.stop = d.reduce.ReduceMapLike(v, d.result) - if d.stop { - return nil - } - if _ = v.Key.AcceptShape(d); d.stop { - return nil - } - if _ = v.Val.AcceptShape(d); d.stop { - return nil - } - - return nil -} - -func (d *ShapeDepthFirstVisitor[A]) VisitStructLike(v *StructLike) any { - d.result, d.stop = d.reduce.ReduceStructLike(v, d.result) - if d.stop { - return nil - } - - return nil -} - -func (d *ShapeDepthFirstVisitor[A]) VisitUnionLike(v *UnionLike) any { - d.result, d.stop = d.reduce.ReduceUnionLike(v, d.result) - if d.stop { - return nil - } - for idx := range v.Variant { - if _ = v.Variant[idx].AcceptShape(d); d.stop { - return nil - } - } - - return nil -} - -func ReduceShapeDepthFirst[A any](r ShapeReducer[A], v Shape, init A) A { - reducer := &ShapeDepthFirstVisitor[A]{ - result: init, - reduce: r, - } - - _ = v.AcceptShape(reducer) - - return reducer.result -} diff --git a/x/shape/shape_mkunion_shape_schema.go b/x/shape/shape_mkunion_shape_schema.go deleted file mode 100644 index a9a285ab..00000000 --- a/x/shape/shape_mkunion_shape_schema.go +++ /dev/null @@ -1,24 +0,0 @@ -// Code generated by mkunion. DO NOT EDIT. -package shape - -import ( - "github.com/widmogrod/mkunion/x/schema" -) - -func init() { - schema.RegisterUnionTypes(ShapeSchemaDef()) -} - -func ShapeSchemaDef() *schema.UnionVariants[Shape] { - return schema.MustDefineUnion[Shape]( - &Any{}, - &RefName{}, - &BooleanLike{}, - &StringLike{}, - &NumberLike{}, - &ListLike{}, - &MapLike{}, - &StructLike{}, - &UnionLike{}, - ) -} diff --git a/x/shape/shape_mkunion_shape_visitor.go b/x/shape/shape_mkunion_shape_visitor.go deleted file mode 100644 index dd529f0c..00000000 --- a/x/shape/shape_mkunion_shape_visitor.go +++ /dev/null @@ -1,121 +0,0 @@ -// Code generated by mkunion. DO NOT EDIT. -package shape - -import ( - "github.com/widmogrod/mkunion/f" -) - -type ShapeVisitor interface { - VisitAny(v *Any) any - VisitRefName(v *RefName) any - VisitBooleanLike(v *BooleanLike) any - VisitStringLike(v *StringLike) any - VisitNumberLike(v *NumberLike) any - VisitListLike(v *ListLike) any - VisitMapLike(v *MapLike) any - VisitStructLike(v *StructLike) any - VisitUnionLike(v *UnionLike) any -} - -type Shape interface { - AcceptShape(g ShapeVisitor) any -} - -func (r *Any) AcceptShape(v ShapeVisitor) any { return v.VisitAny(r) } -func (r *RefName) AcceptShape(v ShapeVisitor) any { return v.VisitRefName(r) } -func (r *BooleanLike) AcceptShape(v ShapeVisitor) any { return v.VisitBooleanLike(r) } -func (r *StringLike) AcceptShape(v ShapeVisitor) any { return v.VisitStringLike(r) } -func (r *NumberLike) AcceptShape(v ShapeVisitor) any { return v.VisitNumberLike(r) } -func (r *ListLike) AcceptShape(v ShapeVisitor) any { return v.VisitListLike(r) } -func (r *MapLike) AcceptShape(v ShapeVisitor) any { return v.VisitMapLike(r) } -func (r *StructLike) AcceptShape(v ShapeVisitor) any { return v.VisitStructLike(r) } -func (r *UnionLike) AcceptShape(v ShapeVisitor) any { return v.VisitUnionLike(r) } - -var ( - _ Shape = (*Any)(nil) - _ Shape = (*RefName)(nil) - _ Shape = (*BooleanLike)(nil) - _ Shape = (*StringLike)(nil) - _ Shape = (*NumberLike)(nil) - _ Shape = (*ListLike)(nil) - _ Shape = (*MapLike)(nil) - _ Shape = (*StructLike)(nil) - _ Shape = (*UnionLike)(nil) -) - -func MatchShape[TOut any]( - x Shape, - f1 func(x *Any) TOut, - f2 func(x *RefName) TOut, - f3 func(x *BooleanLike) TOut, - f4 func(x *StringLike) TOut, - f5 func(x *NumberLike) TOut, - f6 func(x *ListLike) TOut, - f7 func(x *MapLike) TOut, - f8 func(x *StructLike) TOut, - f9 func(x *UnionLike) TOut, - df func(x Shape) TOut, -) TOut { - return f.Match9(x, f1, f2, f3, f4, f5, f6, f7, f8, f9, df) -} - -func MatchShapeR2[TOut1, TOut2 any]( - x Shape, - f1 func(x *Any) (TOut1, TOut2), - f2 func(x *RefName) (TOut1, TOut2), - f3 func(x *BooleanLike) (TOut1, TOut2), - f4 func(x *StringLike) (TOut1, TOut2), - f5 func(x *NumberLike) (TOut1, TOut2), - f6 func(x *ListLike) (TOut1, TOut2), - f7 func(x *MapLike) (TOut1, TOut2), - f8 func(x *StructLike) (TOut1, TOut2), - f9 func(x *UnionLike) (TOut1, TOut2), - df func(x Shape) (TOut1, TOut2), -) (TOut1, TOut2) { - return f.Match9R2(x, f1, f2, f3, f4, f5, f6, f7, f8, f9, df) -} - -func MustMatchShape[TOut any]( - x Shape, - f1 func(x *Any) TOut, - f2 func(x *RefName) TOut, - f3 func(x *BooleanLike) TOut, - f4 func(x *StringLike) TOut, - f5 func(x *NumberLike) TOut, - f6 func(x *ListLike) TOut, - f7 func(x *MapLike) TOut, - f8 func(x *StructLike) TOut, - f9 func(x *UnionLike) TOut, -) TOut { - return f.MustMatch9(x, f1, f2, f3, f4, f5, f6, f7, f8, f9) -} - -func MustMatchShapeR0( - x Shape, - f1 func(x *Any), - f2 func(x *RefName), - f3 func(x *BooleanLike), - f4 func(x *StringLike), - f5 func(x *NumberLike), - f6 func(x *ListLike), - f7 func(x *MapLike), - f8 func(x *StructLike), - f9 func(x *UnionLike), -) { - f.MustMatch9R0(x, f1, f2, f3, f4, f5, f6, f7, f8, f9) -} - -func MustMatchShapeR2[TOut1, TOut2 any]( - x Shape, - f1 func(x *Any) (TOut1, TOut2), - f2 func(x *RefName) (TOut1, TOut2), - f3 func(x *BooleanLike) (TOut1, TOut2), - f4 func(x *StringLike) (TOut1, TOut2), - f5 func(x *NumberLike) (TOut1, TOut2), - f6 func(x *ListLike) (TOut1, TOut2), - f7 func(x *MapLike) (TOut1, TOut2), - f8 func(x *StructLike) (TOut1, TOut2), - f9 func(x *UnionLike) (TOut1, TOut2), -) (TOut1, TOut2) { - return f.MustMatch9R2(x, f1, f2, f3, f4, f5, f6, f7, f8, f9) -} diff --git a/x/storage/predicate/predicate_mkunion_bindable_default_reducer.go b/x/storage/predicate/predicate_mkunion_bindable_default_reducer.go deleted file mode 100644 index 2824a162..00000000 --- a/x/storage/predicate/predicate_mkunion_bindable_default_reducer.go +++ /dev/null @@ -1,44 +0,0 @@ -// Code generated by mkunion. DO NOT EDIT. -package predicate - -var _ BindableReducer[any] = (*BindableDefaultReduction[any])(nil) - -type ( - BindableDefaultReduction[A any] struct { - PanicOnFallback bool - DefaultStopReduction bool - OnBindValue func(x *BindValue, agg A) (result A, stop bool) - OnLiteral func(x *Literal, agg A) (result A, stop bool) - OnLocatable func(x *Locatable, agg A) (result A, stop bool) - } -) - -func (t *BindableDefaultReduction[A]) ReduceBindValue(x *BindValue, agg A) (result A, stop bool) { - if t.OnBindValue != nil { - return t.OnBindValue(x, agg) - } - if t.PanicOnFallback { - panic("no fallback allowed on undefined ReduceBranch") - } - return agg, t.DefaultStopReduction -} - -func (t *BindableDefaultReduction[A]) ReduceLiteral(x *Literal, agg A) (result A, stop bool) { - if t.OnLiteral != nil { - return t.OnLiteral(x, agg) - } - if t.PanicOnFallback { - panic("no fallback allowed on undefined ReduceBranch") - } - return agg, t.DefaultStopReduction -} - -func (t *BindableDefaultReduction[A]) ReduceLocatable(x *Locatable, agg A) (result A, stop bool) { - if t.OnLocatable != nil { - return t.OnLocatable(x, agg) - } - if t.PanicOnFallback { - panic("no fallback allowed on undefined ReduceBranch") - } - return agg, t.DefaultStopReduction -} diff --git a/x/storage/predicate/predicate_mkunion_bindable_default_visitor.go b/x/storage/predicate/predicate_mkunion_bindable_default_visitor.go deleted file mode 100644 index 7b02838b..00000000 --- a/x/storage/predicate/predicate_mkunion_bindable_default_visitor.go +++ /dev/null @@ -1,28 +0,0 @@ -// Code generated by mkunion. DO NOT EDIT. -package predicate - -type BindableDefaultVisitor[A any] struct { - Default A - OnBindValue func(x *BindValue) A - OnLiteral func(x *Literal) A - OnLocatable func(x *Locatable) A -} - -func (t *BindableDefaultVisitor[A]) VisitBindValue(v *BindValue) any { - if t.OnBindValue != nil { - return t.OnBindValue(v) - } - return t.Default -} -func (t *BindableDefaultVisitor[A]) VisitLiteral(v *Literal) any { - if t.OnLiteral != nil { - return t.OnLiteral(v) - } - return t.Default -} -func (t *BindableDefaultVisitor[A]) VisitLocatable(v *Locatable) any { - if t.OnLocatable != nil { - return t.OnLocatable(v) - } - return t.Default -} diff --git a/x/storage/predicate/predicate_mkunion_bindable_reducer_bfs.go b/x/storage/predicate/predicate_mkunion_bindable_reducer_bfs.go deleted file mode 100644 index beaed0c1..00000000 --- a/x/storage/predicate/predicate_mkunion_bindable_reducer_bfs.go +++ /dev/null @@ -1,88 +0,0 @@ -// Code generated by mkunion. DO NOT EDIT. -package predicate - -var _ BindableVisitor = (*BindableBreadthFirstVisitor[any])(nil) - -type BindableBreadthFirstVisitor[A any] struct { - stop bool - result A - reduce BindableReducer[A] - - queue []Bindable - visited map[Bindable]bool - shouldExecute map[Bindable]bool -} - -func (d *BindableBreadthFirstVisitor[A]) VisitBindValue(v *BindValue) any { - d.queue = append(d.queue, v) - - if d.shouldExecute[v] { - d.shouldExecute[v] = false - d.result, d.stop = d.reduce.ReduceBindValue(v, d.result) - } else { - d.execute() - } - return nil -} - -func (d *BindableBreadthFirstVisitor[A]) VisitLiteral(v *Literal) any { - d.queue = append(d.queue, v) - - if d.shouldExecute[v] { - d.shouldExecute[v] = false - d.result, d.stop = d.reduce.ReduceLiteral(v, d.result) - } else { - d.execute() - } - return nil -} - -func (d *BindableBreadthFirstVisitor[A]) VisitLocatable(v *Locatable) any { - d.queue = append(d.queue, v) - - if d.shouldExecute[v] { - d.shouldExecute[v] = false - d.result, d.stop = d.reduce.ReduceLocatable(v, d.result) - } else { - d.execute() - } - return nil -} - -func (d *BindableBreadthFirstVisitor[A]) execute() { - for len(d.queue) > 0 { - if d.stop { - return - } - - i := d.pop() - if d.visited[i] { - continue - } - d.visited[i] = true - d.shouldExecute[i] = true - i.AcceptBindable(d) - } - - return -} - -func (d *BindableBreadthFirstVisitor[A]) pop() Bindable { - i := d.queue[0] - d.queue = d.queue[1:] - return i -} - -func ReduceBindableBreadthFirst[A any](r BindableReducer[A], v Bindable, init A) A { - reducer := &BindableBreadthFirstVisitor[A]{ - result: init, - reduce: r, - queue: []Bindable{v}, - visited: make(map[Bindable]bool), - shouldExecute: make(map[Bindable]bool), - } - - _ = v.AcceptBindable(reducer) - - return reducer.result -} diff --git a/x/storage/predicate/predicate_mkunion_bindable_reducer_dfs.go b/x/storage/predicate/predicate_mkunion_bindable_reducer_dfs.go deleted file mode 100644 index e55e5506..00000000 --- a/x/storage/predicate/predicate_mkunion_bindable_reducer_dfs.go +++ /dev/null @@ -1,56 +0,0 @@ -// Code generated by mkunion. DO NOT EDIT. -package predicate - -type ( - BindableReducer[A any] interface { - ReduceBindValue(x *BindValue, agg A) (result A, stop bool) - ReduceLiteral(x *Literal, agg A) (result A, stop bool) - ReduceLocatable(x *Locatable, agg A) (result A, stop bool) - } -) - -type BindableDepthFirstVisitor[A any] struct { - stop bool - result A - reduce BindableReducer[A] -} - -var _ BindableVisitor = (*BindableDepthFirstVisitor[any])(nil) - -func (d *BindableDepthFirstVisitor[A]) VisitBindValue(v *BindValue) any { - d.result, d.stop = d.reduce.ReduceBindValue(v, d.result) - if d.stop { - return nil - } - - return nil -} - -func (d *BindableDepthFirstVisitor[A]) VisitLiteral(v *Literal) any { - d.result, d.stop = d.reduce.ReduceLiteral(v, d.result) - if d.stop { - return nil - } - - return nil -} - -func (d *BindableDepthFirstVisitor[A]) VisitLocatable(v *Locatable) any { - d.result, d.stop = d.reduce.ReduceLocatable(v, d.result) - if d.stop { - return nil - } - - return nil -} - -func ReduceBindableDepthFirst[A any](r BindableReducer[A], v Bindable, init A) A { - reducer := &BindableDepthFirstVisitor[A]{ - result: init, - reduce: r, - } - - _ = v.AcceptBindable(reducer) - - return reducer.result -} diff --git a/x/storage/predicate/predicate_mkunion_bindable_schema.go b/x/storage/predicate/predicate_mkunion_bindable_schema.go deleted file mode 100644 index e2528efe..00000000 --- a/x/storage/predicate/predicate_mkunion_bindable_schema.go +++ /dev/null @@ -1,18 +0,0 @@ -// Code generated by mkunion. DO NOT EDIT. -package predicate - -import ( - "github.com/widmogrod/mkunion/x/schema" -) - -func init() { - schema.RegisterUnionTypes(BindableSchemaDef()) -} - -func BindableSchemaDef() *schema.UnionVariants[Bindable] { - return schema.MustDefineUnion[Bindable]( - &BindValue{}, - &Literal{}, - &Locatable{}, - ) -} diff --git a/x/storage/predicate/predicate_mkunion_bindable_visitor.go b/x/storage/predicate/predicate_mkunion_bindable_visitor.go deleted file mode 100644 index 0fcfa5d4..00000000 --- a/x/storage/predicate/predicate_mkunion_bindable_visitor.go +++ /dev/null @@ -1,73 +0,0 @@ -// Code generated by mkunion. DO NOT EDIT. -package predicate - -import ( - "github.com/widmogrod/mkunion/f" -) - -type BindableVisitor interface { - VisitBindValue(v *BindValue) any - VisitLiteral(v *Literal) any - VisitLocatable(v *Locatable) any -} - -type Bindable interface { - AcceptBindable(g BindableVisitor) any -} - -func (r *BindValue) AcceptBindable(v BindableVisitor) any { return v.VisitBindValue(r) } -func (r *Literal) AcceptBindable(v BindableVisitor) any { return v.VisitLiteral(r) } -func (r *Locatable) AcceptBindable(v BindableVisitor) any { return v.VisitLocatable(r) } - -var ( - _ Bindable = (*BindValue)(nil) - _ Bindable = (*Literal)(nil) - _ Bindable = (*Locatable)(nil) -) - -func MatchBindable[TOut any]( - x Bindable, - f1 func(x *BindValue) TOut, - f2 func(x *Literal) TOut, - f3 func(x *Locatable) TOut, - df func(x Bindable) TOut, -) TOut { - return f.Match3(x, f1, f2, f3, df) -} - -func MatchBindableR2[TOut1, TOut2 any]( - x Bindable, - f1 func(x *BindValue) (TOut1, TOut2), - f2 func(x *Literal) (TOut1, TOut2), - f3 func(x *Locatable) (TOut1, TOut2), - df func(x Bindable) (TOut1, TOut2), -) (TOut1, TOut2) { - return f.Match3R2(x, f1, f2, f3, df) -} - -func MustMatchBindable[TOut any]( - x Bindable, - f1 func(x *BindValue) TOut, - f2 func(x *Literal) TOut, - f3 func(x *Locatable) TOut, -) TOut { - return f.MustMatch3(x, f1, f2, f3) -} - -func MustMatchBindableR0( - x Bindable, - f1 func(x *BindValue), - f2 func(x *Literal), - f3 func(x *Locatable), -) { - f.MustMatch3R0(x, f1, f2, f3) -} - -func MustMatchBindableR2[TOut1, TOut2 any]( - x Bindable, - f1 func(x *BindValue) (TOut1, TOut2), - f2 func(x *Literal) (TOut1, TOut2), - f3 func(x *Locatable) (TOut1, TOut2), -) (TOut1, TOut2) { - return f.MustMatch3R2(x, f1, f2, f3) -} diff --git a/x/storage/predicate/predicate_mkunion_predicate_default_reducer.go b/x/storage/predicate/predicate_mkunion_predicate_default_reducer.go deleted file mode 100644 index ce87a4d7..00000000 --- a/x/storage/predicate/predicate_mkunion_predicate_default_reducer.go +++ /dev/null @@ -1,55 +0,0 @@ -// Code generated by mkunion. DO NOT EDIT. -package predicate - -var _ PredicateReducer[any] = (*PredicateDefaultReduction[any])(nil) - -type ( - PredicateDefaultReduction[A any] struct { - PanicOnFallback bool - DefaultStopReduction bool - OnAnd func(x *And, agg A) (result A, stop bool) - OnOr func(x *Or, agg A) (result A, stop bool) - OnNot func(x *Not, agg A) (result A, stop bool) - OnCompare func(x *Compare, agg A) (result A, stop bool) - } -) - -func (t *PredicateDefaultReduction[A]) ReduceAnd(x *And, agg A) (result A, stop bool) { - if t.OnAnd != nil { - return t.OnAnd(x, agg) - } - if t.PanicOnFallback { - panic("no fallback allowed on undefined ReduceBranch") - } - return agg, t.DefaultStopReduction -} - -func (t *PredicateDefaultReduction[A]) ReduceOr(x *Or, agg A) (result A, stop bool) { - if t.OnOr != nil { - return t.OnOr(x, agg) - } - if t.PanicOnFallback { - panic("no fallback allowed on undefined ReduceBranch") - } - return agg, t.DefaultStopReduction -} - -func (t *PredicateDefaultReduction[A]) ReduceNot(x *Not, agg A) (result A, stop bool) { - if t.OnNot != nil { - return t.OnNot(x, agg) - } - if t.PanicOnFallback { - panic("no fallback allowed on undefined ReduceBranch") - } - return agg, t.DefaultStopReduction -} - -func (t *PredicateDefaultReduction[A]) ReduceCompare(x *Compare, agg A) (result A, stop bool) { - if t.OnCompare != nil { - return t.OnCompare(x, agg) - } - if t.PanicOnFallback { - panic("no fallback allowed on undefined ReduceBranch") - } - return agg, t.DefaultStopReduction -} diff --git a/x/storage/predicate/predicate_mkunion_predicate_default_visitor.go b/x/storage/predicate/predicate_mkunion_predicate_default_visitor.go deleted file mode 100644 index 57d1d22f..00000000 --- a/x/storage/predicate/predicate_mkunion_predicate_default_visitor.go +++ /dev/null @@ -1,35 +0,0 @@ -// Code generated by mkunion. DO NOT EDIT. -package predicate - -type PredicateDefaultVisitor[A any] struct { - Default A - OnAnd func(x *And) A - OnOr func(x *Or) A - OnNot func(x *Not) A - OnCompare func(x *Compare) A -} - -func (t *PredicateDefaultVisitor[A]) VisitAnd(v *And) any { - if t.OnAnd != nil { - return t.OnAnd(v) - } - return t.Default -} -func (t *PredicateDefaultVisitor[A]) VisitOr(v *Or) any { - if t.OnOr != nil { - return t.OnOr(v) - } - return t.Default -} -func (t *PredicateDefaultVisitor[A]) VisitNot(v *Not) any { - if t.OnNot != nil { - return t.OnNot(v) - } - return t.Default -} -func (t *PredicateDefaultVisitor[A]) VisitCompare(v *Compare) any { - if t.OnCompare != nil { - return t.OnCompare(v) - } - return t.Default -} diff --git a/x/storage/predicate/predicate_mkunion_predicate_reducer_bfs.go b/x/storage/predicate/predicate_mkunion_predicate_reducer_bfs.go deleted file mode 100644 index 97d51dd6..00000000 --- a/x/storage/predicate/predicate_mkunion_predicate_reducer_bfs.go +++ /dev/null @@ -1,107 +0,0 @@ -// Code generated by mkunion. DO NOT EDIT. -package predicate - -var _ PredicateVisitor = (*PredicateBreadthFirstVisitor[any])(nil) - -type PredicateBreadthFirstVisitor[A any] struct { - stop bool - result A - reduce PredicateReducer[A] - - queue []Predicate - visited map[Predicate]bool - shouldExecute map[Predicate]bool -} - -func (d *PredicateBreadthFirstVisitor[A]) VisitAnd(v *And) any { - d.queue = append(d.queue, v) - for idx := range v.L { - d.queue = append(d.queue, v.L[idx]) - } - - if d.shouldExecute[v] { - d.shouldExecute[v] = false - d.result, d.stop = d.reduce.ReduceAnd(v, d.result) - } else { - d.execute() - } - return nil -} - -func (d *PredicateBreadthFirstVisitor[A]) VisitOr(v *Or) any { - d.queue = append(d.queue, v) - for idx := range v.L { - d.queue = append(d.queue, v.L[idx]) - } - - if d.shouldExecute[v] { - d.shouldExecute[v] = false - d.result, d.stop = d.reduce.ReduceOr(v, d.result) - } else { - d.execute() - } - return nil -} - -func (d *PredicateBreadthFirstVisitor[A]) VisitNot(v *Not) any { - d.queue = append(d.queue, v) - d.queue = append(d.queue, v.P) - - if d.shouldExecute[v] { - d.shouldExecute[v] = false - d.result, d.stop = d.reduce.ReduceNot(v, d.result) - } else { - d.execute() - } - return nil -} - -func (d *PredicateBreadthFirstVisitor[A]) VisitCompare(v *Compare) any { - d.queue = append(d.queue, v) - - if d.shouldExecute[v] { - d.shouldExecute[v] = false - d.result, d.stop = d.reduce.ReduceCompare(v, d.result) - } else { - d.execute() - } - return nil -} - -func (d *PredicateBreadthFirstVisitor[A]) execute() { - for len(d.queue) > 0 { - if d.stop { - return - } - - i := d.pop() - if d.visited[i] { - continue - } - d.visited[i] = true - d.shouldExecute[i] = true - i.AcceptPredicate(d) - } - - return -} - -func (d *PredicateBreadthFirstVisitor[A]) pop() Predicate { - i := d.queue[0] - d.queue = d.queue[1:] - return i -} - -func ReducePredicateBreadthFirst[A any](r PredicateReducer[A], v Predicate, init A) A { - reducer := &PredicateBreadthFirstVisitor[A]{ - result: init, - reduce: r, - queue: []Predicate{v}, - visited: make(map[Predicate]bool), - shouldExecute: make(map[Predicate]bool), - } - - _ = v.AcceptPredicate(reducer) - - return reducer.result -} diff --git a/x/storage/predicate/predicate_mkunion_predicate_reducer_dfs.go b/x/storage/predicate/predicate_mkunion_predicate_reducer_dfs.go deleted file mode 100644 index 71920695..00000000 --- a/x/storage/predicate/predicate_mkunion_predicate_reducer_dfs.go +++ /dev/null @@ -1,79 +0,0 @@ -// Code generated by mkunion. DO NOT EDIT. -package predicate - -type ( - PredicateReducer[A any] interface { - ReduceAnd(x *And, agg A) (result A, stop bool) - ReduceOr(x *Or, agg A) (result A, stop bool) - ReduceNot(x *Not, agg A) (result A, stop bool) - ReduceCompare(x *Compare, agg A) (result A, stop bool) - } -) - -type PredicateDepthFirstVisitor[A any] struct { - stop bool - result A - reduce PredicateReducer[A] -} - -var _ PredicateVisitor = (*PredicateDepthFirstVisitor[any])(nil) - -func (d *PredicateDepthFirstVisitor[A]) VisitAnd(v *And) any { - d.result, d.stop = d.reduce.ReduceAnd(v, d.result) - if d.stop { - return nil - } - for idx := range v.L { - if _ = v.L[idx].AcceptPredicate(d); d.stop { - return nil - } - } - - return nil -} - -func (d *PredicateDepthFirstVisitor[A]) VisitOr(v *Or) any { - d.result, d.stop = d.reduce.ReduceOr(v, d.result) - if d.stop { - return nil - } - for idx := range v.L { - if _ = v.L[idx].AcceptPredicate(d); d.stop { - return nil - } - } - - return nil -} - -func (d *PredicateDepthFirstVisitor[A]) VisitNot(v *Not) any { - d.result, d.stop = d.reduce.ReduceNot(v, d.result) - if d.stop { - return nil - } - if _ = v.P.AcceptPredicate(d); d.stop { - return nil - } - - return nil -} - -func (d *PredicateDepthFirstVisitor[A]) VisitCompare(v *Compare) any { - d.result, d.stop = d.reduce.ReduceCompare(v, d.result) - if d.stop { - return nil - } - - return nil -} - -func ReducePredicateDepthFirst[A any](r PredicateReducer[A], v Predicate, init A) A { - reducer := &PredicateDepthFirstVisitor[A]{ - result: init, - reduce: r, - } - - _ = v.AcceptPredicate(reducer) - - return reducer.result -} diff --git a/x/storage/predicate/predicate_mkunion_predicate_schema.go b/x/storage/predicate/predicate_mkunion_predicate_schema.go deleted file mode 100644 index f2f51cda..00000000 --- a/x/storage/predicate/predicate_mkunion_predicate_schema.go +++ /dev/null @@ -1,19 +0,0 @@ -// Code generated by mkunion. DO NOT EDIT. -package predicate - -import ( - "github.com/widmogrod/mkunion/x/schema" -) - -func init() { - schema.RegisterUnionTypes(PredicateSchemaDef()) -} - -func PredicateSchemaDef() *schema.UnionVariants[Predicate] { - return schema.MustDefineUnion[Predicate]( - &And{}, - &Or{}, - &Not{}, - &Compare{}, - ) -} diff --git a/x/storage/predicate/predicate_mkunion_predicate_visitor.go b/x/storage/predicate/predicate_mkunion_predicate_visitor.go deleted file mode 100644 index ce090a48..00000000 --- a/x/storage/predicate/predicate_mkunion_predicate_visitor.go +++ /dev/null @@ -1,81 +0,0 @@ -// Code generated by mkunion. DO NOT EDIT. -package predicate - -import ( - "github.com/widmogrod/mkunion/f" -) - -type PredicateVisitor interface { - VisitAnd(v *And) any - VisitOr(v *Or) any - VisitNot(v *Not) any - VisitCompare(v *Compare) any -} - -type Predicate interface { - AcceptPredicate(g PredicateVisitor) any -} - -func (r *And) AcceptPredicate(v PredicateVisitor) any { return v.VisitAnd(r) } -func (r *Or) AcceptPredicate(v PredicateVisitor) any { return v.VisitOr(r) } -func (r *Not) AcceptPredicate(v PredicateVisitor) any { return v.VisitNot(r) } -func (r *Compare) AcceptPredicate(v PredicateVisitor) any { return v.VisitCompare(r) } - -var ( - _ Predicate = (*And)(nil) - _ Predicate = (*Or)(nil) - _ Predicate = (*Not)(nil) - _ Predicate = (*Compare)(nil) -) - -func MatchPredicate[TOut any]( - x Predicate, - f1 func(x *And) TOut, - f2 func(x *Or) TOut, - f3 func(x *Not) TOut, - f4 func(x *Compare) TOut, - df func(x Predicate) TOut, -) TOut { - return f.Match4(x, f1, f2, f3, f4, df) -} - -func MatchPredicateR2[TOut1, TOut2 any]( - x Predicate, - f1 func(x *And) (TOut1, TOut2), - f2 func(x *Or) (TOut1, TOut2), - f3 func(x *Not) (TOut1, TOut2), - f4 func(x *Compare) (TOut1, TOut2), - df func(x Predicate) (TOut1, TOut2), -) (TOut1, TOut2) { - return f.Match4R2(x, f1, f2, f3, f4, df) -} - -func MustMatchPredicate[TOut any]( - x Predicate, - f1 func(x *And) TOut, - f2 func(x *Or) TOut, - f3 func(x *Not) TOut, - f4 func(x *Compare) TOut, -) TOut { - return f.MustMatch4(x, f1, f2, f3, f4) -} - -func MustMatchPredicateR0( - x Predicate, - f1 func(x *And), - f2 func(x *Or), - f3 func(x *Not), - f4 func(x *Compare), -) { - f.MustMatch4R0(x, f1, f2, f3, f4) -} - -func MustMatchPredicateR2[TOut1, TOut2 any]( - x Predicate, - f1 func(x *And) (TOut1, TOut2), - f2 func(x *Or) (TOut1, TOut2), - f3 func(x *Not) (TOut1, TOut2), - f4 func(x *Compare) (TOut1, TOut2), -) (TOut1, TOut2) { - return f.MustMatch4R2(x, f1, f2, f3, f4) -}