kcl-lang / kcl-lang.io

KCL Website and Documentation Repo: https://kcl-lang.io
https://kcl-lang.github.io
Apache License 2.0
13 stars 35 forks source link

getting items returned inside list using schema's instances() call? #298

Closed wilsonwang371 closed 6 months ago

wilsonwang371 commented 6 months ago

General Question

If I am creating a list of schema A inside one of my lambdas, how do I later get the returned list items through schema A's instances() call?

alist = pkg.mylambda.getSchemaAList()

print(A.instances())

The result is null

Peefy commented 6 months ago

The instance function calling requires the schema instances which are in the main package. This is because it is necessary to control the impact of configuration changes as much as possible, so this limitation is added.

wilsonwang371 commented 6 months ago

if this is the case, how do I call a function and get a list of objects that I want to access later? Assuming I no longer have a direct reference to the return value.

Peefy commented 6 months ago

Does your scenario still assume that the instance method of a schema is called in the function? Or can be used as follows

schema InputSchema:

schema OutputSchema:

getSchemaList = lambda input: InputSchema -> [OutputSchema] {
    [OutputSchema {}] 
}

alist = getSchemaList(InputSchema {})
wilsonwang371 commented 6 months ago

here is my code

import pkg.abc.clusters as cl
import pkg.abc.environ as env
import manifests

clusters = env.getClusters()

appTemplates = [
    cl.AppTemplate {
        appName = "myapp"
        revision = "master"
        valueFiles = ["values.yaml"]
    }
    cl.AppTemplate {
        appName = "myapp"
        revision = "master"
        valueFiles = ["values.yaml"]
    }
    cl.AppTemplate {
        appName = "myapp"
        revision = "master"
    }
]

cl.Project {
    project = "myproject1"
    repoURL = "https://github.com/abc.git"
    path = "a/b/c"
    autoSync = True
}

clusteredAppTemplates = cl.genClusteredAppTemplates(appTemplates, clusters) # in this function, it need access all single instances in clusterdeploys list
print(clusteredAppTemplates)

res = cl.createAllApps()
manifests.yaml_stream(res)
wilsonwang371 commented 6 months ago

However, in my code, it is printing null at the last line

Peefy commented 6 months ago

I see. We usually recommend this method to do it here https://github.com/kcl-lang/konfig/blob/main/examples/appops/nginx-example/dev/main.k

Here, a render file is used to perform rendering or deploy logic

wilsonwang371 commented 6 months ago

ideally, I assume all elements in clusteredAppTemplates can be accessed by their schema's instances() call.

wilsonwang371 commented 6 months ago

I see. We usually recommend this method to do it here https://github.com/kcl-lang/konfig/blob/main/examples/appops/nginx-example/dev/main.k

Here, a render file is used to perform rendering or deploy logic

let me take a look