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

Generate req/resp converters with nested structs #97

Merged
merged 23 commits into from
May 18, 2017

Conversation

Raynos
Copy link
Contributor

@Raynos Raynos commented May 12, 2017

This PR improves our existing request / response converter
functions to copy over nested structs.

I've significantly expanded and refactor the approach for
generating these and moved logic into a seperate file.

There is more work still to do be done that will come in a
seperate PR, for example lists, maps, sets, enums, etc.

r: @uber/zanzibar-team


// ConvertFields will add lines to the TypeConverter for mapping
// from one go struct to another based on two thriftrw.FieldGroups
func (c *TypeConverter) ConvertFields(
Copy link
Contributor

Choose a reason for hiding this comment

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

without looking at the code, to me the method signature (name and parameters) reads like converting fields between the two, while it actually generates code for conversion. IMO, func GenConversionCode(keyPrefix string, from []*compile.FieldSpec, to []*compile.FieldSpec, helper *packageHelper) ([]string, error) is a more intuitive signature.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

makes sense.

// TypeConverter can generate a function body that converts two thriftrw
// FieldGroups from one to another. It's assumed that the converted code
// operates on two variables, "in" and "out" and that both are a go struct.
type TypeConverter struct {
Copy link
Contributor

@ChuntaoLu ChuntaoLu May 12, 2017

Choose a reason for hiding this comment

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

Is a converter struct necessary? feels like the convertFields method can be a standalone function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

a standalone function passes too many args around so it put it on an object.

line = "}"
c.Lines = append(c.Lines, line)

default:
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe default should return error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It should once we support other types, implementation is partial atm.

}

return nil
}
Copy link
Contributor

@ChuntaoLu ChuntaoLu May 12, 2017

Choose a reason for hiding this comment

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

Unit tests on this would be nice.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

😭

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wrote ALL the unit tests.

@coveralls
Copy link

coveralls commented May 12, 2017

Coverage Status

Coverage increased (+0.2%) to 69.184% when pulling 8c17060 on convert-nested-bodies into a89ab21 on master.

end=`date +%s`
runtime=$((end-start))
echo "Generated easy_json files for endpoints +$runtime"
# TODO : Add new post steps :)
Copy link
Contributor

Choose a reason for hiding this comment

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

mind expanding the comment to list what kinds of post steps?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I dont think we have any post steps at the moment. I can delete this file.

RequestTypeMap map[string]string

// Statements for converting request types
ConvertRequestLines []string
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe change "Lines" to "GolangStatements" or something that makes it clear that these are lines of go code and the go code here is being build ahead of time by the convert function and not by the templates.

Copy link
Contributor

Choose a reason for hiding this comment

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

also, we need some tests on the type converter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tests. 😭

@Raynos
Copy link
Contributor Author

Raynos commented May 18, 2017

I addressed all comments and wrote tests. cc @stevededalus @ChuntaoLu

Copy link
Contributor

@ChuntaoLu ChuntaoLu left a comment

Choose a reason for hiding this comment

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

lgtm

Copy link
Contributor

@stevededalus stevededalus left a comment

Choose a reason for hiding this comment

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

LGTM

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