Skip to content

Commit

Permalink
Add complex item
Browse files Browse the repository at this point in the history
  • Loading branch information
sourishkrout committed Jul 15, 2024
1 parent 446fc2b commit 8ca636c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 30 deletions.
2 changes: 1 addition & 1 deletion internal/owl/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,7 @@ func reconcileAsymmetry(store *Store) QueryNodeReducer {
}
}

func reduceSpecsAtomic(ns string, parent *SpecDef) QueryNodeReducer {
func reduceSpecsAtomic(ns string, parent *ComplexDef) QueryNodeReducer {
return func(opSets []*OperationSet, opDef *ast.OperationDefinition, selSet *ast.SelectionSet) (*ast.SelectionSet, error) {
var specKeys []string
varSpecs := make(map[string]*SetVarItem)
Expand Down
67 changes: 40 additions & 27 deletions internal/owl/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ type TagFailedError struct {
varItem *SetVarItem
code ValidateErrorType
tag string
item string
}

func NewTagFailedError(varItem *SetVarItem, tag string) *TagFailedError {
func NewTagFailedError(varItem *SetVarItem, tag string, item string) *TagFailedError {
return &TagFailedError{
varItem: varItem,
code: ValidateErrorTagFailed,
tag: tag,
item: item,
}
}

Expand All @@ -46,11 +48,12 @@ func (e TagFailedError) VarItem() *SetVarItem {
}

func (e TagFailedError) Error() string {
return fmt.Sprintf("Error %v: The value of variable \"%s\" failed tag validation \"%s\" required by \"%s!\" declared in \"%s\"",
return fmt.Sprintf("Error %v: The value of variable \"%s\" failed tag validation \"%s\" required by \"%s->%s\" declared in \"%s\"",
e.Code(),
e.Key(),
e.Tag(),
e.SpecName(),
e.Item(),
e.Source())
}

Expand Down Expand Up @@ -78,6 +81,10 @@ func (e TagFailedError) SpecName() string {
return e.varItem.Spec.Name
}

func (e TagFailedError) Item() string {
return e.item
}

func (e TagFailedError) Source() string {
if e.varItem.Spec.Operation == nil {
return "-"
Expand All @@ -96,15 +103,15 @@ var (

const ComplexSpecType string = "Complex"

type SpecDef struct {
type ComplexDef struct {
Name string
Breaker string
Items map[string]*varSpec
}

var validator = valid.New()

var ComplexDefTypes = map[string]*SpecDef{
var ComplexDefTypes = map[string]*ComplexDef{
"Redis": {
Name: "Redis",
Breaker: "REDIS",
Expand Down Expand Up @@ -140,8 +147,8 @@ var ComplexDefTypes = map[string]*SpecDef{
}

func (s *ComplexOperationSet) validate() (ValidationErrors, error) {
data := make(map[string]interface{})
rules := make(map[string]interface{})
var validationErrs ValidationErrors

for _, k := range s.Keys {
spec, ok := s.specs[k]
if !ok {
Expand All @@ -167,28 +174,34 @@ func (s *ComplexOperationSet) validate() (ValidationErrors, error) {
return nil, fmt.Errorf("invalid key not matching complex item: %s", val.Var.Key)
}

typkey := (parts[len(parts)-1])
itemKey := (parts[len(parts)-1])
data := make(map[string]interface{}, 1)
rules := make(map[string]interface{}, 1)
data[val.Var.Key] = val.Value.Resolved
rules[val.Var.Key] = typ.Items[typkey].Rules
}

fields := validator.ValidateMap(data, rules)
var validationErrs ValidationErrors

for key, errs := range fields {
verrs, ok := errs.(valid.ValidationErrors)
if !ok {
return nil, fmt.Errorf("unexpected error type: %T", errs)
}
for _, err := range verrs {
val := s.values[key]
spec := s.specs[key]
validationErrs = append(validationErrs,
NewTagFailedError(
&SetVarItem{Var: val.Var, Value: val.Value, Spec: spec.Spec},
err.Tag(),
),
)
rules[val.Var.Key] = typ.Items[itemKey].Rules

field := validator.ValidateMap(data, rules)

for key, errs := range field {
verrs, ok := errs.(valid.ValidationErrors)
if !ok {
return nil, fmt.Errorf("unexpected error type: %T", errs)
}
for _, err := range verrs {
val := s.values[key]
spec := s.specs[key]
validationErrs = append(validationErrs,
NewTagFailedError(
&SetVarItem{
Var: val.Var,
Value: val.Value,
Spec: spec.Spec,
},
err.Tag(),
itemKey,
),
)
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions internal/owl/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ func Test_Store_ComplexSpecs(t *testing.T) {
assert.EqualValues(t, "REDIS_HOST", snapshot[4].Var.Key)
assert.EqualValues(t, "12345", snapshot[4].Value.Resolved)
assert.EqualValues(t,
`Error 1: The value of variable "REDIS_HOST" failed tag validation "ip|hostname" required by "Redis!" declared in ".env.example"`,
`Error 1: The value of variable "REDIS_HOST" failed tag validation "ip|hostname" required by "Redis->HOST" declared in ".env.example"`,
snapshot[4].Errors[0].Message,
)

assert.EqualValues(t, "REDIS_PORT", snapshot[5].Var.Key)
assert.EqualValues(t, "invalid-port", snapshot[5].Value.Resolved)
assert.EqualValues(t,
`Error 1: The value of variable "REDIS_PORT" failed tag validation "number" required by "Redis!" declared in ".env.example"`,
`Error 1: The value of variable "REDIS_PORT" failed tag validation "number" required by "Redis->PORT" declared in ".env.example"`,
snapshot[5].Errors[0].Message,
)
})
Expand Down

0 comments on commit 8ca636c

Please sign in to comment.