-
Notifications
You must be signed in to change notification settings - Fork 0
[1/2] x/cli/output: Add an abstraction to take care of output rendering #27
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
Changes from all commits
5fd18bb
3d16686
f1c9071
ffc0d06
2581ac1
f69c22d
60446cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,39 @@ | |
|
|
||
| type WalkFunc func(*Field) error | ||
|
|
||
| func Walk(in interface{}, fn WalkFunc) error { | ||
| // Prefixed is an optional interface that a value passed to Walk can | ||
| // implement to inject dynamic key prefix segments. When Walk receives a | ||
| // Prefixed value it builds a synthetic ancestor chain from the prefix | ||
| // segments and walks the inner value returned by WalkValue. | ||
|
Comment on lines
+24
to
+27
Contributor
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. I'll be 100% straight here: I have no idea what this means.
Member
Author
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. checkout ./configurator_test.go it explicit the behavour quite extensively |
||
| type Prefixed interface { | ||
| WalkPrefix() []string | ||
| WalkValue() any | ||
| } | ||
|
|
||
| func Walk(in any, fn WalkFunc) error { | ||
| return walkValue(in, fn, nil) | ||
| } | ||
|
|
||
| func walkValue(in any, fn WalkFunc, ancestor *Field) error { | ||
| if p, ok := in.(Prefixed); ok { | ||
| return walkPrefixed(p, fn, ancestor) | ||
| } | ||
|
|
||
| return walkStruct(in, fn, ancestor) | ||
| } | ||
|
|
||
| func walkPrefixed(p Prefixed, fn WalkFunc, ancestor *Field) error { | ||
| for _, seg := range p.WalkPrefix() { | ||
| ancestor = &Field{ | ||
| Field: reflect.StructField{Name: seg}, | ||
| Ancestor: ancestor, | ||
| } | ||
| } | ||
|
|
||
| return walkValue(p.WalkValue(), fn, ancestor) | ||
| } | ||
|
|
||
| func walkStruct(in any, fn WalkFunc, ancestor *Field) error { | ||
| if in == nil { | ||
| return ErrShouldBeAStructPtr | ||
| } | ||
|
|
@@ -40,7 +72,7 @@ | |
| return ErrShouldBeAStructPtr | ||
| } | ||
|
|
||
| return walk(inv, fn, nil) | ||
| return walk(inv, fn, ancestor) | ||
| } | ||
|
|
||
| func indirectedType(t reflect.Type) reflect.Type { | ||
|
|
@@ -67,6 +99,16 @@ | |
| return v.Addr() | ||
| } | ||
|
|
||
| func walkField(nv reflect.Value, fn WalkFunc, f *Field) error { | ||
| if nv.CanInterface() { | ||
| if p, ok := nv.Interface().(Prefixed); ok { | ||
| return walkPrefixed(p, fn, f) | ||
| } | ||
| } | ||
|
|
||
| return walk(nv, fn, f) | ||
| } | ||
|
|
||
| func walk(v reflect.Value, fn WalkFunc, a *Field) error { | ||
| vit := indirectedType(v.Type()) | ||
|
|
||
|
|
@@ -88,7 +130,7 @@ | |
|
|
||
| if unicode.IsUpper(rune(sf.Name[0])) { | ||
| switch err := fn(&f); err { | ||
| case SkipStruct: | ||
| continue | ||
| case nil: | ||
| default: | ||
|
|
@@ -106,7 +148,7 @@ | |
| nv.Set(reflect.New(sf.Type.Elem())) | ||
| } | ||
|
|
||
| if err := walk(nv, fn, &f); err != nil { | ||
| if err := walkField(nv, fn, &f); err != nil { | ||
| return err | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.