Skip to content

Commit

Permalink
replace all empty interfaces with any (open-telemetry#1703)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmcollins authored Jun 21, 2022
1 parent d991758 commit da3ca1f
Show file tree
Hide file tree
Showing 51 changed files with 416 additions and 416 deletions.
4 changes: 2 additions & 2 deletions cmd/translatesfx/translatesfx/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ func translateConfig(fname, wd string) (configYaml string, warnings []error) {
return header + string(bytes), warnings
}

func loadCfg(fname string) (interface{}, error) {
func loadCfg(fname string) (any, error) {
bytes, err := os.ReadFile(fname)
if err != nil {
return nil, err
}
var orig interface{}
var orig any
err = yaml.UnmarshalStrict(bytes, &orig)
return orig, err
}
2 changes: 1 addition & 1 deletion cmd/translatesfx/translatesfx/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestTranslateConfig(t *testing.T) {
assert.Nil(t, w)
expected, err := os.ReadFile("testdata/otel-e2e-expected.yaml")
require.NoError(t, err)
var translatedV, expectedV interface{}
var translatedV, expectedV any
err = yaml.Unmarshal([]byte(translated), &translatedV)
require.NoError(t, err)
err = yaml.Unmarshal(expected, &expectedV)
Expand Down
8 changes: 4 additions & 4 deletions cmd/translatesfx/translatesfx/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package translatesfx
import "fmt"

type component struct {
attrs map[string]interface{}
attrs map[string]any
// baseName has the baseName for what will eventually be the key to this
// component in the config -- e.g. "smartagent/sql" which might end up being
// "smartagent/sql/0"
Expand All @@ -28,7 +28,7 @@ type componentCollection []component

// toComponentMap turns a componentCollection into a map such that its keys have a `/<number>`
// suffix for any components with colliding provisional keys
func (cc componentCollection) toComponentMap() map[string]map[string]interface{} {
func (cc componentCollection) toComponentMap() map[string]map[string]any {
keyCounts := map[string]int{}
hasMultiKeys := map[string]struct{}{}
for _, c := range cc {
Expand All @@ -39,7 +39,7 @@ func (cc componentCollection) toComponentMap() map[string]map[string]interface{}
keyCounts[c.baseName] = count + 1
}
keyCounts = map[string]int{}
out := map[string]map[string]interface{}{}
out := map[string]map[string]any{}
for _, c := range cc {
_, found := hasMultiKeys[c.baseName]
key := c.baseName
Expand All @@ -53,7 +53,7 @@ func (cc componentCollection) toComponentMap() map[string]map[string]interface{}
return out
}

func saMonitorToStandardReceiver(monitor map[string]interface{}) component {
func saMonitorToStandardReceiver(monitor map[string]any) component {
if excludes, ok := monitor[metricsToExclude]; ok {
delete(monitor, metricsToExclude)
monitor["datapointsToExclude"] = excludes
Expand Down
48 changes: 24 additions & 24 deletions cmd/translatesfx/translatesfx/directive.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const (
directiveSourceVault
)

func parseDirective(m map[interface{}]interface{}, wd string) (out directive, isDirective bool, err error) {
func parseDirective(m map[any]any, wd string) (out directive, isDirective bool, err error) {
fromRaw, ok := m["#from"]
if !ok {
return
Expand Down Expand Up @@ -103,15 +103,15 @@ func strToSource(s string) source {
return directiveSourceUnknown
}

func parseFlatten(m map[interface{}]interface{}) (bool, error) {
func parseFlatten(m map[any]any) (bool, error) {
return parseField(m, "flatten")
}

func parseOptional(m map[interface{}]interface{}) (bool, error) {
func parseOptional(m map[any]any) (bool, error) {
return parseField(m, "optional")
}

func parseField(m map[interface{}]interface{}, field string) (bool, error) {
func parseField(m map[any]any, field string) (bool, error) {
v, ok := m[field]
if !ok {
return false, nil
Expand All @@ -123,7 +123,7 @@ func parseField(m map[interface{}]interface{}, field string) (bool, error) {
return out, nil
}

func parseDefault(m map[interface{}]interface{}) (string, error) {
func parseDefault(m map[any]any) (string, error) {
rawDefault, ok := m["default"]
if !ok {
return "", nil
Expand All @@ -144,7 +144,7 @@ type directive struct {
optional bool
}

func (d directive) render(forceExpand bool, vaultPaths *[]string) (interface{}, error) {
func (d directive) render(forceExpand bool, vaultPaths *[]string) (any, error) {
switch d.fromType {
case directiveSourceFile:
return d.handleFileType(forceExpand)
Expand All @@ -163,7 +163,7 @@ func (d directive) render(forceExpand bool, vaultPaths *[]string) (interface{},
}
}

func (d directive) expandEnv() (interface{}, error) {
func (d directive) expandEnv() (any, error) {
return fmt.Sprintf("${%s}", d.fromPath), nil
}

Expand All @@ -175,7 +175,7 @@ func directiveSource(from string) string {
return from[:idx]
}

func (d directive) handleFileType(forceExpand bool) (interface{}, error) {
func (d directive) handleFileType(forceExpand bool) (any, error) {
// configsource doesn't handle flatten, glob, or default values at this time, so
// we inline the value if any of those are specified in the #from directive
if forceExpand || d.flatten || hasGlob(d.fromPath) || d.defaultV != "" {
Expand All @@ -189,7 +189,7 @@ func hasGlob(path string) bool {
return strings.ContainsAny(path, "*?[]")
}

func (d directive) expandFiles() (interface{}, error) {
func (d directive) expandFiles() (any, error) {
path := resolvePath(d.fromPath, d.wd)
filepaths, err := filepath.Glob(path)
if err != nil {
Expand All @@ -206,7 +206,7 @@ func (d directive) expandFiles() (interface{}, error) {
}
}

var items []interface{}
var items []any
for _, p := range filepaths {
unmarshaled, err := unmarshal(p)
if err != nil {
Expand All @@ -217,15 +217,15 @@ func (d directive) expandFiles() (interface{}, error) {
return merge(items)
}

func (d directive) expandZK() (interface{}, error) {
func (d directive) expandZK() (any, error) {
return fmt.Sprintf("${zookeeper:%s}", d.fromPath), nil
}

func (d directive) expandEtcd2() (interface{}, error) {
func (d directive) expandEtcd2() (any, error) {
return fmt.Sprintf("${etcd2:%s}", d.fromPath), nil
}

func (d directive) expandVault(vaultPaths *[]string) (interface{}, error) {
func (d directive) expandVault(vaultPaths *[]string) (any, error) {
path, keys := parseVaultPath(d.fromPath)
idx, found := indexOf(*vaultPaths, path)
if !found {
Expand Down Expand Up @@ -258,39 +258,39 @@ func resolvePath(path, wd string) string {
return filepath.Join(wd, path)
}

func unmarshal(path string) (interface{}, error) {
func unmarshal(path string) (any, error) {
bytes, err := os.ReadFile(path)
if err != nil {
return nil, err
}
var replacement interface{}
var replacement any
err = yaml.UnmarshalStrict(bytes, &replacement)
if err != nil {
return nil, err
}
return replacement, nil
}

func merge(items []interface{}) (interface{}, error) {
func merge(items []any) (any, error) {
switch len(items) {
case 0:
return nil, nil
case 1:
return items[0], nil
}
switch items[0].(type) {
case []interface{}:
case []any:
return mergeSlices(items)
case map[interface{}]interface{}:
case map[any]any:
return mergeMaps(items)
}
return nil, fmt.Errorf("unable to merge: %v", items)
}

func mergeSlices(items []interface{}) (interface{}, error) {
var out []interface{}
func mergeSlices(items []any) (any, error) {
var out []any
for _, item := range items {
l, ok := item.([]interface{})
l, ok := item.([]any)
if !ok {
return nil, fmt.Errorf("mergeSlices: type coersion failed for item %v in items %v", item, items)
}
Expand All @@ -299,10 +299,10 @@ func mergeSlices(items []interface{}) (interface{}, error) {
return out, nil
}

func mergeMaps(items []interface{}) (interface{}, error) {
out := map[interface{}]interface{}{}
func mergeMaps(items []any) (any, error) {
out := map[any]any{}
for _, item := range items {
m, ok := item.(map[interface{}]interface{})
m, ok := item.(map[any]any)
if !ok {
return nil, fmt.Errorf("mergeMaps: type coersion failed for item %v in items %v", item, items)
}
Expand Down
38 changes: 19 additions & 19 deletions cmd/translatesfx/translatesfx/directive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
)

func TestParseDirective_Empty(t *testing.T) {
_, isDirective, err := parseDirective(map[interface{}]interface{}{}, "")
_, isDirective, err := parseDirective(map[any]any{}, "")
require.NoError(t, err)
require.False(t, isDirective)
}
Expand All @@ -47,7 +47,7 @@ func TestParseFrom_Unknown(t *testing.T) {
}

func TestParseDirective(t *testing.T) {
d, _, err := parseDirective(map[interface{}]interface{}{
d, _, err := parseDirective(map[any]any{
"#from": "/some/path",
"flatten": true,
"default": "abc123",
Expand All @@ -61,7 +61,7 @@ func TestParseDirective(t *testing.T) {
}

func TestRender_ConfigSource(t *testing.T) {
d, _, err := parseDirective(map[interface{}]interface{}{
d, _, err := parseDirective(map[any]any{
"#from": "testdata/token",
}, "")
require.NoError(t, err)
Expand All @@ -71,7 +71,7 @@ func TestRender_ConfigSource(t *testing.T) {
}

func TestRender_FileExpansion_Simple(t *testing.T) {
d, _, err := parseDirective(map[interface{}]interface{}{
d, _, err := parseDirective(map[any]any{
"#from": "testdata/token",
"default": "foo", // specify a default to force file expansion
}, "")
Expand All @@ -82,21 +82,21 @@ func TestRender_FileExpansion_Simple(t *testing.T) {
}

func TestRender_FileExpansion_Map(t *testing.T) {
d, _, err := parseDirective(map[interface{}]interface{}{
d, _, err := parseDirective(map[any]any{
"#from": "testdata/map.yaml",
"default": "foo", // specify a default to force file expansion
}, "")
require.NoError(t, err)
v, err := d.render(false, nil)
require.NoError(t, err)
assert.Equal(t, map[interface{}]interface{}{
assert.Equal(t, map[any]any{
"foo": "bar",
"baz": "glarch",
}, v)
}

func TestRender_FileExpansion_Wildcard(t *testing.T) {
d, _, err := parseDirective(map[interface{}]interface{}{
d, _, err := parseDirective(map[any]any{
"#from": "testdata/cfgs/map*.yaml", // asterisk forces file expansion
}, "")
require.NoError(t, err)
Expand All @@ -106,7 +106,7 @@ func TestRender_FileExpansion_Wildcard(t *testing.T) {
}

func TestRender_FileExpansion_MissingNonOptionalFile(t *testing.T) {
d, _, err := parseDirective(map[interface{}]interface{}{
d, _, err := parseDirective(map[any]any{
"#from": "testdata/missing",
"default": "foo", // specify a default to force file expansion
}, "")
Expand All @@ -117,7 +117,7 @@ func TestRender_FileExpansion_MissingNonOptionalFile(t *testing.T) {
}

func TestDirective_ZK(t *testing.T) {
d, _, err := parseDirective(map[interface{}]interface{}{
d, _, err := parseDirective(map[any]any{
"#from": "zk:/foo/bar",
}, "")
require.NoError(t, err)
Expand All @@ -127,7 +127,7 @@ func TestDirective_ZK(t *testing.T) {
}

func TestDirective_Vault(t *testing.T) {
d, _, err := parseDirective(map[interface{}]interface{}{
d, _, err := parseDirective(map[any]any{
"#from": "vault:/foo/bar[aaa]",
}, "")
require.NoError(t, err)
Expand All @@ -137,7 +137,7 @@ func TestDirective_Vault(t *testing.T) {
assert.Equal(t, "${vault/0:aaa}", rendered)
assert.Equal(t, []string{"/foo/bar"}, vaultPaths)

d, _, err = parseDirective(map[interface{}]interface{}{
d, _, err = parseDirective(map[any]any{
"#from": "vault:/foo/bar[bbb]",
}, "")
require.NoError(t, err)
Expand All @@ -146,7 +146,7 @@ func TestDirective_Vault(t *testing.T) {
assert.Equal(t, "${vault/0:bbb}", rendered)
assert.Equal(t, []string{"/foo/bar"}, vaultPaths)

d, _, err = parseDirective(map[interface{}]interface{}{
d, _, err = parseDirective(map[any]any{
"#from": "vault:/foo/baz[ccc]",
}, "")
require.NoError(t, err)
Expand All @@ -162,19 +162,19 @@ func TestDirectiveSource(t *testing.T) {
}

func TestMerge_Slices(t *testing.T) {
merged, err := merge([]interface{}{
[]interface{}{"aaa"}, []interface{}{"bbb"},
merged, err := merge([]any{
[]any{"aaa"}, []any{"bbb"},
})
require.NoError(t, err)
assert.Equal(t, []interface{}{"aaa", "bbb"}, merged)
assert.Equal(t, []any{"aaa", "bbb"}, merged)
}

func TestMerge_Maps(t *testing.T) {
m1 := map[interface{}]interface{}{"aaa": 111}
m2 := map[interface{}]interface{}{"bbb": 222}
merged, err := merge([]interface{}{m1, m2})
m1 := map[any]any{"aaa": 111}
m2 := map[any]any{"bbb": 222}
merged, err := merge([]any{m1, m2})
require.NoError(t, err)
assert.Equal(t, map[interface{}]interface{}{
assert.Equal(t, map[any]any{
"aaa": 111,
"bbb": 222,
}, merged)
Expand Down
6 changes: 3 additions & 3 deletions cmd/translatesfx/translatesfx/discovery_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ var k8sIDs = map[string]string{
"kubernetes_pod_uid": otelRulePodUID,
}

func discoveryRuleToRCRule(dr string, observers []interface{}) (rcRule string, err error) {
func discoveryRuleToRCRule(dr string, observers []any) (rcRule string, err error) {
dr = strings.ReplaceAll(dr, "=~", " matches ")

tree, err := parser.Parse(dr)
Expand Down Expand Up @@ -204,14 +204,14 @@ func translateTree(node ast.Node, idMap map[string]string) error {
// guessRuleType is for rules that don't have the required e.g. 'type == "foo"'.
// We guess the type is "hostport" if there is one `host` observer and "port"
// if there is one `k8s-api` observer.
func guessRuleType(observers []interface{}) (string, error) {
func guessRuleType(observers []any) (string, error) {
if observers == nil {
return "", errors.New("guessRuleType: no observers")
}
if len(observers) != 1 {
return "", fmt.Errorf("guessRuleType: too many observers: %v", observers)
}
obs := observers[0].(map[interface{}]interface{})
obs := observers[0].(map[any]any)
switch obs["type"] {
case "host":
return "hostport", nil
Expand Down
Loading

0 comments on commit da3ca1f

Please sign in to comment.