-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Defaulting was implemented nested into the main validation algorithm. This makes the defaulting algorithm much more complex. This commit prototypes an externally implemented trivial looking defaulting algorithm.
- Loading branch information
Showing
6 changed files
with
229 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package validate | ||
|
||
import "fmt" | ||
|
||
func ApplyDefaults(root interface{}, result *Result) { | ||
applyDefaults(root, result) | ||
} | ||
|
||
func applyDefaults(root interface{}, result *Result) { | ||
switch obj := root.(type) { | ||
case map[string]interface{}: | ||
for _, val := range obj { | ||
applyDefaults(val, result) | ||
} | ||
applyObjectDefaults(obj, result) | ||
case []interface{}: | ||
for _, val := range obj { | ||
applyDefaults(val, result) | ||
} | ||
} | ||
} | ||
|
||
func applyObjectDefaults(obj map[string]interface{}, result *Result) { | ||
for fld, schemata := range result.propertySchemata[fmt.Sprintf("%p", obj)] { | ||
if _, ok := obj[fld]; ok { | ||
continue | ||
} | ||
|
||
for _, schema := range schemata { | ||
if schema.Default != nil { | ||
obj[fld] = schema.Default | ||
break | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters