-
Notifications
You must be signed in to change notification settings - Fork 3k
[pkg/ottl] Add ParseKeyValue function #31035
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
evan-bradley
merged 9 commits into
open-telemetry:main
from
observIQ:ottl/key-value-parser
Feb 15, 2024
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
479838a
write ottl key value func
dpaasman00 e36dbd7
add e2e tests, documentation, and changelog entry
dpaasman00 885a566
internal feedback
dpaasman00 967289c
name changes & remove inQuotes
dpaasman00 2ecf32d
update parsing to reflect changes made to stanza keyvalue parser
dpaasman00 6b0a26d
add license
dpaasman00 0dbc0d9
put pair parsing into internal pkg, add some more unit tests, update …
dpaasman00 ca573d9
update how empty string is handled
dpaasman00 e1bbb38
move key-value split into parseutils
dpaasman00 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,27 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: enhancement | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: pkg/ottl | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Add `ParseKeyValue` function for parsing key value pairs from a target string | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [30998] | ||
|
||
# (Optional) One or more lines of additional information to render under the primary note. | ||
# These lines will be padded with 2 spaces and then inserted directly into the document. | ||
# Use pipe (|) for multiline entries. | ||
subtext: | ||
|
||
# If your change doesn't affect end users or the exported elements of any package, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [] |
This file contains hidden or 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,4 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package parseutils // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/parseutils" |
This file contains hidden or 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,14 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package parseutils | ||
|
||
import ( | ||
"testing" | ||
|
||
"go.uber.org/goleak" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
goleak.VerifyTestMain(m) | ||
} |
This file contains hidden or 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,49 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package parseutils | ||
|
||
import "fmt" | ||
|
||
// SplitString will split the input on the delimiter and return the resulting slice while respecting quotes. Outer quotes are stripped. | ||
// Use in place of `strings.Split` when quotes need to be respected. | ||
// Requires `delimiter` not be an empty string | ||
func SplitString(input, delimiter string) ([]string, error) { | ||
var result []string | ||
current := "" | ||
delimiterLength := len(delimiter) | ||
quoteChar := "" // "" means we are not in quotes | ||
|
||
for i := 0; i < len(input); i++ { | ||
if quoteChar == "" && i+delimiterLength <= len(input) && input[i:i+delimiterLength] == delimiter { // delimiter | ||
if current == "" { // leading || trailing delimiter; ignore | ||
i += delimiterLength - 1 | ||
continue | ||
} | ||
result = append(result, current) | ||
current = "" | ||
i += delimiterLength - 1 | ||
continue | ||
} | ||
|
||
if quoteChar == "" && (input[i] == '"' || input[i] == '\'') { // start of quote | ||
quoteChar = string(input[i]) | ||
continue | ||
} | ||
if string(input[i]) == quoteChar { // end of quote | ||
quoteChar = "" | ||
continue | ||
} | ||
|
||
current += string(input[i]) | ||
} | ||
|
||
if quoteChar != "" { // check for closed quotes | ||
return nil, fmt.Errorf("never reached the end of a quoted value") | ||
} | ||
if current != "" { // avoid adding empty value bc of a trailing delimiter | ||
return append(result, current), nil | ||
} | ||
|
||
return result, nil | ||
} |
This file contains hidden or 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,186 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package parseutils | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_SplitString(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
input string | ||
delimiter string | ||
expected []string | ||
expectedErr error | ||
}{ | ||
{ | ||
name: "simple", | ||
input: "a b c", | ||
delimiter: " ", | ||
expected: []string{ | ||
"a", | ||
"b", | ||
"c", | ||
}, | ||
}, | ||
{ | ||
name: "single quotes", | ||
input: "a 'b c d'", | ||
delimiter: " ", | ||
expected: []string{ | ||
"a", | ||
"b c d", | ||
}, | ||
}, | ||
{ | ||
name: "double quotes", | ||
input: `a " b c " d`, | ||
delimiter: " ", | ||
expected: []string{ | ||
"a", | ||
" b c ", | ||
"d", | ||
}, | ||
}, | ||
{ | ||
name: "multi-char delimiter", | ||
input: "abc!@! def !@! g", | ||
delimiter: "!@!", | ||
expected: []string{ | ||
"abc", | ||
" def ", | ||
" g", | ||
}, | ||
}, | ||
{ | ||
name: "leading and trailing delimiters", | ||
input: " name=ottl func=key_value hello=world ", | ||
delimiter: " ", | ||
expected: []string{ | ||
"name=ottl", | ||
"func=key_value", | ||
"hello=world", | ||
}, | ||
}, | ||
{ | ||
name: "embedded double quotes in single quoted value", | ||
input: `ab c='this is a "co ol" value'`, | ||
delimiter: " ", | ||
expected: []string{ | ||
"ab", | ||
`c=this is a "co ol" value`, | ||
}, | ||
}, | ||
{ | ||
name: "embedded double quotes end single quoted value", | ||
input: `ab c='this is a "co ol"'`, | ||
delimiter: " ", | ||
expected: []string{ | ||
"ab", | ||
`c=this is a "co ol"`, | ||
}, | ||
}, | ||
{ | ||
name: "quoted values include whitespace", | ||
input: `name=" ottl " func=" key_ value"`, | ||
delimiter: " ", | ||
expected: []string{ | ||
"name= ottl ", | ||
"func= key_ value", | ||
}, | ||
}, | ||
{ | ||
name: "delimiter longer than input", | ||
input: "abc", | ||
delimiter: "aaaa", | ||
expected: []string{ | ||
"abc", | ||
}, | ||
}, | ||
{ | ||
name: "delimiter not found", | ||
input: "a b c", | ||
delimiter: "!", | ||
expected: []string{ | ||
"a b c", | ||
}, | ||
}, | ||
{ | ||
name: "newlines in input", | ||
input: `a | ||
b | ||
c`, | ||
delimiter: " ", | ||
expected: []string{ | ||
"a\nb\nc", | ||
}, | ||
}, | ||
{ | ||
name: "newline delimiter", | ||
input: `a b c | ||
d e f | ||
g | ||
h`, | ||
delimiter: "\n", | ||
expected: []string{ | ||
"a b c", | ||
"d e f", | ||
"g ", | ||
"h", | ||
}, | ||
}, | ||
{ | ||
name: "empty input", | ||
input: "", | ||
delimiter: " ", | ||
expected: nil, | ||
}, | ||
{ | ||
name: "equal input and delimiter", | ||
input: "abc", | ||
delimiter: "abc", | ||
expected: nil, | ||
}, | ||
{ | ||
name: "unclosed quotes", | ||
input: "a 'b c", | ||
delimiter: " ", | ||
expectedErr: fmt.Errorf("never reached the end of a quoted value"), | ||
}, | ||
{ | ||
name: "mismatched quotes", | ||
input: `a 'b c' "d '`, | ||
delimiter: " ", | ||
expectedErr: fmt.Errorf("never reached the end of a quoted value"), | ||
}, | ||
{ | ||
name: "tab delimiters", | ||
input: "a b c", | ||
delimiter: "\t", | ||
expected: []string{ | ||
"a", | ||
"b", | ||
"c", | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
result, err := SplitString(tc.input, tc.delimiter) | ||
|
||
if tc.expectedErr == nil { | ||
assert.NoError(t, err) | ||
assert.Equal(t, tc.expected, result) | ||
} else { | ||
assert.EqualError(t, err, tc.expectedErr.Error()) | ||
assert.Nil(t, result) | ||
} | ||
}) | ||
} | ||
} |
This file contains hidden or 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
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.