Documentation
¶
Overview ¶
KCLVM binding for Go
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ kcl files │ │ KCLVM-Go-API │ │ KCLResultList │
│ ┌───────────┐ │ │ │ │ │
│ │ 1.k │ │ │ │ │ │
│ └───────────┘ │ │ │ │ ┌───────────┐ │ ┌───────────────┐
│ ┌───────────┐ │ │ ┌───────────┐ │ │ │ KCLResult │──┼────────▶│x.Get("a.b.c") │
│ │ 2.k │ │ │ │ Run(path) │ │ │ └───────────┘ │ └───────────────┘
│ └───────────┘ │────┐ │ └───────────┘ │ │ │
│ ┌───────────┐ │ │ │ │ │ ┌───────────┐ │ ┌───────────────┐
│ │ 3.k │ │ │ │ │ │ │ KCLResult │──┼────────▶│x.Get("k", &v) │
│ └───────────┘ │ │ │ │ │ └───────────┘ │ └───────────────┘
│ ┌───────────┐ │ ├───▶│ ┌───────────┐ │──────────▶│ │
│ │setting.yml│ │ │ │ │RunFiles() │ │ │ ┌───────────┐ │ ┌───────────────┐
│ └───────────┘ │ │ │ └───────────┘ │ │ │ KCLResult │──┼────────▶│x.JSONString() │
└─────────────────┘ │ │ │ │ └───────────┘ │ └───────────────┘
│ │ │ │ │
┌─────────────────┐ │ │ │ │ ┌───────────┐ │ ┌───────────────┐
│ Options │ │ │ ┌───────────┐ │ │ │ KCLResult │──┼────────▶│x.YAMLString() │
│WithOptions │ │ │ │MustRun() │ │ │ └───────────┘ │ └───────────────┘
│WithOverrides │────┘ │ └───────────┘ │ │ │
│WithWorkDir │ │ │ │ │
│WithDisableNone │ │ │ │ │
└─────────────────┘ └─────────────────┘ └─────────────────┘
Example ¶
const k_code = `
name = "kcl"
age = 1
two = 2
schema Person:
name: str = "kcl"
age: int = 1
x0 = Person {}
x1 = Person {
age = 101
}
`
yaml := kclvm.MustRun("testdata/main.k", kclvm.WithCode(k_code)).First().YAMLString()
fmt.Println(yaml)
fmt.Println("----")
result := kclvm.MustRun("./testdata/main.k").First()
fmt.Println(result.JSONString())
fmt.Println("----")
fmt.Println("x0.name:", result.Get("x0.name"))
fmt.Println("x1.age:", result.Get("x1.age"))
fmt.Println("----")
var person struct {
Name string
Age int
}
fmt.Printf("person: %+v\n", result.Get("x1", &person))
Index ¶
- Constants
- func FormatCode(code interface{}) ([]byte, error)
- func FormatPath(path string) (changedPaths []string, err error)
- func InitKclvmPath(kclvmRoot string)
- func InitKclvmRuntime(n int)
- func LintPath(paths []string) (results []string, err error)
- func ListDepFiles(workDir string, opt *ListDepFilesOption) (files []string, err error)
- func ListDownStreamFiles(workDir string, opt *ListDepsOptions) ([]string, error)
- func ListUpStreamFiles(workDir string, opt *ListDepsOptions) (deps []string, err error)
- func OverrideFile(file string, specs, importPaths []string) (bool, error)
- func ValidateCode(data, code string, opt *ValidateOptions) (ok bool, err error)
- type KCLResult
- type KCLResultList
- type KclType
- type ListDepFilesOption
- type ListDepsOptions
- type Option
- func WithCode(codes ...string) Option
- func WithDisableNone(disableNone bool) Option
- func WithKFilenames(filenames ...string) Option
- func WithOptions(key_value_list ...string) Option
- func WithOverrides(override_list ...string) Option
- func WithPrintOverridesAST(printOverridesAST bool) Option
- func WithSettings(filename string) Option
- func WithSortKeys(sortKeys bool) Option
- func WithWorkDir(workDir string) Option
- type ValidateOptions
Examples ¶
Constants ¶
const KclvmAbiVersion = scripts.KclvmAbiVersion
KclvmAbiVersion is the current kclvm ABI version.
Variables ¶
This section is empty.
Functions ¶
func FormatCode ¶
FormatCode returns the formatted code.
Example ¶
out, err := kclvm.FormatCode(`a = 1+2`)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(out))
Output: a = 1 + 2
func FormatPath ¶
FormatPath formats files from the given path path: if path is `.` or empty string, all KCL files in current directory will be formatted, not recursively if path is `path/file.k`, the specified KCL file will be formatted if path is `path/to/dir`, all KCL files in the specified dir will be formatted, not recursively if path is `path/to/dir/...`, all KCL files in the specified dir will be formatted recursively
the returned changedPaths are the changed file paths (relative path)
Example ¶
changedPaths, err := kclvm.FormatPath("testdata/fmt")
if err != nil {
log.Fatal(err)
}
fmt.Println(changedPaths)
func LintPath ¶
LintPath lint files from the given path
Example ¶
// import a
// import a # reimport
results, err := kclvm.LintPath([]string{"testdata/lint/import.k"})
if err != nil {
log.Fatal(err)
}
for _, s := range results {
fmt.Println(s)
}
Output: Module 'a' is reimported multiple times Module 'a' imported but unused
func ListDepFiles ¶
func ListDepFiles(workDir string, opt *ListDepFilesOption) (files []string, err error)
ListDepFiles return the depend files from the given path
func ListDownStreamFiles ¶
func ListDownStreamFiles(workDir string, opt *ListDepsOptions) ([]string, error)
ListDownStreamFiles return a list of downstream depend files from the given changed path list.
func ListUpStreamFiles ¶
func ListUpStreamFiles(workDir string, opt *ListDepsOptions) (deps []string, err error)
ListUpStreamFiles return a list of upstream depend files from the given path list
func OverrideFile ¶
OverrideFile rewrites a file with override spec file: string. The File that need to be overridden specs: []string. List of specs that need to be overridden.
Each spec string satisfies the form: <pkgpath>:<field_path>=<filed_value> or <pkgpath>:<field_path>- When the pkgpath is '__main__', it can be omitted.
importPaths. List of import statements that need to be added
func ValidateCode ¶
func ValidateCode(data, code string, opt *ValidateOptions) (ok bool, err error)
ValidateCode validate data match code
Types ¶
type KCLResult ¶
Example ¶
const k_code = `
name = "kcl"
age = 1
two = 2
schema Person:
name: str = "kcl"
age: int = 1
x0 = Person {name = "kcl-go"}
x1 = Person {age = 101}
`
result := kclvm.MustRun("testdata/main.k", kclvm.WithCode(k_code)).First()
fmt.Println("x0.name:", result.Get("x0.name"))
fmt.Println("x1.age:", result.Get("x1.age"))
Output: x0.name: kcl-go x1.age: 101
type KCLResultList ¶
type KCLResultList = kcl.KCLResultList
func MustRun ¶
func MustRun(path string, opts ...Option) *KCLResultList
MustRun is like Run but panics if return any error.
Example ¶
yaml := kclvm.MustRun("testdata/main.k", kclvm.WithCode(`name = "kcl"`)).First().YAMLString()
fmt.Println(yaml)
Output: name: kcl
Example (RawYaml) ¶
const code = `
b = 1
a = 2
`
yaml := kclvm.MustRun("testdata/main.k", kclvm.WithCode(code)).GetRawYamlResult()
fmt.Println(yaml)
yaml_sorted := kclvm.MustRun("testdata/main.k", kclvm.WithCode(code), kclvm.WithSortKeys(true)).GetRawYamlResult()
fmt.Println(yaml_sorted)
Output: b: 1 a: 2 a: 2 b: 1
Example (SchemaType) ¶
const code = `
schema Person:
name: str = ""
x = Person()
`
json := kclvm.MustRun("testdata/main.k", kclvm.WithCode(code)).First().JSONString()
fmt.Println(json)
Output: { "x": { "name": "" } }
Example (Settings) ¶
yaml := kclvm.MustRun("./testdata/app0/kcl.yaml").First().YAMLString()
fmt.Println(yaml)
func Run ¶
func Run(path string, opts ...Option) (*KCLResultList, error)
Run evaluates the KCL program with path and opts, then returns the object list.
Example (GetField) ¶
// run kcl.yaml
x, err := kclvm.Run("./testdata/app0/kcl.yaml")
assert(err == nil, err)
// print deploy_topology[1].zone
fmt.Println(x.First().Get("deploy_topology.1.zone"))
Output: R000A
type KclType ¶
func GetSchemaType ¶
GetSchemaType returns schema types from a kcl file or code.
file: string
The kcl filename
code: string
The kcl code string
schema_name: string
The schema name got, when the schema name is empty, all schemas are returned.
type ListDepFilesOption ¶
type ListDepsOptions ¶
type ListDepsOptions = list.DepOptions
type Option ¶
func WithDisableNone ¶
WithDisableNone returns a Option which hold a disable none switch.
func WithKFilenames ¶
WithKFilenames returns a Option which hold a filenames list.
func WithOptions ¶
WithOptions returns a Option which hold a key=value pair list for option function.
Example ¶
const code = `
name = option("name")
age = option("age")
`
x, err := kclvm.Run("hello.k", kclvm.WithCode(code),
kclvm.WithOptions("name=kcl", "age=1"),
)
if err != nil {
log.Fatal(err)
}
fmt.Println(x.First().YAMLString())
Output: age: 1 name: kcl
func WithOverrides ¶
WithOverrides returns a Option which hold a override list.
func WithPrintOverridesAST ¶
WithPrintOverridesAST returns a Option which hold a printOverridesAST switch.
func WithSettings ¶
WithSettings returns a Option which hold a settings file.
func WithSortKeys ¶
WithSortKeys returns a Option which hold a sortKeys switch.
func WithWorkDir ¶
WithWorkDir returns a Option which hold a work dir.
type ValidateOptions ¶
type ValidateOptions = validate.ValidateOptions
Directories
¶
| Path | Synopsis |
|---|---|
|
cmds
|
|
|
kcl-go
command
Kusion Configuration Language (KCL) is a declarative configuration language inspired by Python3 designed for Cloud-Native scenes used in Kusion.
|
Kusion Configuration Language (KCL) is a declarative configuration language inspired by Python3 designed for Cloud-Native scenes used in Kusion. |
|
examples
|
|
|
hello
command
|
|
|
kubernetes
command
|
|
|
pkg
|
|
|
kcl
Package kcl defines the top-level interface for the Kusion Configuration Language (KCL).
|
Package kcl defines the top-level interface for the Kusion Configuration Language (KCL). |
|
tools/list
Package list extracts information by parsing KCL source code and return it as list.
|
Package list extracts information by parsing KCL source code and return it as list. |