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

Go: object type not recognized #2880

Closed
1 task done
njlynch opened this issue Jun 11, 2021 · 3 comments · Fixed by #3485
Closed
1 task done

Go: object type not recognized #2880

njlynch opened this issue Jun 11, 2021 · 3 comments · Fixed by #3485
Assignees
Labels
bug This issue is a bug. language/go Regarding GoLang bindings p1

Comments

@njlynch
Copy link
Contributor

njlynch commented Jun 11, 2021

🐛 Bug Report

Affected Languages

  • Go

General Information

  • JSII Version: 1.30.0 (build adae23f), typescript 3.9.9
  • Platform: OSX (Darwin Kernel Version 19.6.0)

What is the problem?

See aws/aws-cdk#15007.

Attempting to create a CfnFunction_FunctionConfigProperty results in a malformed object that doesn't pass required field checks.

The following should work, but complains that the comment and runtime properties aren't set:

	awscloudfront.NewCfnFunction(stack, jsii.String("myCDKFunction"), &awscloudfront.CfnFunctionProps{
		Name:         jsii.String("myCfnFunctionCdk"),
		FunctionCode: jsii.String(myFunctionCode),
		AutoPublish:  jsii.Bool(true),
		FunctionConfig: &awscloudfront.CfnFunction_FunctionConfigProperty{
			Comment: jsii.String("testcdk"),
			Runtime: jsii.String("cloudfront-js-1.0"),
		},
	})

Verbose Log

