-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
translatesfx: factor out
directive
type (#455)
* translatesfx: factor out `directive` type * Address PR feedback
- Loading branch information
1 parent
8b8dc56
commit f68b14d
Showing
4 changed files
with
253 additions
and
123 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,137 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package translatesfx | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type source int | ||
|
||
const ( | ||
directiveSourceUnknown source = iota | ||
directiveSourceFile | ||
directiveSourceEnv | ||
directiveSourceZK | ||
directiveSourceEtcd2 | ||
directiveSourceConsul | ||
directiveSourceVault | ||
) | ||
|
||
type directive struct { | ||
fromPath string | ||
defaultV string | ||
fromType source | ||
isDirective bool | ||
flatten bool | ||
optional bool | ||
} | ||
|
||
func parseDirective(m map[interface{}]interface{}) (out directive, err error) { | ||
fromRaw, ok := m["#from"] | ||
if !ok { | ||
return | ||
} | ||
out.isDirective = true | ||
from, ok := fromRaw.(string) | ||
if !ok { | ||
return out, fmt.Errorf("error parsing from %v", m) | ||
} | ||
|
||
out.fromType, out.fromPath, err = parseFrom(from) | ||
if err != nil { | ||
return | ||
} | ||
|
||
out.flatten, err = parseFlatten(m) | ||
if err != nil { | ||
return | ||
} | ||
|
||
out.defaultV, err = parseDefault(m) | ||
if err != nil { | ||
return | ||
} | ||
|
||
out.optional, err = parseOptional(m) | ||
if err != nil { | ||
return | ||
} | ||
|
||
return | ||
} | ||
|
||
func parseFrom(from string) (source, string, error) { | ||
idx := strings.Index(from, ":") | ||
if idx == -1 { | ||
return directiveSourceFile, from, nil | ||
} | ||
s := strToSource(from[:idx]) | ||
if s == directiveSourceUnknown { | ||
return s, "", fmt.Errorf("#from fromType type unknown: %s", from) | ||
} | ||
return s, from[idx+1:], nil | ||
} | ||
|
||
func strToSource(s string) source { | ||
switch s { | ||
case "", "file": | ||
return directiveSourceFile | ||
case "env": | ||
return directiveSourceEnv | ||
case "zookeeper", "zk": | ||
return directiveSourceZK | ||
case "etcd2": | ||
return directiveSourceEtcd2 | ||
case "consul": | ||
return directiveSourceConsul | ||
case "vault": | ||
return directiveSourceVault | ||
} | ||
return directiveSourceUnknown | ||
} | ||
|
||
func parseFlatten(m map[interface{}]interface{}) (bool, error) { | ||
return parseField(m, "flatten") | ||
} | ||
|
||
func parseOptional(m map[interface{}]interface{}) (bool, error) { | ||
return parseField(m, "optional") | ||
} | ||
|
||
func parseField(m map[interface{}]interface{}, field string) (bool, error) { | ||
v, ok := m[field] | ||
if !ok { | ||
return false, nil | ||
} | ||
out, ok := v.(bool) | ||
if !ok { | ||
return false, fmt.Errorf(`unable to parse field %q: %v`, field, m) | ||
} | ||
return out, nil | ||
} | ||
|
||
func parseDefault(m map[interface{}]interface{}) (string, error) { | ||
rawDefault, ok := m["default"] | ||
if !ok { | ||
return "", nil | ||
} | ||
out, ok := rawDefault.(string) | ||
if !ok { | ||
return "", fmt.Errorf("unable to parse default value: %v", m) | ||
} | ||
return out, 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,62 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package translatesfx | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestParseDirective_Empty(t *testing.T) { | ||
d, err := parseDirective(map[interface{}]interface{}{}) | ||
require.NoError(t, err) | ||
assert.False(t, d.isDirective) | ||
} | ||
|
||
func TestParseDirective(t *testing.T) { | ||
d, err := parseDirective(map[interface{}]interface{}{ | ||
"#from": "/some/path", | ||
"flatten": true, | ||
"default": "abc123", | ||
"optional": true, | ||
}) | ||
require.NoError(t, err) | ||
require.True(t, d.isDirective) | ||
assert.Equal(t, "/some/path", d.fromPath) | ||
assert.True(t, d.flatten) | ||
assert.Equal(t, "abc123", d.defaultV) | ||
assert.True(t, d.optional) | ||
} | ||
|
||
func TestParseFrom_Simple(t *testing.T) { | ||
source, target, err := parseFrom("/some/path") | ||
require.NoError(t, err) | ||
assert.Equal(t, directiveSourceFile, source) | ||
assert.Equal(t, "/some/path", target) | ||
} | ||
|
||
func TestParseFrom_Env(t *testing.T) { | ||
source, target, err := parseFrom("env:FOO") | ||
require.NoError(t, err) | ||
assert.Equal(t, directiveSourceEnv, source) | ||
assert.Equal(t, "FOO", target) | ||
} | ||
|
||
func TestParseFrom_Unknown(t *testing.T) { | ||
_, _, err := parseFrom("xyz:FOO") | ||
require.Error(t, err) | ||
} |
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
Oops, something went wrong.