kcl icon indicating copy to clipboard operation
kcl copied to clipboard

[Potential Bug, TBD]: schema inference does not work well for nested layout when there's dynamic fields defined

Open Yufeireal opened this issue 1 year ago • 2 comments

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!

Yufeireal avatar Oct 21 '24 17:10 Yufeireal

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
            }
        }
    }
}

Peefy avatar Oct 22 '24 02:10 Peefy

Got it!! Thanks for taking a look.

Yufeireal avatar Oct 23 '24 00:10 Yufeireal