kcl-lang / kcl

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

[Enhancement]: schema inference does not work well for nested layout when there's dynamic fields defined #1707

Open Yufeireal opened 1 month ago

Yufeireal commented 1 month ago

General Question

Hi team, I've posted a question in our kcl slack channel: https://cloud-native.slack.com/archives/C05TC96NWN8/p1729279836481899 File an issue here in case it's a bug.

When defining schemas like this

schema ContainerInputs:
    maxReplicas?: int = 3

schema DeploymentInputs:
    [...str]: ContainerInputs

schema DeploymentCollectionInputs:
    [...str]: DeploymentInputs

schema NewDeploymentInputs(DeploymentInputs):
    [...str]: NewContainerInputs

schema NewContainerInputs(ContainerInputs):
    minReplicas?: int = 2

schema NewDeploymentCollectionInputs(DeploymentCollectionInputs):
    [...str]: NewDeploymentInputs

schema Inputs:
    vdeployments: DeploymentCollectionInputs

schema NewInputs(Inputs):
    vdeployments: NewDeploymentCollectionInputs

a = Inputs {
    vdeployments: {
        deploy_a: {
            container_a: {
                maxReplicas: 4
            }
        }
    }
}
# When trying to use inheritance, got `expect ContainerInputs, got dict`. Expect to get {minReplicas: 2, maxReplicas: 3}?
b = NewInputs {
    vdeployments: {
        deploy_b: {
            container_b: {
              minReplicas: 2
            }
        }
    }
}

Playground link

I got an error about expect ContainerInputs, but got a dict. However, if I change the initialization to be:

b = NewInputs {
    vdeployments: {
        deploy_b: {
            container_b: NewContainerInputs {
               minReplicas: 3
            }
        }
    }
}

It works.

Not sure if this is expected when base schema has dynamic fields and we does not support override those dynamic fields right now. Need some help here, thanks!

Peefy commented 1 month ago

Yes, this is due to the imperfect running mechanism of the KCL evaluator, although the IDE can recognize it correctly. We will improve it in future versions, and now you need to explicitly specify the type.

b = NewInputs {
    vdeployments: NewDeploymentCollectionInputs {
        deploy_b: {
            container_b: {
                minReplicas: 2
            }
        }
    }
}

or

b = NewInputs {
    vdeployments: {
        deploy_b: {
            container_b: NewContainerInputs {
                minReplicas: 2
            }
        }
    }
}
Yufeireal commented 1 month ago

Got it!! Thanks for taking a look.