Skip to content

Commit

Permalink
Merge pull request #13 from widmogrod/feature/example4
Browse files Browse the repository at this point in the history
Add support for map[any]{Variant} type
  • Loading branch information
widmogrod authored Oct 23, 2022
2 parents a2609e8 + 5f2731e commit 6b6eb3d
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 11 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,21 @@ go test ./...
```

## Roadmap ideas
### V1.0.x
- [x] Add visitor generation for unions
- [x] Add support for depth-first traversal
- [x] Add support for slice []{Variant} type traversal

### V1.1.x
- [ ] Add support for map[any]{Variant} type
- [x] Add support for map[any]{Variant} type
- [ ] Add breath-first reducer traversal
- [ ] Add leaf-first reducer traversal
- [ ] Add leaf-first reducer traversal [?]

### V1.2.x
- [ ] Add support for not-stop able reducer

### V2.x.x
- [ ] Add support for generic union types
- [ ] Add support for generic union types

### Knows bugs
- [ ] Multiple go:generates mkunion in one file overwrite generated. Solution: split to multiple files
1 change: 1 addition & 0 deletions example/where_predicate_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ type (
Condition WherePredicate
Then []WherePredicate
X Eq
Y map[string]WherePredicate
}
)
14 changes: 10 additions & 4 deletions infer_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,16 @@ func (f *InferredInfo) Visit(n ast.Node) ast.Visitor {
}

isList := false
isMap := false

var id *ast.Ident
var ok bool
if arr, isArr := field.Type.(*ast.ArrayType); isArr {
if arr, okArr := field.Type.(*ast.ArrayType); okArr {
id, ok = arr.Elt.(*ast.Ident)
isList = true
} else if m, okMap := field.Type.(*ast.MapType); okMap {
id, ok = m.Value.(*ast.Ident)
isMap = true
} else {
id, ok = field.Type.(*ast.Ident)
}
Expand All @@ -69,11 +73,13 @@ func (f *InferredInfo) Visit(n ast.Node) ast.Visitor {
continue
}

for _, n := range field.Names {
for _, ff := range field.Names {
fieldTypeName := fieldName
branch := Branching{Lit: PtrStr(n.Name)}
branch := Branching{Lit: PtrStr(ff.Name)}
if isList {
branch = Branching{List: PtrStr(n.Name)}
branch = Branching{List: PtrStr(ff.Name)}
} else if isMap {
branch = Branching{Map: PtrStr(ff.Name)}
}
f.Types[f.currentType][fieldTypeName] = append(f.Types[f.currentType][fieldTypeName], branch)
}
Expand Down
1 change: 1 addition & 0 deletions infer_defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func TestExtractInferenceForWherePredicate(t *testing.T) {
"Path": {
{Lit: PtrStr("Condition")},
{List: PtrStr("Then")},
{Map: PtrStr("Y")},
},
},
out.ForVariantType("WherePredicate", []string{"Eq", "And", "Or", "Path"}))
Expand Down
7 changes: 7 additions & 0 deletions reducer_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type variantName = string
type Branching struct {
Lit *string
List *string
Map *string
}

type ReducerGenerator struct {
Expand Down Expand Up @@ -62,6 +63,12 @@ func (d *{{ $name }}DepthFirstVisitor[A]) Visit{{ . }}(v *{{ . }}) any {
return nil
}
}
{{- else if .Map }}
for idx, _ := range v.{{ .Map }} {
if _ = v.{{ .Map }}[idx].Accept(d); d.stop {
return nil
}
}
{{- end -}}
{{- end }}
Expand Down
19 changes: 15 additions & 4 deletions reducer_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ func TestTravers(t *testing.T) {
PackageName: "visitor",
Types: []string{"Branch", "Leaf"},
Branches: map[string][]Branching{
"Branch": {{Lit: PtrStr("L")}, {Lit: PtrStr("R")}},
"Branch": {
{Lit: PtrStr("Lit")},
{List: PtrStr("List")},
{Map: PtrStr("Map")},
},
},
}

Expand Down Expand Up @@ -40,11 +44,18 @@ func (d *TreeDepthFirstVisitor[A]) VisitBranch(v *Branch) any {
if d.stop {
return nil
}
if _ = v.L.Accept(d); d.stop {
if _ = v.Lit.Accept(d); d.stop {
return nil
}
if _ = v.R.Accept(d); d.stop {
return nil
for idx := range v.List {
if _ = v.List[idx].Accept(d); d.stop {
return nil
}
}
for idx, _ := range v.Map {
if _ = v.Map[idx].Accept(d); d.stop {
return nil
}
}
return nil
Expand Down

0 comments on commit 6b6eb3d

Please sign in to comment.