Skip to content
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

Merged
merged 28 commits into from
Aug 8, 2017

Conversation

stevededalus
Copy link
Contributor

@stevededalus stevededalus commented Jun 1, 2017

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:

  • Wire transform configs from a middleware config file in the the type map
  • Add nil checks for deeply nested structs (that can contain required fields nested in optional fields)
  • Add camelCasing and other transforms
  • Ability to remove mapped field
  • Support mapping optional -> required using zero-values and pointer dereferencing

@stevededalus stevededalus requested a review from Raynos June 1, 2017 02:06
@coveralls
Copy link

coveralls commented Jun 1, 2017

Coverage Status

Coverage increased (+0.6%) to 75.326% when pulling 4dde80c on transforms into 3154c35 on master.

Copy link
Contributor

@Raynos Raynos left a 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.

program.Types[toStruct].(*compile.StructSpec).Fields,
"")

fmt.Printf("%v", overrideMap)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: remove dead print.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

// converting and overriding fields.
type FieldMapperEntry struct {
QualifiedName string
Field *compile.FieldSpec
Copy link
Contributor

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 ?

@coveralls
Copy link

coveralls commented Jun 12, 2017

Coverage Status

Coverage increased (+0.7%) to 75.397% when pulling c54fc89 on transforms into 3154c35 on master.

Copy link
Contributor

@Raynos Raynos left a 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".

}

// TODO: Verify all intermediate objects are not nil
c.append(indent, toIdentifier, " = ", typeName, "(", overriddenIdentifier, ")")
Copy link
Contributor

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.

Copy link
Contributor

@Raynos Raynos left a 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{
Copy link
Contributor

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{
Copy link
Contributor

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,
Copy link
Contributor

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 {
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if in.Four != nil {
out.Four = &structs.NestedBar{}
out.Four.One = string(in.Four.One)
out.Four.Two = (*string)(in.Four.One)
Copy link
Contributor

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.

@coveralls
Copy link

coveralls commented Jul 18, 2017

Coverage Status

Coverage increased (+0.3%) to 77.955% when pulling a27fb97 on transforms into 342e355 on master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.2%) to 76.406% when pulling f64e17d on transforms into eaf12b8 on master.

@coveralls
Copy link

coveralls commented Aug 3, 2017

Coverage Status

Coverage increased (+0.5%) to 76.704% when pulling 5a6027b on transforms into eaf12b8 on master.

@coveralls
Copy link

coveralls commented Aug 8, 2017

Coverage Status

Coverage increased (+0.006%) to 76.561% when pulling b06db68 on transforms into d85a193 on master.

Copy link
Contributor

@Raynos Raynos left a 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
Copy link
Contributor

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
Copy link
Contributor

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 :/

@coveralls
Copy link

coveralls commented Aug 8, 2017

Coverage Status

Coverage decreased (-0.09%) to 76.461% when pulling db38037 on transforms into d85a193 on master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants