jsii-rosetta icon indicating copy to clipboard operation
jsii-rosetta copied to clipboard

fix: inferred structs from a namespace should be taken from namespace

Open DanielMSchmidt opened this issue 2 years ago • 0 comments
trafficstars

WIP

TODOs

  • [ ] Test Case for star import / inference with namespaces
  • [ ] Test Case for adding imports for inferred structs
  • [ ] See if the problems are correlated, sacrifice the star import one if needed
  • [ ] Might need to have a side-effect in the traversal that informs things to import as well as the ones present in TS

Problem

We have generated typescript code that looks either like this

import { Construct } from "constructs";
import * as aws from "./.gen/providers/aws/";

class MyConvertedCode extends Construct {
  constructor(scope: Construct, name: string) {
    super(scope, name);
    new aws.s3_bucket.S3Bucket(this, "bucket", {
      logging:  {
          target_bucket: "target",
        },
    });
  }
}

or like this (in newer cdktf versions)

import { Construct } from "constructs";
import { S3Bucket } from "./.gen/providers/aws/s3-bucket";

class MyConvertedCode extends Construct {
  constructor(scope: Construct, name: string) {
    super(scope, name);
    new S3Bucket(this, "bucket", {
      logging:  {
          target_bucket: "target",
        },
    });
  }
}

The resulting rosetta converted code leads to this e.g. in Python:

from imports.aws.s3_bucket import S3Bucket
class MyConvertedCode(Construct):
    def __init__(self, scope, name):
        super().__init__(scope, name)

        S3Bucket(self, "bucket",
            logging=S3BucketLogging(
                target_bucket="target"
            )
        )

The correct code would be

from imports.aws.s3_bucket import S3Bucket, S3BucketLogging
class MyConvertedCode(Construct):
    def __init__(self, scope, name):
        super().__init__(scope, name)

        S3Bucket(self, "bucket",
            logging=S3BucketLogging(
                target_bucket="target"
            )
        )

or for the star imported version

import imports.aws as aws
class MyConvertedCode(Construct):
    def __init__(self, scope, name):
        super().__init__(scope, name)

        aws.s3_bucket.S3Bucket(self, "bucket",
            logging=aws.s3_bucket.S3BucketLogging(
                target_bucket="target"
            )
        )

DanielMSchmidt avatar Aug 08 '23 14:08 DanielMSchmidt