From 20ae4ee47fe4a72635abb002fda6de89c3320fcd Mon Sep 17 00:00:00 2001 From: peefy Date: Mon, 23 Oct 2023 14:17:42 +0800 Subject: [PATCH] feat: support package_maps config parse and add unit test cases. Signed-off-by: peefy --- pkg/settings/a_test_unix.go | 4 ++++ pkg/settings/a_test_windows.go | 4 ++++ pkg/settings/utils_settings_yaml.go | 19 +++++++++++++++---- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/pkg/settings/a_test_unix.go b/pkg/settings/a_test_unix.go index fc8ad86f..7e149531 100644 --- a/pkg/settings/a_test_unix.go +++ b/pkg/settings/a_test_unix.go @@ -19,6 +19,8 @@ kcl_cli_configs: - ${KCL_MOD}/file2.k - ../../base/base.k disable_none: false + package_maps: + k8s: ../vendor/k8s ` f, err := LoadFile("./sub/settings.yaml", []byte(s)) if err != nil { @@ -33,6 +35,8 @@ kcl_cli_configs: tAssertEQ(t, x.KFilenameList[1], filepath.Join(pwd, "sub", "sub_main.k")) tAssertEQ(t, x.KFilenameList[2], filepath.Join(pwd, "file2.k")) tAssertEQ(t, x.KFilenameList[3], filepath.Join(pwd, "..", "base", "base.k")) + tAssertEQ(t, x.ExternalPkgs[1].PkgName, "k8s") + tAssertEQ(t, x.ExternalPkgs[1].PkgPath, "../vendor/k8s") } func tAssertEQ(t *testing.T, x, y interface{}) { diff --git a/pkg/settings/a_test_windows.go b/pkg/settings/a_test_windows.go index 2d636b66..62068003 100644 --- a/pkg/settings/a_test_windows.go +++ b/pkg/settings/a_test_windows.go @@ -16,6 +16,8 @@ kcl_cli_configs: - ${KCL_MOD}/file2.k - ../../base/base.k disable_none: false + package_maps: + k8s: ..\\vendor\\k8s ` f, err := LoadFile("./sub/settings.yaml", []byte(s)) if err != nil { @@ -30,6 +32,8 @@ kcl_cli_configs: tAssertEQ(t, x.KFilenameList[1], filepath.Join(pwd, "sub", "sub_main.k")) tAssertEQ(t, x.KFilenameList[2], filepath.Join(pwd, "file2.k")) tAssertEQ(t, x.KFilenameList[3], filepath.Join(pwd, "..", "base", "base.k")) + tAssertEQ(t, x.ExternalPkgs[1].PkgName, "k8s") + tAssertEQ(t, x.ExternalPkgs[1].PkgPath, filepath.Join(pwd, "..", "vendor", "k8s")) } func tAssertEQ(t *testing.T, x, y interface{}) { diff --git a/pkg/settings/utils_settings_yaml.go b/pkg/settings/utils_settings_yaml.go index 546454d3..ed544d99 100644 --- a/pkg/settings/utils_settings_yaml.go +++ b/pkg/settings/utils_settings_yaml.go @@ -29,11 +29,13 @@ type ConfigStruct struct { Overrides []string `yaml:"overrides"` PathSelector []string `yaml:"path_selector"` - StrictRangeCheck bool `yaml:"strict_range_check"` - DisableNone bool `yaml:"disable_none"` - Verbose int `yaml:"verbose"` - Debug bool `yaml:"debug"` + StrictRangeCheck bool `yaml:"strict_range_check"` + DisableNone bool `yaml:"disable_none"` + Verbose int `yaml:"verbose"` + Debug bool `yaml:"debug"` + PackageMaps map[string]string `yaml:"package_maps"` } + type KeyValueStruct struct { Key string `yaml:"key"` Value interface{} `yaml:"value"` @@ -174,6 +176,15 @@ func (settings *SettingsFile) To_ExecProgram_Args() *gpyrpc.ExecProgram_Args { }) } + // kcl -E k8s=../vendor/k8s + for name, path := range settings.Config.PackageMaps { + externalPkg := gpyrpc.CmdExternalPkgSpec{ + PkgName: name, + PkgPath: path, + } + args.ExternalPkgs = append(args.ExternalPkgs, &externalPkg) + } + return args }