kcl-lang / kcl

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

[Enhancement] Top level config unification backward reference. #247

Closed Peefy closed 5 months ago

Peefy commented 1 year ago

Enhancement

For the following KCL code (test.k):

schema Data:
    name: str
    version?: str

data1: Data {
    name = data2.name
}

data2: Data {
    name = "1"
    version = version
}

# Global version
version = "v0.1.0"

I want to get the expected YAML data:

data1:
  name: '1'
data2:
  name: '1'
  version: v0.1.0
version: v0.1.0

Instead of the error message:

KCL Compile Error[E2L23] : A complie error occurs during compiling
---> File test.k:5:12
5 |    name = data2.name
           12 ^  -> Failure
name 'data2' is not defined
octonawish-akcodes commented 8 months ago

Since data2 is defined after data1 struct during compilation it isn't finding the data2 struct because it is used before the declaration.

I tried to recreate the issue I do get the same error but when I tried to rearrange the code like this:

schema Data:
    name: str
    version?: str

# Global version
version = "v0.1.0"

data2: Data {
    name = "1"
    version = version
}

data1: Data {
    name = data2.name
}

it gives the expected output:

version: v0.1.0
data2:
  name: '1'
  version: v0.1.0
data1:
  name: '1'

I am not sure what is expected here? @Peefy

Peefy commented 8 months ago

Since data2 is defined after data1 struct during compilation it isn't finding the data2 struct because it is used before the declaration.

I tried to recreate the issue I do get the same error but when I tried to rearrange the code like this:

schema Data:
    name: str
    version?: str

# Global version
version = "v0.1.0"

data2: Data {
    name = "1"
    version = version
}

data1: Data {
    name = data2.name
}

it gives the expected output:

version: v0.1.0
data2:
  name: '1'
  version: v0.1.0
data1:
  name: '1'

I am not sure what is expected here? @Peefy

You are right!