-
Notifications
You must be signed in to change notification settings - Fork 32
K8SPS-567: automatic yaml generation #1136
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
Open
pooknull
wants to merge
16
commits into
main
Choose a base branch
from
K8SPS-567
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,325
−2,351
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
88305af
K8SPS-567: automatic cr.yaml generation
pooknull afa24ce
Update cmd/example-gen/defaults/preset.go
pooknull a4d6c96
fix commenting
pooknull a576772
small fixes
pooknull 48ef7b8
reduce size of `manual.go`
pooknull 75a86cb
generation of backups and restores
pooknull 31368ed
fix shellcheck
pooknull 231116d
fix shellcheck
pooknull de4116b
fix shellcheck
pooknull 168ea42
fix shellcheck
pooknull 918120e
fix shellcheck
pooknull 708e8ff
Merge branch 'main' into K8SPS-567
pooknull ed6a0a0
check
pooknull 3c20bca
Revert "check"
pooknull 0df4642
Merge branch 'main' into K8SPS-567
pooknull 6e5b3f5
Merge remote-tracking branch 'origin/main' into K8SPS-567
pooknull 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 hidden or 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 hidden or 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 hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,147 @@ | ||
| package fill | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "reflect" | ||
| ) | ||
|
|
||
| // ByType recursively fills zero or nil fields in the given pointer value `v` | ||
| // using provided preset values matched by type. It modifies `v` in place. | ||
| func ByType(v any, presets ...any) error { | ||
| rv := reflect.ValueOf(v) | ||
| if !rv.IsValid() { | ||
| return fmt.Errorf("nil value") | ||
| } | ||
| if rv.Kind() != reflect.Pointer || rv.IsNil() { | ||
| return fmt.Errorf("value must be a non-nil pointer") | ||
| } | ||
| fillValue(rv, buildRules(presets...)) | ||
| return nil | ||
| } | ||
|
|
||
| type rules map[reflect.Type]reflect.Value | ||
|
|
||
| func buildRules(presets ...any) rules { | ||
| r := make(rules, len(presets)) | ||
| for _, p := range presets { | ||
| if p == nil { | ||
| continue | ||
| } | ||
| v := reflect.ValueOf(p) | ||
| r[v.Type()] = v | ||
| } | ||
| return r | ||
| } | ||
|
|
||
| func fillValue(v reflect.Value, r rules) { | ||
| for v.Kind() == reflect.Pointer { | ||
| if v.IsNil() { | ||
| if ok, def := lookup(v.Type(), r); ok { | ||
| v.Set(def) | ||
| } else { | ||
| return | ||
| } | ||
| } | ||
| v = v.Elem() | ||
| } | ||
|
|
||
| switch v.Kind() { | ||
| case reflect.Struct: | ||
| t := v.Type() | ||
| for i := 0; i < v.NumField(); i++ { | ||
| sf := t.Field(i) | ||
| if sf.PkgPath != "" { // not exported | ||
| continue | ||
| } | ||
| fv := v.Field(i) | ||
|
|
||
| setIfZero(fv, r) | ||
|
|
||
| switch fv.Kind() { | ||
| case reflect.Pointer, reflect.Struct, reflect.Slice, reflect.Map, reflect.Interface: | ||
| fillValue(fv, r) | ||
| } | ||
| } | ||
|
|
||
| case reflect.Slice: | ||
| for i := 0; i < v.Len(); i++ { | ||
| fillValue(v.Index(i), r) | ||
| } | ||
|
|
||
| case reflect.Map: | ||
| iter := v.MapRange() | ||
| for iter.Next() { | ||
| k, val := iter.Key(), iter.Value() | ||
|
|
||
| elem := reflect.New(v.Type().Elem()).Elem() | ||
| elem.Set(val) | ||
|
|
||
| setIfZero(elem, r) | ||
|
|
||
| switch elem.Kind() { | ||
| case reflect.Pointer, reflect.Struct, reflect.Slice, reflect.Map, reflect.Interface: | ||
| fillValue(elem, r) | ||
| } | ||
| v.SetMapIndex(k, elem) | ||
| } | ||
|
|
||
| case reflect.Interface: | ||
| if !v.IsNil() { | ||
| cur := v.Elem() | ||
| tmp := reflect.New(cur.Type()).Elem() | ||
| tmp.Set(cur) | ||
| fillValue(tmp, r) | ||
| v.Set(tmp) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func setIfZero(fv reflect.Value, r rules) { | ||
| need := false | ||
| switch fv.Kind() { | ||
| case reflect.Pointer, reflect.Map, reflect.Slice, reflect.Interface: | ||
| need = fv.IsNil() | ||
| default: | ||
| need = fv.IsZero() | ||
| } | ||
| if !need { | ||
| return | ||
| } | ||
| if ok, def := lookup(fv.Type(), r); ok { | ||
| fv.Set(def) | ||
| return | ||
| } | ||
| } | ||
|
|
||
| func lookup(t reflect.Type, r rules) (bool, reflect.Value) { | ||
| if dv, ok := r[t]; ok { | ||
| if dv.Type().AssignableTo(t) { | ||
| return true, dv | ||
| } | ||
| if dv.Type().ConvertibleTo(t) { | ||
| return true, dv.Convert(t) | ||
| } | ||
| } | ||
|
|
||
| if t.Kind() == reflect.Pointer { | ||
| elem := t.Elem() | ||
| if dv, ok := r[elem]; ok { | ||
| ptr := reflect.New(elem) | ||
| if dv.Type().AssignableTo(elem) { | ||
| ptr.Elem().Set(dv) | ||
| return true, ptr | ||
| } | ||
| if dv.Type().ConvertibleTo(elem) { | ||
| ptr.Elem().Set(dv.Convert(elem)) | ||
| return true, ptr | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if t.Kind() != reflect.Interface { | ||
| if dv, ok := r[reflect.PointerTo(t)]; ok && dv.Kind() == reflect.Pointer && !dv.IsNil() && dv.Type().Elem().AssignableTo(t) { | ||
| return true, dv.Elem() | ||
| } | ||
| } | ||
| return false, reflect.Value{} | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.