kcl-lang / kcl

KCL Programming Language (CNCF Sandbox Project). https://kcl-lang.io
https://kcl-lang.io
Apache License 2.0
1.7k stars 119 forks source link

Adding Elements in with conditional does not work as expected #1658

Closed karlderkaefer closed 2 months ago

karlderkaefer commented 2 months ago

Bug Report

I have problems to use arrays with conditionals. I would like add an element to an array, but the output is not as expected. Not sure if only primitive types are supported in arrays

1. Minimal reproduce step (Required)

# Define schemas
schema PolicyStatement:
    Sid: str
    Effect: str
    Action: [str]
    Resource: [str]

schema PolicyDocument:
    Version: str = "2012-10-17"
    Statement: [PolicyStatement]

_s3_statement = PolicyStatement {
    Sid = "AllowS3Read"
    Effect = "Allow"
    Action = [
        "s3:GetObject"
        "s3:GetObjectVersion"
        "s3:ListBucket"
        "s3:ListBucketVersions"
    ]
    Resource = ["arn:aws:s3:::mybucket/*"]
}

_statement_custom_key_1 = PolicyStatement {
    Sid = "AllowKMSDecryptCustomKey"
    Effect = "Allow"
    Action = ["kms:Decrypt"]
    Resource = ["arn:aws:kms:::alias/custom-key"]
}

_statement_custom_key_2: PolicyStatement = PolicyStatement {
    Sid = "AllowKMSDecryptCustomKey2"
    Effect = "Allow"
    Action = ["kms:Decrypt"]
    Resource = ["arn:aws:kms:::alias/custom-key2"]
}

_statements: [PolicyStatement] = [_s3_statement]

if False:
    _statements += [_statement_custom_key_1]
if True:
    _statements += [_statement_custom_key_2]

items = [
    PolicyDocument {
        Statement = _statements
    }
]

2. What did you expect to see? (Required)

items:
- Version: '2012-10-17'
  Statement:
  - Sid: AllowS3Read
    Effect: Allow
    Action:
    - s3:GetObject
    - s3:GetObjectVersion
    - s3:ListBucket
    - s3:ListBucketVersions
    Resource:
    - arn:aws:s3:::mybucket/*
  - Sid: AllowKMSDecryptCustomKey2
    Effect: Allow
    Action:
    - kms:Decrypt
    Resource:
    - arn:aws:kms:::alias/custom-key2

3. What did you see instead (Required)

items:
- Version: '2012-10-17'
  Statement:
  - Sid: AllowS3Read
    Effect: Allow
    Action:
    - s3:GetObject
    - s3:GetObjectVersion
    - s3:ListBucket
    - s3:ListBucketVersions
    Resource:
    - arn:aws:s3:::mybucket/*
  - Sid: AllowKMSDecryptCustomKey2
    Effect: Allow
    Action:
    - kms:Decrypt
    Resource:
    - arn:aws:kms:::alias/custom-key2
  - Sid: AllowKMSDecryptCustomKey2
    Effect: Allow
    Action:
    - kms:Decrypt
    Resource:

4. What is your KCL components version? (Required)

kcl --version kcl version 0.9.4

karlderkaefer commented 2 months ago

It seem to work with work with latest version 0.10.0 im doing some more tests in crossplane

kcl demo.k
items:
- Version: '2012-10-17'
  Statement:
  - Sid: AllowS3Read
    Effect: Allow
    Action:
    - s3:GetObject
    - s3:GetObjectVersion
    - s3:ListBucket
    - s3:ListBucketVersions
    Resource:
    - arn:aws:s3:::mybucket/*
  - Sid: AllowKMSDecryptCustomKey2
    Effect: Allow
    Action:
    - kms:Decrypt
    Resource:
    - arn:aws:kms:::alias/custom-key2
Peefy commented 2 months ago

Yes, we have fixed this issue in KCL v0.10.0 and you can use crossplane function-kcl v0.10.0