-
Notifications
You must be signed in to change notification settings - Fork 66
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
Add support for Transforms that map fields in the Type Converter #116
Conversation
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.
partial comments, more review to come.
codegen/type_converter_test.go
Outdated
program.Types[toStruct].(*compile.StructSpec).Fields, | ||
"") | ||
|
||
fmt.Printf("%v", overrideMap) |
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.
nitpick: remove dead print.
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.
Done
codegen/type_converter.go
Outdated
// converting and overriding fields. | ||
type FieldMapperEntry struct { | ||
QualifiedName string | ||
Field *compile.FieldSpec |
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.
comment about whether this is source or destination FieldSpec ?
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 do not understand the use case for this "Override" feature. it really complicates the code generation and the generated code.
It might benefit to add tests that describe intent instead of just "one two three".
codegen/type_converter.go
Outdated
} | ||
|
||
// TODO: Verify all intermediate objects are not nil | ||
c.append(indent, toIdentifier, " = ", typeName, "(", overriddenIdentifier, ")") |
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.
Why do we assign from both fields ?
It looks like a transform should only support the transformed field not the original 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.
Can we have a markdown document for all possible permutations ?
toFieldReqOpt, originalFromFieldReqOpt, overriddenFromFieldReqOpt, overrideBool.
There are 16 permutations and we should state : "use orig field" , "use overriden field" , "try overridden then orig" , "try orig then overridden" or "invalid permutation, junk semantics"
Once we agree these 16 permutations make since from a documentation perspective we can then verify we have tests for 16 cases and that the code is correct.
QualifiedName: "Two", | ||
Override: true, | ||
} // Map from required filed, unconditional assignment | ||
fieldMap["Two"] = codegen.FieldMapperEntry{ |
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.
It's confusing to set "override: true" but still have the field come from in.Two
instead of in.One
Maybe we should make both fields required if override is true and fail the code generation if one of them is optional.
QualifiedName: "One", | ||
Override: true, | ||
} | ||
fieldMap["Three"] = codegen.FieldMapperEntry{ |
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.
Override true should preferablly not read from in.Three
} | ||
fieldMap["Four"] = codegen.FieldMapperEntry{ | ||
QualifiedName: "Three", | ||
Override: false, |
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.
Very sneaky but correct.
assert.NoError(t, err) | ||
assert.Equal(t, trim(` | ||
out.One = (*bool)(in.Two) | ||
if in.One != 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.
Might be cleaner to generate an if ... {} else {}
to reduce confusion.
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.
codegen/type_converter_test.go
Outdated
if in.Four != nil { | ||
out.Four = &structs.NestedBar{} | ||
out.Four.One = string(in.Four.One) | ||
out.Four.Two = (*string)(in.Four.One) |
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.
cannot cast string to *string
generated code does not work.
Need to use ptr.String(in.Four.One)
to cast from required to optional.
841273c
to
f64e17d
Compare
d81a300
to
e2c91b8
Compare
e2c91b8
to
b06db68
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.
I'll do a little cleanup and then merge
glide.yaml
Outdated
@@ -47,3 +47,4 @@ import: | |||
version: 5b691c8ebc4af5191baa426561d62f1b5115d6e5 | |||
- package: github.com/stretchr/testify | |||
version: f6abca593680b2315d2075e0f5e2a9751e3f431a | |||
- package: github.com/wacul/ptr |
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.
lets use the version of ptr
that comes with thriftrw if we can.
glide.lock
Outdated
@@ -21,12 +21,24 @@ imports: | |||
version: 3e4f037f12a1221a0864cf0dd2e81c452ab22448 | |||
- name: github.com/go-yaml/yaml | |||
version: a83829b6f1293c91addabc89d0571c246397bbf4 | |||
- name: github.com/golang/lint |
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.
will need to regenerate with all this extra development only deps removed.
Not sure why glide suddenly added everything :/
Generates code that allows fields of the same type to be mapped to one another based on a provided mapping of field names. Performs overwritting if provided flag is set.
Remaining work for future PRs: