Skip to content

Commit 36dca14

Browse files
authored
refactor tables check generate code (#90) (#91)
* chore: normalize test to != 0 to simplify table condition * refactor: operations table * refactor: condition table * chore: type CountValues moved to common package * refact: operations list moved to operations package * chore: constants in camelCase (not pascal_case) * refactor: simplifies operations methods
1 parent 0c462a0 commit 36dca14

File tree

15 files changed

+1173
-1051
lines changed

15 files changed

+1173
-1051
lines changed

internal/analyzer/analyzer.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"strconv"
66
"strings"
77

8+
"github.com/opencodeco/validgen/internal/analyzer/operations"
89
"github.com/opencodeco/validgen/internal/common"
910
"github.com/opencodeco/validgen/internal/parser"
1011
"github.com/opencodeco/validgen/types"
@@ -84,12 +85,14 @@ func checkForInvalidOperations(structs []*Struct) error {
8485
structsWithValidation[common.KeyPath(st.PackageName, st.StructName)] = true
8586
}
8687

88+
ops := operations.New()
89+
8790
for _, st := range structs {
8891
for i, fd := range st.Fields {
8992
for _, val := range st.FieldsValidations[i].Validations {
9093
// Check if is a valid operation.
9194
op := val.Operation
92-
if operations[op].Name == "" {
95+
if !ops.IsValid(op) {
9396
return types.NewValidationError("unsupported operation %s", op)
9497
}
9598

@@ -105,7 +108,7 @@ func checkForInvalidOperations(structs []*Struct) error {
105108
}
106109

107110
// Check if is a valid operation for this type.
108-
if !operations[op].ValidTypes[fdType.ToNormalizedString()] {
111+
if !ops.IsValidByType(op, fdType.ToNormalizedString()) {
109112
return types.NewValidationError("operation %s: invalid %s type", op, fdType.BaseType)
110113
}
111114
}
@@ -125,12 +128,14 @@ func analyzeFieldOperations(structs []*Struct) error {
125128
}
126129
}
127130

131+
ops := operations.New()
132+
128133
for _, st := range structs {
129134
for i, fd := range st.Fields {
130135
for _, val := range st.FieldsValidations[i].Validations {
131136
// Check if is a field operation.
132137
op := val.Operation
133-
if !operations[op].IsFieldOperation {
138+
if !ops.IsFieldOperation(op) {
134139
continue
135140
}
136141

internal/analyzer/operations.go

Lines changed: 0 additions & 236 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package operations
2+
3+
import (
4+
"slices"
5+
6+
"github.com/opencodeco/validgen/internal/common"
7+
)
8+
9+
type Operation struct {
10+
CountValues common.CountValues
11+
IsFieldOperation bool
12+
ValidTypes []string
13+
}
14+
15+
type Operations struct {
16+
operations map[string]Operation
17+
}
18+
19+
func New() *Operations {
20+
return &Operations{
21+
operations: operationsList,
22+
}
23+
}
24+
25+
func (o *Operations) IsValid(op string) bool {
26+
_, ok := o.operations[op]
27+
28+
return ok
29+
}
30+
31+
func (o *Operations) IsValidByType(op, fieldType string) bool {
32+
return slices.Contains(o.operations[op].ValidTypes, fieldType)
33+
}
34+
35+
func (o *Operations) IsFieldOperation(op string) bool {
36+
return o.operations[op].IsFieldOperation
37+
}
38+
39+
func (o *Operations) ArgsCount(op string) common.CountValues {
40+
return o.operations[op].CountValues
41+
}

0 commit comments

Comments
 (0)