-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Attempt of using generics instead of
interface{}
in component's
configuration handling and creation. Drafted solution lives for now in `generic` package and example usage (with some fake test component) is demonstrated in `component_test.go`.
- Loading branch information
Showing
4 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package generic | ||
|
||
import ( | ||
"github.com/hashicorp/hcl/v2" | ||
"github.com/hashicorp/hcl/v2/gohcl" | ||
) | ||
|
||
// Implemented by each component, like kinesis target | ||
type ConfigurableComponent[CONFIG any, COMPONENT any] interface { | ||
DefaultConfig() *CONFIG | ||
FromConfig(config *CONFIG) (*COMPONENT, error) | ||
} | ||
|
||
// Also implemented by each component, but generic type parameter 'OUT' specifies interface requested by consumer, e.g. 'Target' interface. | ||
type ProviderOf[OUT any] interface { | ||
Provide(input hcl.Body, ctx *hcl.EvalContext) (OUT, error) | ||
} | ||
|
||
func createComponent[CONFIG any, COMPONENT any](component ConfigurableComponent[CONFIG, COMPONENT], input hcl.Body, ctx *hcl.EvalContext) (*COMPONENT, error) { | ||
config := component.DefaultConfig() | ||
decode[CONFIG](input, ctx, config) | ||
readyComponent, err := component.FromConfig(config) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
return readyComponent, nil | ||
} | ||
|
||
func decode[CONFIG any](input hcl.Body, evalContext *hcl.EvalContext, configuration *CONFIG) error { | ||
if input == nil { | ||
return nil | ||
} | ||
|
||
if configuration == nil { | ||
return nil | ||
} | ||
|
||
diag := gohcl.DecodeBody(input, evalContext, configuration) | ||
if len(diag) > 0 { | ||
return diag | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package generic | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/hcl/v2" | ||
"github.com/hashicorp/hcl/v2/hclparse" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var supportedTargets = map[string]ProviderOf[Target]{ | ||
"testTarget": &TestProvider{}, | ||
} | ||
|
||
func Test_CreatingTarget(t *testing.T) { | ||
assert := assert.New(t) | ||
hclFile, _ := hclparse.NewParser().ParseHCL([]byte(`test_string = "ateststring"`), "placeholder.hcl") | ||
input := hclFile.Body | ||
|
||
target, _ := supportedTargets["testTarget"].Provide(input, &hcl.EvalContext{}) | ||
|
||
output := target.Write("Test string") | ||
|
||
assert.Equal("Written: Test string. This is value in config - ateststring", output) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package generic | ||
|
||
import ( | ||
"github.com/hashicorp/hcl/v2" | ||
) | ||
|
||
type myComponentConfig struct { | ||
Test string `hcl:"test_string"` | ||
} | ||
|
||
type myComponent struct { | ||
config myComponentConfig | ||
} | ||
|
||
// This one is used by component's clients. If you want ot use component, just use this struct | ||
type TestProvider struct{} | ||
|
||
// Return component as interface, here as 'Target' | ||
func (cc TestProvider) Provide(input hcl.Body, ctx *hcl.EvalContext) (Target, error) { | ||
return createComponent[myComponentConfig, myComponent](cc, input, ctx) | ||
} | ||
|
||
func (TestProvider) DefaultConfig() *myComponentConfig { | ||
Check failure on line 23 in config/generic/example_component.go GitHub Actions / Compile & Test (1.22, ubuntu-latest)
|
||
return &myComponentConfig{"some default value"} | ||
} | ||
|
||
func (TestProvider) FromConfig(config *myComponentConfig) (*myComponent, error) { | ||
Check failure on line 27 in config/generic/example_component.go GitHub Actions / Compile & Test (1.22, ubuntu-latest)
|
||
return &myComponent{*config}, nil | ||
} | ||
|
||
// Implementing desired interfaces, like 'Target' | ||
func (mc myComponent) Write(input string) string { | ||
return "Written: " + input + ". This is value in config - " + mc.config.Test | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package generic | ||
|
||
type Target interface { | ||
Write(input string) string | ||
} |