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

Support custom object types #1674

Merged
merged 3 commits into from
Feb 6, 2024
Merged

Conversation

iwahbe
Copy link
Member

@iwahbe iwahbe commented Feb 5, 2024

Fixes #1673 and unblocks pulumi/pulumi-aws#3378.

This is necessary to support custom object types in the PF.
@iwahbe iwahbe self-assigned this Feb 5, 2024
@iwahbe iwahbe force-pushed the iwahbe/support-custom-object-types branch from 6a1bb31 to 43f2eed Compare February 5, 2024 16:22
Comment on lines +129 to +130
if t, err := r.obj.ApplyTerraform5AttributePathStep(tftypes.AttributeName(key)); err == nil {
typ, ok := t.(attr.Type)
Copy link
Member Author

Choose a reason for hiding this comment

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

As I understand it r.obj.ApplyTerraform5AttributePathStep(tftypes.AttributeName(key)) represents a .key access in the same space value space (attr.Type) as r.obj.

By working within attr.Type (instead of down-casting to types.ObjectType, we get support for custom implementations of basetypes.ObjectTypeable.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah this seems right.

@t0yv0
Copy link
Member

t0yv0 commented Feb 5, 2024

This fix covers tfgen, but I wonder if we have some similar incompleteness at runtime. Did you get a chance to provision this at runtime to see if that works out as expected? Much appreciated.

@iwahbe
Copy link
Member Author

iwahbe commented Feb 5, 2024

This fix covers tfgen, but I wonder if we have some similar incompleteness at runtime. Did you get a chance to provision this at runtime to see if that works out as expected? Much appreciated.

I didn't take the time to provision this and check that it works as expected, but I believe that it will. Unlike GetOk, which works in attr.Type, our runtime encoders use tftypes.Type, which corresponds to TF's runtime representation:

type objectEncoder struct {
objectType tftypes.Object

We should be immune to this kind of error here.

I have checked and we don't have any usage of types.ObjectType in our codebase (outside of tests). The regex [^(tf)(base)]types.Object shows no non-test instances. I'll open a follow-up issue to remove all types.${KIND}Type in favor of basetypes.${KIND}Typeable.


// --- custom object machinery ---
//
// copied from terraform-provider-aws/internal/framework/types/objectof.go
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it reasonable to use the TF AWS implementation of custom object as a general test support machinery here? unless I'm misunderstanding something, this pattern is specific to AWS custom objects, isn't it?

I wonder if it would be possible (and not awfully impractical) to pull this in as a dependency instead, rather than copying?

Copy link
Member

Choose a reason for hiding this comment

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

I think it is.

return objectTypeOf[T]{basetypes.ObjectType{AttrTypes: AttributeTypesMust[T](ctx)}}
}

func (t objectTypeOf[T]) Equal(o attr.Type) bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

super nitty readability nit: we call objectTypeOf[T] -> t and we also call *testing.T -> t. these are both reasonable conventions; they just clash here.

Maybe this machinery code can live in a different file, or we can got with typ as a variable name for the former.

@@ -57,7 +58,7 @@ func (*typeSchema) Sensitive() bool { return false }

func (s *typeSchema) Elem() interface{} {
switch tt := s.t.(type) {
case types.ObjectType:
case basetypes.ObjectTypable:
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this get rid of ObjectType entirely, in favor of the more flexible basetypes implementation?

Copy link
Contributor

Choose a reason for hiding this comment

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

ah, rereading your comment: yes, it is meant to do that, and we should follow up with other types in the same manner.

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.

Support custom object types
3 participants