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

Allow property assignments to pull values from functions #1545

Merged
merged 9 commits into from
Jun 14, 2021

Conversation

theunrepentantgeek
Copy link
Member

@theunrepentantgeek theunrepentantgeek commented Jun 2, 2021

What this PR does / why we need it:

Allows generated conversions to read a value from a suitably named source function for writing into a field on the destination type. Having this will allow resolution of #1468 by having a function on an API variant (e.g. v1alpha1api20210601.Person) that is used to populate a field on the associated storage variant (e.g. v1alpha1api20210601storage.Person).

Special notes for your reviewer:

Implementing a generic solution was cleaner than trying to do lots of semi-nasty special casing. Introducing ValueFunction avoided some wide spread changes to existing implementations of Function.

There's also a little tidy up around the golden tests as they were previously using private (lowercase) field names when our generated types actually use public (CapitalCase) naming.

How does this PR make you feel:
gif

If applicable:

  • this PR contains tests

@codecov-commenter
Copy link

codecov-commenter commented Jun 2, 2021

Codecov Report

Merging #1545 (0009450) into master (0338b8d) will increase coverage by 1.41%.
The diff coverage is 79.06%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1545      +/-   ##
==========================================
+ Coverage   62.17%   63.59%   +1.41%     
==========================================
  Files         163      178      +15     
  Lines       10739    11726     +987     
==========================================
+ Hits         6677     7457     +780     
- Misses       3422     3606     +184     
- Partials      640      663      +23     
Impacted Files Coverage Δ
hack/generated/main.go 0.00% <0.00%> (ø)
hack/generated/pkg/armclient/raw_client.go 59.13% <ø> (ø)
hack/generated/pkg/genruntime/base_types.go 96.55% <ø> (ø)
...ted/pkg/reconcilers/azure_deployment_reconciler.go 59.39% <ø> (+1.17%) ⬆️
.../generated/pkg/testcommon/counting_roundtripper.go 100.00% <ø> (ø)
hack/generated/pkg/testcommon/kube_matcher.go 100.00% <ø> (ø)
.../generated/pkg/testcommon/kube_per_test_context.go 79.13% <ø> (ø)
hack/generated/pkg/testcommon/kube_test_context.go 90.00% <ø> (+13.45%) ⬆️
hack/generated/pkg/testcommon/test_context.go 57.60% <ø> (-1.22%) ⬇️
hack/generator/cmd/root.go 0.00% <ø> (ø)
... and 119 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b10a226...0009450. Read the comment docs.

Copy link
Member

@babbageclunk babbageclunk left a comment

Choose a reason for hiding this comment

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

Looks good! Maybe the possibility of reducing some code duplication in the handling of functions vs properties? But not a deal breaker.


// ReturnTypes returns nil as we can't represent byte[] in our type system
func (f *OneOfJSONMarshalFunction) ReturnTypes() []Type {
return nil
Copy link
Member

Choose a reason for hiding this comment

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

Presumably this isn't a problem because ReturnTypes is just used for References?

Copy link
Member Author

Choose a reason for hiding this comment

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

I've removed this method - have added a new interface ValueFunction which is implemented only where needed, eliminating the need to add ReturnTypes() to a bunch of existing functions.

Comment on lines 314 to 377
conv, err = fn.createPropertyConversion(receiverProperty, otherProperty)
default:
panic(fmt.Sprintf("unexpected conversion direction %q", fn.direction))
// Generate a conversion from one property to another
conv, err := fn.createPropertyConversion(sourceProperty, destinationProperty)
if err != nil {
// An error was returned, we abort creating conversions for this object
return errors.Wrapf(
err,
"creating conversion setting property %q from property %q",
destinationProperty.PropertyName(),
sourceProperty.PropertyName())
} else if conv != nil {
// A conversion was created, keep it for later
fn.conversions[destinationName] = conv
}
continue
}

sourceFunction, ok := sourceFunctions[destinationName]
if ok {
// Generate a conversion to assign the property from the function
conv, err := fn.createFunctionConversion(sourceFunction, destinationProperty)
if err != nil {
// An error was returned, we abort creating conversions for this object
return errors.Wrapf(err, "creating conversion for property %q of %q", receiverProperty.PropertyName(), receiver.Name())
return errors.Wrapf(
err,
"creating conversion setting property %q from function %s()",
destinationName,
sourceFunction.Name())
Copy link
Member

Choose a reason for hiding this comment

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

These two chunks (and the createPropertyConversion and createFunctionConversion methods below) are very similar. Would it be feasible to make wrappers for sourceProperty and sourceFunction to make them expose the same interface (where one reads from a selector and the other reads from a callexpr) and then use that in a single createPropertyConversion method?

Copy link
Member Author

Choose a reason for hiding this comment

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

I've created two wrapper end points - ReadableConversionEndpoint and WritableConversionEndpoint that achieve a nice reduction in duplication.

@theunrepentantgeek theunrepentantgeek force-pushed the feature/function-return-types branch 2 times, most recently from a1db005 to ca35fe9 Compare June 14, 2021 01:14
@theunrepentantgeek theunrepentantgeek force-pushed the feature/function-return-types branch from ca35fe9 to ee8ea72 Compare June 14, 2021 01:19
@theunrepentantgeek theunrepentantgeek force-pushed the feature/function-return-types branch from ee8ea72 to e436b52 Compare June 14, 2021 01:21
Copy link
Member

@babbageclunk babbageclunk left a comment

Choose a reason for hiding this comment

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

Nice - I like the readable/writable endpoint split!

Comment on lines +305 to +307
"creating conversion to %s by %s",
sourceEndpoint,
destinationEndpoint)
Copy link
Member

Choose a reason for hiding this comment

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

Are the arguments here the right way around?

Copy link
Member Author

Choose a reason for hiding this comment

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

They need to be swapped; will do that in my next PR.

@theunrepentantgeek theunrepentantgeek merged commit f92bb04 into master Jun 14, 2021
@theunrepentantgeek theunrepentantgeek deleted the feature/function-return-types branch June 14, 2021 02:40
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.

3 participants