-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Validate zero modules #324
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,16 @@ | ||
package moduleconfig | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"reflect" | ||
"strings" | ||
|
||
yaml "gopkg.in/yaml.v2" | ||
|
||
"github.com/commitdev/zero/pkg/util/flog" | ||
"github.com/iancoleman/strcase" | ||
) | ||
|
||
type ModuleConfig struct { | ||
|
@@ -48,16 +55,109 @@ type TemplateConfig struct { | |
OutputDir string `yaml:"outputDir"` | ||
} | ||
|
||
// A "nice" wrapper around findMissing() | ||
func (cfg ModuleConfig) collectMissing() []string { | ||
var missing []string | ||
findMissing(reflect.ValueOf(cfg), "", "", &missing) | ||
|
||
return missing | ||
} | ||
|
||
func LoadModuleConfig(filePath string) (ModuleConfig, error) { | ||
config := ModuleConfig{} | ||
|
||
var required = []string{} | ||
fmt.Printf("%+#v\n", required) | ||
|
||
data, err := ioutil.ReadFile(filePath) | ||
if err != nil { | ||
return config, err | ||
} | ||
|
||
err = yaml.Unmarshal(data, &config) | ||
if err != nil { | ||
return config, err | ||
} | ||
|
||
missing := config.collectMissing() | ||
if len(missing) > 0 { | ||
flog.Errorf("%v is missing information", filePath) | ||
|
||
for _, m := range missing { | ||
flog.Errorf("\t %v\n", m) | ||
} | ||
|
||
log.Fatal("") | ||
} | ||
|
||
return config, nil | ||
} | ||
|
||
// Recurses through a datastructure to find any missing data. | ||
// This assumes several things: | ||
// 1. The structure matches that defined by ModuleConfig and its child datastructures. | ||
// 2. YAML struct field metadata is sufficient to define whether an attribute is missing or not. | ||
// That is, "yaml:foo,omitempty" tells us this is not a required field because we can omit it. | ||
// 3. Slices and arrays are assumed to be optional. | ||
// | ||
// As this function recurses through the datastructure, it builds up a string | ||
// path representing each node's path within the datastructure. | ||
// If the value of the current node is equal to the zero value for its datatype | ||
// and its struct field does *not* have a "omitempty" value, then we assume it | ||
// is missing and add it to the resultset. | ||
func findMissing(obj reflect.Value, path, metadata string, missing *[]string) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🕶️ 👍 |
||
t := obj.Type() | ||
switch t.Kind() { | ||
case reflect.String: | ||
if obj.String() == "" && !strings.Contains(metadata, "omitempty") { | ||
*missing = append(*missing, path) | ||
} | ||
|
||
case reflect.Slice, reflect.Array: | ||
for i := 0; i < obj.Len(); i++ { | ||
prefix := fmt.Sprintf("%v[%v]", path, i) | ||
findMissing(obj.Index(i), prefix, metadata, missing) | ||
} | ||
|
||
case reflect.Struct: | ||
for i := 0; i < t.NumField(); i++ { | ||
fieldType := t.Field(i) | ||
fieldTags, _ := fieldType.Tag.Lookup("yaml") | ||
fieldVal := obj.Field(i) | ||
|
||
tags := strings.Split(fieldTags, ",") | ||
|
||
hasOmitEmpty := false | ||
// We have all metadata yaml tags, now let's remove the "omitempty" tag if | ||
// it is present. | ||
// Then if we have only one tag remaining, this must be the expected yaml | ||
// identifer. | ||
// Otherwise the name of the yaml identifier should match the struct | ||
// attribute name. | ||
for i := len(tags) - 1; i >= 0; i-- { | ||
tag := tags[i] | ||
if tag == "omitempty" { | ||
hasOmitEmpty = true | ||
tags = append(tags[:i], tags[i+1:]...) | ||
} | ||
} | ||
|
||
yamlName := strcase.ToLowerCamel(fieldType.Name) | ||
if len(tags) == 1 && tags[0] != "" { // For some reason, empty tag lists are giving a count of 1. | ||
yamlName = tags[0] | ||
} | ||
|
||
prefix := yamlName | ||
if path != "" { | ||
prefix = fmt.Sprintf("%v.%v", path, yamlName) | ||
} | ||
|
||
zeroVal := reflect.Zero(fieldType.Type) | ||
if fieldVal == zeroVal && !hasOmitEmpty { | ||
*missing = append(*missing, prefix) | ||
} | ||
|
||
findMissing(fieldVal, prefix, fieldTags, missing) | ||
} | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume this is not needed anymore? since we're treating non-omitempty as required
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, for sure! Thanks.