-
Notifications
You must be signed in to change notification settings - Fork 2
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
AliasMangler #95
AliasMangler #95
Conversation
047e3d9
to
0ff99d6
Compare
5d5cfaa
to
40b8af1
Compare
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.
a few comments
ez/ez_test.go
Outdated
envErr := os.Setenv("ALTCONFIGPATH", "../testhelper/testconfig.yaml") | ||
require.NoError(t, envErr) | ||
defer os.Unsetenv("ALTCONFIGPATH") |
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.
you can use t.SetEnv
here. (available since go 1.17)
README.md
Outdated
and `dialspflag` as well by appending "alias" to the flag name. The `ez` | ||
package also wraps the appropriate file source so config files can also make use | ||
of aliases. |
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.
and `dialspflag` as well by appending "alias" to the flag name. The `ez` | |
package also wraps the appropriate file source so config files can also make use | |
of aliases. | |
and `dialspflag` as well by appending "alias" to the flag name. The `ez` | |
package also wraps the appropriate file source's decoder so config files can also make use | |
of aliases. |
Dials supports aliases for fields where you want to change the name and support | ||
an orderly transition from an old name to a new name. Just as there is a | ||
`dials` struct tag there is also a `dialsalias` struct tag that you can use as | ||
an alternate name. Any other casing or transformation rules on the original |
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 think we should mention that this functionality is provided by a mangler upfront, so anyone who doesn't use the ez
package isn't confused when it doesn't work out of the box.
(I think there's a section in the README that you can link to that describes manglers -- we should add on if there isn't)
// If file-watching is not enabled, we should shutdown the monitor | ||
// goroutine when exiting this function. | ||
// Usually `dials.Config` is smart enough not to start a monitor when | ||
// there are no `Watcher` implementations in the source-list, but the | ||
// `Blank` source uses `Watcher` for its core functionality, so we need | ||
// to shutdown the blank source to actually clean up resources. | ||
if !params.WatchConfigFile { | ||
defer blank.Done(ctx) | ||
} | ||
|
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.
Can we split this (unrelated) bug-fix into a different commit? (it can be the same PR, but it's super-confusing to have a move that's fixing a bug in the middle of a commit introducing a new feature)
ez/ez.go
Outdated
@@ -219,7 +219,8 @@ func ConfigFileEnvFlagDecoderFactoryParams[T any, TP ConfigWithConfigPath[T]](ct | |||
return nil, fmt.Errorf("decoderFactory provided a nil decoder for path: %s", cfgPath) | |||
} | |||
|
|||
manglers := make([]transform.Mangler, 0, 2) | |||
manglers := make([]transform.Mangler, 0, 3) | |||
manglers = append(manglers, &transform.AliasMangler{}) |
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.
nit: drop the &
.
AliasMangler
is a zero-size struct, and doesn't need an address.
The go runtime/compiler will dedup zero-size-typed addresses in a lot of cases, including interface assignments. Storing the pointer type does us zero good here.
transform/alias_mangler.go
Outdated
// conveniently. | ||
type AliasMangler struct{} | ||
|
||
// Mangle implements the Mangler interface. If an alias tag is defined, the |
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.
nit: s/an/any/
. (an
is a little ambiguous in the case where multiple tags are set)
transform/alias_mangler.go
Outdated
Name: desc.Name + " (alias of `" + originalVals[tag] + "`)", | ||
} | ||
if setErr := tags.Set(newDesc); setErr != nil { | ||
return nil, fmt.Errorf("error setting amended dialsdesc for tag %q: %w", tag, setErr) |
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 hate it when people try to make APIs too "safe".
The only existing case where tags.Set
returns an error is when the Key
field is empty .
I wonder how much code we'd be able to delete if we used a library that paniced when you do something that stupid instead of forcing everyone to handle an error that cannot happen in any case where you have the least bit of control.
transform/alias_mangler.go
Outdated
// update dialsdesc if there is one | ||
if desc, getErr := tags.Get("dialsdesc"); getErr == nil { | ||
newDesc := &structtag.Tag{ | ||
Key: "dialsdesc", | ||
Name: desc.Name + " (alias of `" + originalVals[tag] + "`)", | ||
} | ||
if setErr := tags.Set(newDesc); setErr != nil { |
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.
Hmm, this dialsdesc
handling should probably be outside the tag loop. I think as-is, it'll clobber the tag multiple times if several of the alias tags are set at once. (which I think we want to allow)
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.
As discussed on Slack, I need to leave some accumulation in the loop and then concatenate all the options for the dialsdesc
tag afterwards.
transform/alias_mangler.go
Outdated
} | ||
} | ||
|
||
// set the new flags on the alias field |
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.
nit: s/flags/tags/
// also don't recurse into TextUnarshaler types | ||
if ft.Implements(textMReflectType) || reflect.PointerTo(ft).Implements(textMReflectType) { | ||
continue | ||
} |
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.
We should split this into a separate commit.
40b8af1
to
f7a1949
Compare
This fixes a bug in ez where an error thrown by a Source may cause the process to hang.
We shouldn't ever mangle types that implement TextUnmarshaler.
55f2b40
to
8e387cb
Compare
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.
one nit
README.md
Outdated
|
||
Aliases are implemented using the | ||
[Mangler](https://github.com/vimeo/dials/blob/402b4821e3f191d96580b9933583f1651325381d/transform/transformer.go#L17-L32) | ||
interface. The `env`, `flag`, and `pflag` support aliasing without any |
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.
nit:
interface. The `env`, `flag`, and `pflag` support aliasing without any | |
interface. The `env`, `flag`, and `pflag` sources support aliasing without any |
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.
Good catch. Fixed.
Add a new mangler that allows us to have aliases for dials tags to facilitate migrating from one name to another seamlessly. If both the old name and the new name are set, an error will be returned because the expectation is that you're using one or the other exclusively.
8e387cb
to
57e6f14
Compare
AliasMangler
Add a new mangler that allows us to have aliases for dials tags to
facilitate migrating from one name to another seamlessly. If both the
old name and the new name are set, an error will be returned because the
expectation is that you're using one or the other exclusively.
Also, fix a bug in
ez
where an error thrown by a Source may cause theprocess to hang.
And, lastly, specifically exclude TextUnmarshaler implementing types
from mangling.