How to handle cross-type attribute references in golang

Hey folks, I’m using cdktf golang to build an ASG with a launch template. What I have is this:

	provider.NewAwsProvider(stack, jsii.String("aws"), &provider.AwsProviderConfig{
		Region: jsii.String("ap-northeast-1"),
	})

	serverLt := launchtemplate.NewLaunchTemplate(stack, jsii.String("serverLt"), &launchtemplate.LaunchTemplateConfig{})

	autoscalinggroup.NewAutoscalingGroup(stack, jsii.String("serverAsg"), &autoscalinggroup.AutoscalingGroupConfig{
		MaxSize: jsii.Number[float64](1),
		MinSize: jsii.Number[float64](1),
		LaunchTemplate: &autoscalinggroup.AutoscalingGroupLaunchTemplate{
			Id:      serverLt.Id(),
			Version: serverLt.GetStringAttribute(jsii.String("latest_version")),
		},
	})

But the GetStringAttribute feels a little rough.

In Typescript, I can take the (float) latestVersion and simply call toString to get a string-typed reference:

    new AwsProvider(this, "aws", {
      region: "ap-northeast-1"
    })

    const launchTemplate = new LaunchTemplate(this, "serverLt", {
      name: "server",
    })

    new AutoscalingGroup(this, "serverAsg", {
      name: "server",
      minSize: 1,
      maxSize: 1,
      launchTemplate: {
        id: launchTemplate.id,
        version: launchTemplate.latestVersion.toString(),
      }
    })

But doing something like jsii.String(strconv.FormatFloat(*serverLt.LatestVersion(), 'E', -1, 64)) in golang ends up jamming a junk float value (-8.10956221259138E+298) into the synthesized JSON.

Is there a cleaner API to get a string reference from a float64 reference in terraform-cdk-go?

Thanks in advance!
Mat

Hi @mat.schaffer :wave:

You could probably use cdktf.Token_AsString(serverLt.LatestVersion()). Alternatively, using the tostring function should also work: cdktf.Fn_Tostring(serverLt.LatestVersion()), however this will instead render tostring() instead of the automatic type conversion done by Terraform. Both should behave exactly the same though :smile:

– Ansgar

1 Like

Thanks @ansgarm ! Looks like Token_AsString requires a config arg (at least on what I have handy, 0.18.0)

cdktf.Token_AsString(serverLt.LatestVersion(), &cdktf.EncodingOptions{})

or I guess I could pass nil.

And I see that

cdktf.Fn_Tostring(serverLt.LatestVersion())

Does include a tostring wrapper.

Not sure which of the 3 I like best. I guess Token_AsString is at least conceptually similar to the typescript .toString so I’ll go with that!