kcl-lang / kcl

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

GetSchemaType(Mapping) does not list schemas without identifier #1250

Closed patrycju closed 6 months ago

patrycju commented 6 months ago

Bug Report

When schema instances are created without identifier (e.g. as an element of list or without assigning it to any name), they are ignored by GetSchemaType and GetSchemaTypeMapping functions.

1. Minimal reproduce step (Required)

KCL file:

paths = ["./hello", "./world"]

schema Dir:
    path: str

tmpDir = Dir {
    path = "/tmp/sometemp"
}

Dir { path = "/tmp/tmp" }

hello_dirs = [Dir { path = p } for p in paths]
result, _ := kcl.RunFiles([]string{"dirs.k"})
schema, _ := kcl.GetSchemaTypeMapping("dirs.k", "", "")

fmt.Println(result.First().YAMLString())

for k, v := range schema {
    fmt.Println(k, v)
}

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

hello_dirs:
    - path: ./hello
    - path: ./world
path: /tmp/tmptmp
paths:
    - ./hello
    - ./world
tmpDir:
    path: /tmp/sometemp

Dir type:"schema"  schema_name:"Dir"  properties:{key:"path"  value:{type:"str"  line:1}}  required:"path"  filename:"/Users/patrycjusz/Workspace/iceberg/ice/manifests/dirs.k"  pkg_path:"__main__"
tmpDir type:"schema"  schema_name:"Dir"  properties:{key:"path"  value:{type:"str"  line:1}}  required:"path"  filename:"/Users/patrycjusz/Workspace/iceberg/ice/manifests/dirs.k"  pkg_path:"__main__"

# Object without assigned name
type:"schema"  schema_name:"Dir"  properties:{key:"path"  value:{type:"str"  line:1}}  required:"path"  filename:"/Users/patrycjusz/Workspace/iceberg/ice/manifests/dirs.k"  pkg_path:"__main__"

# Objects located in hello_dirs:
type:"schema"  schema_name:"Dir"  properties:{key:"path"  value:{type:"str"  line:1}}  required:"path"  filename:"/Users/patrycjusz/Workspace/iceberg/ice/manifests/dirs.k"  pkg_path:"__main__"
type:"schema"  schema_name:"Dir"  properties:{key:"path"  value:{type:"str"  line:1}}  required:"path"  filename:"/Users/patrycjusz/Workspace/iceberg/ice/manifests/dirs.k"  pkg_path:"__main__"

3. What did you see instead (Required)

hello_dirs:
    - path: ./hello
    - path: ./world
paths:
    - ./hello
    - ./world
tmpDir:
    path: /tmp/sometemp

Dir type:"schema"  schema_name:"Dir"  properties:{key:"path"  value:{type:"str"  line:1}}  required:"path"  filename:"/Users/patrycjusz/Workspace/iceberg/ice/manifests/dirs.k"  pkg_path:"__main__"
tmpDir type:"schema"  schema_name:"Dir"  properties:{key:"path"  value:{type:"str"  line:1}}  required:"path"  filename:"/Users/patrycjusz/Workspace/iceberg/ice/manifests/dirs.k"  pkg_path:"__main__"

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

KCL go SDK v0.8.5

Peefy commented 6 months ago

Hello @patrycju. Thank you very much for your feedback. Currently, the API only returns named schema types for schema instances. Can you provide me with some more specific scenarios? For example, do you want to obtain the types of all instances or the values after running.

Additionally, PR is welcome. If not, I will probably implement it and release a new v0.8.6 version next week.

patrycju commented 6 months ago

Hey @Peefy, hope you're going well. What I essentially need is to load all instances (named and not named) of all schemas (e.g. all Dirs, Files, Socket) into their corresponding structs in Go. I can't use JSON/YAML output as it does not have Schema name information, so as far as I know, the only way is to use GetSchemaType[Mapping] and somehow map schemas and the values from JSON.

Is there a way to do this in the SDK?

Peefy commented 6 months ago

Make sense. You can use the 'WithFullTypePath' option to get all schema type instance names in the 'Run' API. And use the GetSchemaTypeMapping to get the type information.

patrycju commented 6 months ago

WithFullTypePath works like a charm, thanks! Issue can be closed.

Peefy commented 6 months ago

Thank you! ❤