Skip to content

Commit

Permalink
load config from string
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinForReal committed Nov 13, 2024
1 parent 829ef0d commit c31efe2
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
8 changes: 4 additions & 4 deletions pkg/azclient/configloader/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,22 @@ func Load[Type any](ctx context.Context, secretLoaderConfig *K8sSecretLoaderConf
var err error
if fileLoaderConfig != nil {
loadConfigloader := newEmptyLoader[ConfigMergeConfig](&ConfigMergeConfig{CloudConfigType: CloudConfigTypeMerge})
loadConfigloader = newFileLoader(fileLoaderConfig.FilePath, loadConfigloader, newYamlByteLoader[ConfigMergeConfig])
loadConfigloader = newFileLoader(fileLoaderConfig.FilePath, loadConfigloader, NewYamlByteLoader[ConfigMergeConfig])
//by default the config load type is merge
loadConfig, err = loadConfigloader.Load(ctx)
if err != nil {
return nil, err
}
configloader = newFileLoader(fileLoaderConfig.FilePath, nil, newYamlByteLoader[Type])
configloader = newFileLoader(fileLoaderConfig.FilePath, nil, NewYamlByteLoader[Type])
if strings.EqualFold(string(loadConfig.CloudConfigType), string(CloudConfigTypeFile)) {
return configloader.Load(ctx)
}
}
if secretLoaderConfig != nil {
if loadConfig != nil && strings.EqualFold(string(loadConfig.CloudConfigType), string(CloudConfigTypeSecret)) {
configloader = newK8sSecretLoader(&secretLoaderConfig.K8sSecretConfig, secretLoaderConfig.KubeClient, nil, newYamlByteLoader[Type])
configloader = newK8sSecretLoader(&secretLoaderConfig.K8sSecretConfig, secretLoaderConfig.KubeClient, nil, NewYamlByteLoader[Type])
} else {
configloader = newK8sSecretLoader(&secretLoaderConfig.K8sSecretConfig, secretLoaderConfig.KubeClient, configloader, newYamlByteLoader[Type])
configloader = newK8sSecretLoader(&secretLoaderConfig.K8sSecretConfig, secretLoaderConfig.KubeClient, configloader, NewYamlByteLoader[Type])
}
}
return configloader.Load(ctx)
Expand Down
4 changes: 2 additions & 2 deletions pkg/azclient/configloader/loader_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ import (
var _ = Describe("LoaderFile", func() {
When("invalid config content is provided", func() {
It("should return error", func() {
loader := newFileLoader[TestConfig]("error", nil, newYamlByteLoader[TestConfig])
loader := newFileLoader[TestConfig]("error", nil, NewYamlByteLoader[TestConfig])
config, err := loader.Load(nil)
Expect(err).NotTo(BeNil())
Expect(config).To(BeNil())
})
})
When("valid config content is provided", func() {
It("should return error", func() {
loader := newFileLoader[TestConfig]("testdata/azure.json", nil, newYamlByteLoader[TestConfig])
loader := newFileLoader[TestConfig]("testdata/azure.json", nil, NewYamlByteLoader[TestConfig])
config, err := loader.Load(nil)
Expect(err).To(BeNil())
Expect(config).NotTo(BeNil())
Expand Down
12 changes: 6 additions & 6 deletions pkg/azclient/configloader/loader_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ import (
"sigs.k8s.io/yaml"
)

// yamlByteLoader is a FactoryConfigLoader that loads a YAML file from a byte array.
type yamlByteLoader[Type any] struct {
// YamlByteLoader is a FactoryConfigLoader that loads a YAML file from a byte array.
type YamlByteLoader[Type any] struct {
content []byte
configLoader[Type]
}

// Load loads the YAML file from the byte array and returns the client factory config.
func (s *yamlByteLoader[Type]) Load(ctx context.Context) (*Type, error) {
func (s *YamlByteLoader[Type]) Load(ctx context.Context) (*Type, error) {
if s.configLoader == nil {
s.configLoader = newEmptyLoader[Type](nil)
}
Expand All @@ -45,9 +45,9 @@ func (s *yamlByteLoader[Type]) Load(ctx context.Context) (*Type, error) {
return config, nil
}

// newYamlByteLoader creates a YamlByteLoader with the specified content and loader.
func newYamlByteLoader[Type any](content []byte, loader configLoader[Type]) configLoader[Type] {
return &yamlByteLoader[Type]{
// NewYamlByteLoader creates a YamlByteLoader with the specified content and loader.
func NewYamlByteLoader[Type any](content []byte, loader configLoader[Type]) configLoader[Type] {
return &YamlByteLoader[Type]{
content: content,
configLoader: loader,
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/azclient/configloader/loader_yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ import (
var _ = Describe("LoaderYaml", func() {
When("invalid config content is provided", func() {
It("should return error", func() {
loader := newYamlByteLoader[TestConfig]([]byte("error"), nil)
loader := NewYamlByteLoader[TestConfig]([]byte("error"), nil)
config, err := loader.Load(context.Background())
Expect(err).NotTo(BeNil())
Expect(config).To(BeNil())
})
})
When("valid config content is provided", func() {
It("should return error", func() {
loader := newYamlByteLoader[TestConfig]([]byte(`{"value":"123"}`), nil)
loader := NewYamlByteLoader[TestConfig]([]byte(`{"value":"123"}`), nil)
config, err := loader.Load(context.Background())
Expect(err).To(BeNil())
Expect(config).NotTo(BeNil())
Expand All @@ -43,7 +43,7 @@ var _ = Describe("LoaderYaml", func() {
})
When("valid config content is provided with default config", func() {
It("should return error", func() {
loader := newYamlByteLoader[TestConfig]([]byte(`{"value":"123"}`), newFileLoader[TestConfig]("wrong", nil, newYamlByteLoader[TestConfig]))
loader := NewYamlByteLoader[TestConfig]([]byte(`{"value":"123"}`), newFileLoader[TestConfig]("wrong", nil, NewYamlByteLoader[TestConfig]))
config, err := loader.Load(context.Background())
Expect(err).NotTo(BeNil())
Expect(config).To(BeNil())
Expand Down

0 comments on commit c31efe2

Please sign in to comment.