JSII_DEBUG output
>{"api":"load","name":"constructs","version":"10.0.5","tarball":"/var/folders/cf/ttmfqtv97z13mfktyqcjgq040000gr/T/constructs-1.30.0.521499037.tgz"}
[@jsii/kernel] load {
  api: 'load',
  name: 'constructs',
  version: '10.0.5',
  tarball: '/var/folders/cf/ttmfqtv97z13mfktyqcjgq040000gr/T/constructs-1.30.0.521499037.tgz'
}
[@jsii/kernel] creating jsii-kernel modules workdir: /var/folders/cf/ttmfqtv97z13mfktyqcjgq040000gr/T/jsii-kernel-XdEwkS
< {"ok":{"assembly":"constructs","types":10}}
> {"api":"load","name":"aws-cdk-lib","version":"2.0.0-rc.7","tarball":"/var/folders/cf/ttmfqtv97z13mfktyqcjgq040000gr/T/aws-cdk-lib-1.30.0.948206680.tgz"}
[@jsii/kernel] load {
  api: 'load',
  name: 'aws-cdk-lib',
  version: '2.0.0-rc.7',
  tarball: '/var/folders/cf/ttmfqtv97z13mfktyqcjgq040000gr/T/aws-cdk-lib-1.30.0.948206680.tgz'
}
< {"ok":{"assembly":"aws-cdk-lib","types":6514}}
> {"api":"create","fqn":"aws-cdk-lib.App","args":[null]}
[@jsii/kernel] create { api: 'create', fqn: 'aws-cdk-lib.App', args: [ null ] }
[@jsii/kernel] toSandbox null [{"serializationClass":"Struct","typeRef":{"docs":{"summary":"initialization properties."},"name":"props","optional":true,"type":{"fqn":"aws-cdk-lib.AppProps"}}}]
< {"ok":{"$jsii.byref":"aws-cdk-lib.App@10000"}}
> {"api":"create","fqn":"aws-cdk-lib.Stack","args":[{"$jsii.byref":"aws-cdk-lib.App@10000"},"GocloudfrontStack",{"$jsii.struct":{"fqn":"aws-cdk-lib.StackProps","data":{}}}]}
[@jsii/kernel] create {
  api: 'create',
  fqn: 'aws-cdk-lib.Stack',
  args: [
    { '$jsii.byref': 'aws-cdk-lib.App@10000' },
    'GocloudfrontStack',
    { '$jsii.struct': [Object] }
  ]
}
[@jsii/kernel] toSandbox { '$jsii.byref': 'aws-cdk-lib.App@10000' } [{"serializationClass":"RefType","typeRef":{"docs":{"summary":"Parent of this stack, usually an `App` or a `Stage`, but could be any construct."},"name":"scope","optional":true,"type":{"fqn":"constructs.Construct"}}}]
[@jsii/kernel] toSandbox GocloudfrontStack [{"serializationClass":"Scalar","typeRef":{"docs":{"remarks":"If `stackName` is not explicitly\ndefined, this id (and any parent IDs) will be used to determine the\nphysical ID of the stack.","summary":"The construct ID of this stack."},"name":"id","optional":true,"type":{"primitive":"string"}}}]
[@jsii/kernel] toSandbox { '$jsii.struct': { fqn: 'aws-cdk-lib.StackProps', data: {} } } [{"serializationClass":"Struct","typeRef":{"docs":{"summary":"Stack properties."},"name":"props","optional":true,"type":{"fqn":"aws-cdk-lib.StackProps"}}}]
< {"ok":{"$jsii.byref":"aws-cdk-lib.Stack@10001"}}
> {"api":"create","fqn":"Object"}
[@jsii/kernel] create { api: 'create', fqn: 'Object' }
< {"ok":{"$jsii.byref":"Object@10002"}}
> {"api":"create","fqn":"aws-cdk-lib.aws_cloudfront.CfnFunction","args":[{"$jsii.byref":"aws-cdk-lib.Stack@10001"},"myCDKFunction",{"$jsii.struct":{"fqn":"aws-cdk-lib.aws_cloudfront.CfnFunctionProps","data":{"autoPublish":true,"functionCode":"hi","functionConfig":{"$jsii.byref":"Object@10002"},"name":"myCfnFunctionCdk"}}}]}
[@jsii/kernel] create {
  api: 'create',
  fqn: 'aws-cdk-lib.aws_cloudfront.CfnFunction',
  args: [
    { '$jsii.byref': 'aws-cdk-lib.Stack@10001' },
    'myCDKFunction',
    { '$jsii.struct': [Object] }
  ]
}
[@jsii/kernel] toSandbox { '$jsii.byref': 'aws-cdk-lib.Stack@10001' } [{"serializationClass":"RefType","typeRef":{"docs":{"summary":"- scope in which this resource is defined."},"name":"scope","type":{"fqn":"constructs.Construct"}}}]
[@jsii/kernel] toSandbox myCDKFunction [{"serializationClass":"Scalar","typeRef":{"docs":{"summary":"- scoped id of the resource."},"name":"id","type":{"primitive":"string"}}}]
[@jsii/kernel] toSandbox {
  '$jsii.struct': {
    fqn: 'aws-cdk-lib.aws_cloudfront.CfnFunctionProps',
    data: {
      autoPublish: true,
      functionCode: 'hi',
      functionConfig: [Object],
      name: 'myCfnFunctionCdk'
    }
  }
} [{"serializationClass":"Struct","typeRef":{"docs":{"summary":"- resource properties."},"name":"props","type":{"fqn":"aws-cdk-lib.aws_cloudfront.CfnFunctionProps"}}}]
[@jsii/kernel] toSandbox true [{"serializationClass":"Scalar","typeRef":{"type":{"primitive":"boolean"},"optional":true}},{"serializationClass":"RefType","typeRef":{"type":{"fqn":"aws-cdk-lib.IResolvable"},"optional":true}}]
[@jsii/kernel] toSandbox hi [{"serializationClass":"Scalar","typeRef":{"abstract":true,"docs":{"custom":{"link":"http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cloudfront-function.html#cfn-cloudfront-function-functioncode"},"stability":"external","summary":"`AWS::CloudFront::Function.FunctionCode`."},"immutable":true,"locationInModule":{"filename":"lib/aws-cloudfront/lib/cloudfront.generated.ts","line":3191},"name":"functionCode","optional":true,"type":{"primitive":"string"}}}]
[@jsii/kernel] toSandbox { '$jsii.byref': 'Object@10002' } [{"serializationClass":"Struct","typeRef":{"type":{"fqn":"aws-cdk-lib.aws_cloudfront.CfnFunction.FunctionConfigProperty"},"optional":true}},{"serializationClass":"RefType","typeRef":{"type":{"fqn":"aws-cdk-lib.IResolvable"},"optional":true}}]
[@jsii/kernel] Expected value type but got reference type, accepting for now (awslabs/jsii#400)
[@jsii/kernel] toSandbox myCfnFunctionCdk [{"serializationClass":"Scalar","typeRef":{"abstract":true,"docs":{"custom":{"link":"http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cloudfront-function.html#cfn-cloudfront-function-name"},"stability":"external","summary":"`AWS::CloudFront::Function.Name`."},"immutable":true,"locationInModule":{"filename":"lib/aws-cloudfront/lib/cloudfront.generated.ts","line":3181},"name":"name","type":{"primitive":"string"}}}]
< {"ok":{"$jsii.byref":"aws-cdk-lib.aws_cloudfront.CfnFunction@10003"}}
> {"api":"invoke","method":"synth","args":[null],"objref":{"$jsii.byref":"aws-cdk-lib.App@10000"}}
[@jsii/kernel] invoke { '$jsii.byref': 'aws-cdk-lib.App@10000' } synth [ null ]
[@jsii/kernel] toSandbox null [{"serializationClass":"Struct","typeRef":{"name":"options","optional":true,"type":{"fqn":"aws-cdk-lib.StageSynthesisOptions"}}}]
panic: "Resolution error: Supplied properties not correct for \"CfnFunctionProps\"\n  functionConfig: supplied properties not correct for \"FunctionConfigProperty\"\n    comment: required but missing\n    runtime: required but missing."

goroutine 1 [running]:
github.com/aws/jsii-runtime-go/runtime.Invoke(0x1534b20, 0xc0003320d0, 0x158cfe0, 0x5, 0xc0004bbec8, 0x1, 0x1, 0x14ae9e0, 0xc0003328b0)
	/Users/nlynch/go/pkg/mod/github.com/aws/[email protected]/runtime/runtime.go:214 +0x2b7
github.com/aws/aws-cdk-go/awscdk/v2.(*jsiiProxy_App).Synth(0xc0003320d0, 0x0, 0x15943ac, 0x11)
	/Users/nlynch/go/pkg/mod/github.com/aws/aws-cdk-go/awscdk/[email protected]/awscdk.go:324 +0xb5
main.main()
	/var/folders/cf/ttmfqtv97z13mfktyqcjgq040000gr/T/tmp.YbIgojCg/gocloudfront/gocloudfront.go:48 +0xfa
exit status 2
�[31mSubprocess exited with error 1�[39m

To me, the interesting bit appears to be that -- unlike the other objects -- the FunctionConfig is created without any fqn info or args.

> {"api":"create","fqn":"Object"}
[@jsii/kernel] create { api: 'create', fqn: 'Object' }
< {"ok":{"$jsii.byref":"Object@10002"}}

Compared to the Function itself:

> {"api":"create","fqn":"aws-cdk-lib.aws_cloudfront.CfnFunction","args":[{"$jsii.byref":"aws-cdk-lib.Stack@10001"},"myCDKFunction",{"$jsii.struct":{"fqn":"aws-cdk-lib.aws_cloudfront.CfnFunctionProps","data":{"autoPublish":true,"functionCode":"hi","functionConfig":{"$jsii.
byref":"Object@10002"},"name":"myCfnFunctionCdk"}}}]}
[@jsii/kernel] create {
  api: 'create',
  fqn: 'aws-cdk-lib.aws_cloudfront.CfnFunction',
  args: [
    { '$jsii.byref': 'aws-cdk-lib.Stack@10001' },
    'myCDKFunction',
    { '$jsii.struct': [Object] }
  ]
}
@njlynch njlynch added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jun 11, 2021
@NGL321 NGL321 added language/go Regarding GoLang bindings and removed needs-triage This issue or PR still needs to be triaged. labels Jul 24, 2021
@MrArnoldPalmer
Copy link
Contributor

So this works without the pointer type, but I believe this is a bug. This compiles and runs.

	cf.NewCfnFunction(stack, jsii.String("myCDKFunction"), &cf.CfnFunctionProps{
		Name:         jsii.String("myCfnFunctionCdk"),
		FunctionCode: jsii.String("myFunctionCode"),
		AutoPublish:  jsii.Bool(true),
		FunctionConfig: cf.CfnFunction_FunctionConfigProperty{
			Comment: jsii.String("testcdk"),
			Runtime: jsii.String("cloudfront-js-1.0"),
		},
	})

The generated type of CfnFunctionProps is as follows:

type CfnFunctionProps struct {
	Name *string `json:"name"`
	AutoPublish interface{} `json:"autoPublish"`
	FunctionCode *string `json:"functionCode"`
	FunctionConfig interface{} `json:"functionConfig"`
	FunctionMetadata interface{} `json:"functionMetadata"`
}

I believe the FunctionConfig property type should be *CfnFunction_FunctionConfigProperty. @RomainMuller am I missing something in this case?

@RomainMuller
Copy link
Contributor

According to the documentation, the FunctionConfig property is a type union (IResolvable | FunctionConfigProperty). This effectively means the property can only be typed as interface{} in Go.

That being said, the serialiser should probably be able to work it out whether a pointer is used or not in this particular case (since the value is a struct). It might fall in an crack in the current logic.

@MrArnoldPalmer MrArnoldPalmer removed their assignment Dec 21, 2021
RomainMuller added a commit that referenced this issue Apr 11, 2022
When a struct pointer was passed in an `interface{}` position, the
converter would trip and register a new opaque instance of `Object`
instead of correctly serializing a struct.

This is because hte pointer is double-indirected (the `interface{}` is a
reference to the `&struct`, which is a reference to the `struct`). This
change adds a condition to detect such cases (`interface{}` abstracts
another pointer), and properly serialize that.

Fixes #2880
@RomainMuller RomainMuller moved this from Todo to In Review in CDK Go General Availability Apr 11, 2022
@RomainMuller RomainMuller self-assigned this Apr 11, 2022
@mergify mergify bot closed this as completed in #3485 Apr 11, 2022
mergify bot pushed a commit that referenced this issue Apr 11, 2022
When a struct pointer was passed in an `interface{}` position, the
converter would trip and register a new opaque instance of `Object`
instead of correctly serializing a struct.

This is because hte pointer is double-indirected (the `interface{}` is a
reference to the `&struct`, which is a reference to the `struct`). This
change adds a condition to detect such cases (`interface{}` abstracts
another pointer), and properly serialize that.

Fixes #2880



---

By submitting this pull request, I confirm that my contribution is made under the terms of the [Apache 2.0 license].

[Apache 2.0 license]: https://www.apache.org/licenses/LICENSE-2.0
Repository owner moved this from In Review to Done in CDK Go General Availability Apr 11, 2022
@github-actions
Copy link
Contributor

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. language/go Regarding GoLang bindings p1
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

4 participants