Skip to content
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

Local i18n #85

Merged
merged 21 commits into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Change nomenclature of array functions to not conflict with bash syntax
  • Loading branch information
zix99 committed Oct 14, 2022
commit eaa893dd532e32a3d67fa1889e16df2c6cac3b20
2 changes: 1 addition & 1 deletion cmd/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func expressionFunction(c *cli.Context) error {
expCtx.Keys["#"] = buildSpecialKeyJson(data, nil)
expCtx.Keys[".#"] = buildSpecialKeyJson(data, keys)
expCtx.Keys["#."] = expCtx.Keys[".#"]
expCtx.Keys["$"] = expressions.MakeArray(data...)
expCtx.Keys["@"] = expressions.MakeArray(data...)
}

// Output results
Expand Down
46 changes: 26 additions & 20 deletions docs/usage/expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ The following are special Keys:
* `{.}` Returns all matched values with match names as JSON
* `{#}` Returns all matched numbered values as JSON
* `{.#}` Returned numbered and named matches as JSON
* `{$}` All extracted matches in array form
* `{@}` All extracted matches in array form

### Testing

Expand Down Expand Up @@ -249,60 +249,66 @@ Range functions provide the ability to work with arrays in expressions. You
can create an array either manually with the `{$ ...}` helper (above) or
by `{$split ...}` a string into an array.

#### $split
#### Array Definition

Syntax: `{$split <arr> ["delim"]}`
Syntax: `{@ ele0 ele1 ele2}` (`{$ ele0 ele1 ele2}` is equivalent)

Creates an array with the provided elements. Use `{@}` for an array of all matches.

#### @split

Syntax: `{@split <arr> ["delim"]}`

Splits a string into an array with the separating `delim`. If `delim` isn't
specified, `" "` will be used.
#### $join
#### @join

Syntax: `{$join <arr> ["delim"]}`
Syntax: `{@join <arr> ["delim"]}`

Re-joins an array back into a string. If `delim` is empty, it will be `" "`

#### $map
#### @map

Syntax: `{$map <arr> <mapfunc>}`
Syntax: `{@map <arr> <mapfunc>}`

Evaluates `mapfunc` against each element in the array. In `mapfunc`, `{0}`
is the current element. The function must be surrounded by quotes.

For example, given the array `[1,2,3]`, and the function
`{$map {array} "{multi {0} 2}"}` will output [2,4,6].
`{@map {array} "{multi {0} 2}"}` will output [2,4,6].

#### $reduce
#### @reduce

Syntax: `{$reduce <arr> <reducefunc>}`
Syntax: `{@reduce <arr> <reducefunc>}`

Evaluates `reducefunc` against each element and a memo. `{0}` is the memo, and
`{1}` is the current value.

For example, given the array `[1,2,3]`, and the function
`{$reduce {array} "{sumi {0} {1}}"}`, it will return `6`.
`{@reduce {array} "{sumi {0} {1}}"}`, it will return `6`.

#### $filter
#### @filter

Syntax: `{$filter <arr> <filterfunc>}`
Syntax: `{@filter <arr> <filterfunc>}`

Evaluates `filterfunc` for each element. If *truthy*, item will be in resulting
array. If false, it will be omitted. `{0}` will be the value examined.

For example, given the array `[1,abc,23,efg]`, and the function
`{$filter {array} "{isnum {0}}"}` will return `[1,23]`.
`{@filter {array} "{isnum {0}}"}` will return `[1,23]`.

#### $slice
#### @slice

Syntax: `{$slice <arr> "begin" ["length"]}`
Syntax: `{@slice <arr> "begin" ["length"]}`

Gets a slice of an array. If `begin` is a negative number, will start from the end.

Examples: (Array `[1,2,3,4]`)

- `{$slice {array} 1}` - [2,3,4]
- `{$slice {array} 1 1}` - [2]
- `{$slice {array} -2}` - [3,4]
- `{$slice {array} -2 1}` - [3]
- `{@slice {array} 1}` - [2,3,4]
- `{@slice {array} 1 1}` - [2]
- `{@slice {array} -2}` - [3,4]
- `{@slice {array} -2 1}` - [3]


### Drawing
Expand Down
13 changes: 7 additions & 6 deletions pkg/expressions/stdlib/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,13 @@ var StandardFunctions = map[string]KeyBuilderFunction{
"$": kfJoin(ArraySeparator),

// Ranges
"$map": kfArrayMap,
"$split": kfArraySplit,
"$join": kfArrayJoin,
"$reduce": kfArrayReduce,
"$filter": kfArrayFilter,
"$slice": kfArraySlice,
"@": kfJoin(ArraySeparator),
"@map": kfArrayMap,
"@split": kfArraySplit,
"@join": kfArrayJoin,
"@reduce": kfArrayReduce,
"@filter": kfArrayFilter,
"@slice": kfArraySlice,

// Pathing
"basename": kfPathBase,
Expand Down
12 changes: 6 additions & 6 deletions pkg/expressions/stdlib/range.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (s *subContext) Eval(stage KeyBuilderStage, v0, v1 string) string {
return stage(s)
}

// {$split <string> "delim"}
// {@split <string> "delim"}
func kfArraySplit(args []KeyBuilderStage) KeyBuilderStage {
if !isArgCountBetween(args, 1, 2) {
return stageLiteral(ErrorArgCount)
Expand All @@ -53,7 +53,7 @@ func kfArraySplit(args []KeyBuilderStage) KeyBuilderStage {
}
}

// {$join <array> "by"}
// {@join <array> "by"}
func kfArrayJoin(args []KeyBuilderStage) KeyBuilderStage {
if !isArgCountBetween(args, 1, 2) {
return stageLiteral(ErrorArgCount)
Expand All @@ -70,7 +70,7 @@ func kfArrayJoin(args []KeyBuilderStage) KeyBuilderStage {
}
}

// {$map <arr> <mapFunc>}
// {@map <arr> <mapFunc>}
func kfArrayMap(args []KeyBuilderStage) KeyBuilderStage {
if len(args) != 2 {
return stageLiteral(ErrorArgCount)
Expand All @@ -91,7 +91,7 @@ func kfArrayMap(args []KeyBuilderStage) KeyBuilderStage {
}
}

// {$reduce <arr> <reducer>}
// {@reduce <arr> <reducer>}
func kfArrayReduce(args []KeyBuilderStage) KeyBuilderStage {
if len(args) != 2 {
return stageLiteral(ErrorArgCount)
Expand All @@ -115,7 +115,7 @@ func kfArrayReduce(args []KeyBuilderStage) KeyBuilderStage {
}
}

// {slice <arr> start len}
// {@slice <arr> start len}
func kfArraySlice(args []KeyBuilderStage) KeyBuilderStage {
if !isArgCountBetween(args, 2, 3) {
return stageLiteral(ErrorArgCount)
Expand Down Expand Up @@ -154,7 +154,7 @@ func kfArraySlice(args []KeyBuilderStage) KeyBuilderStage {
}
}

// {filter <arr> <truthy-statement>}
// {@filter <arr> <truthy-statement>}
func kfArrayFilter(args []KeyBuilderStage) KeyBuilderStage {
if len(args) != 2 {
return stageLiteral(ErrorArgCount)
Expand Down
52 changes: 26 additions & 26 deletions pkg/expressions/stdlib/range_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ func TestArraySplit(t *testing.T) {
testExpression(
t,
mockContext("a\tb\tc"),
`{$split {0} "\t"}`,
`{@split {0} "\t"}`,
"a\x00b\x00c",
)
testExpression(
t,
mockContext("abc"),
`{$split {0} "\t"}`,
`{@split {0} "\t"}`,
"abc",
)
testExpression(
t,
mockContext("a b\tc"),
`{$split {0}}`,
`{@split {0}}`,
"a\x00b\tc",
)
testExpression(
t,
mockContext("a b\tc"),
`{$split {0} ""}`,
`{@split {0} ""}`,
"<EMPTY>",
)
testExpression(
t,
mockContext("a b\tc"),
`{$split {0} "" "c"}`,
`{@split {0} "" "c"}`,
"<ARGN>",
)
}
Expand All @@ -42,31 +42,31 @@ func TestArrayJoin(t *testing.T) {
testExpression(
t,
mockContext("a\x00b\x00c"),
`{$join {0} ", "}`,
`{@join {0} ", "}`,
"a, b, c",
)
testExpression(
t,
mockContext("a\x00b\x00c"),
`{$join {0} ""}`,
`{@join {0} ""}`,
"abc",
)
testExpression(
t,
mockContext("a"),
`{$join {0} ", "}`,
`{@join {0} ", "}`,
"a",
)
testExpression(
t,
mockContext("a\x00b\x00c"),
`{$join {0}}`,
`{@join {0}}`,
"a b c",
)
testExpression(
t,
mockContext("a\x00b\x00c"),
`{$join {0} ", " "c"}`,
`{@join {0} ", " "c"}`,
"<ARGN>",
)
}
Expand All @@ -75,19 +75,19 @@ func TestArrayMap(t *testing.T) {
testExpression(
t,
mockContext(expressions.MakeArray("joe", "is", "cool")),
`{$join {$map {0} "{0}bob"} ", "}`,
`{@join {@map {0} "{0}bob"} ", "}`,
"joebob, isbob, coolbob",
)
testExpression(
t,
mockContext(expressions.MakeArray("5", "1", "3")),
`{$join {$map {0} "{multi {0} 2}"} ", "}`,
`{@join {@map {0} "{multi {0} 2}"} ", "}`,
"10, 2, 6",
)
testExpression(
t,
mockContext(expressions.MakeArray("5", "1", "3")),
`{$join {$map {0} "{multi {0} 2}" ""} ", "}`,
`{@join {@map {0} "{multi {0} 2}" ""} ", "}`,
"<ARGN>",
)
}
Expand All @@ -96,13 +96,13 @@ func TestArrayReduce(t *testing.T) {
testExpression(
t,
mockContext("0 1 2 5"),
`{$reduce {$split {0} " "} "{sumi {0} {1}}"}`,
`{@reduce {@split {0} " "} "{sumi {0} {1}}"}`,
"8",
)
testExpression(
t,
mockContext("0 1 2 5"),
`{$reduce {$split {0} " "} "{sumi {0} {1}}" bla}`,
`{@reduce {@split {0} " "} "{sumi {0} {1}}" bla}`,
"<ARGN>",
)
}
Expand All @@ -111,37 +111,37 @@ func TestArraySlice(t *testing.T) {
testExpression(
t,
mockContext("0 1 2 5"),
`{$join {$slice {$split {0} " "} 1 2}}`,
`{@join {@slice {@split {0} " "} 1 2}}`,
"1 2",
)
testExpression(
t,
mockContext("0 1 2 5"),
`{$join {$slice {$split {0} " "} 1}}`,
`{@join {@slice {@split {0} " "} 1}}`,
"1 2 5",
)
testExpression(
t,
mockContext("0 1 2 5"),
`{$join {$slice {$split {0} " "} 0 50}}`,
`{@join {@slice {@split {0} " "} 0 50}}`,
"0 1 2 5",
)
testExpression(
t,
mockContext("0 1 2 5"),
`{$join {$slice {$split {0} " "} 10 2}}`,
`{@join {@slice {@split {0} " "} 10 2}}`,
"",
)
testExpression(
t,
mockContext("0 1 2 5"),
`{$join {$slice {$split {0} " "} -3 2}}`,
`{@join {@slice {@split {0} " "} -3 2}}`,
"1 2",
)
testExpression(
t,
mockContext("0 1 2 5"),
`{$join {$slice {$split {0} " "} 1 2 bla}}`,
`{@join {@slice {@split {0} " "} 1 2 bla}}`,
"<ARGN>",
)
}
Expand All @@ -150,31 +150,31 @@ func TestArrayFilter(t *testing.T) {
testExpression(
t,
mockContext(expressions.MakeArray("a", "123", "b", "455")),
`{$join {$filter {0} "{isnum {0}}"}}`,
`{@join {@filter {0} "{isnum {0}}"}}`,
"123 455",
)
testExpression(
t,
mockContext(expressions.MakeArray("a", "123", "b", "455")),
`{$join {$filter {0} "1"}}`,
`{@join {@filter {0} "1"}}`,
"a 123 b 455",
)
testExpression(
t,
mockContext(expressions.MakeArray("a", "123", "b", "455")),
`{$join {$filter {0} ""}}`,
`{@join {@filter {0} ""}}`,
"",
)
testExpression( // filter with empty
t,
mockContext(expressions.MakeArray("", "123", "", "456")),
`{$join {$filter {0} "1"} ","}`,
`{@join {@filter {0} "1"} ","}`,
",123,,456",
)
testExpression(
t,
mockContext(expressions.MakeArray("a", "123", "b", "455")),
`{$join {$filter {0}}}`,
`{@join {@filter {0}}}`,
"<ARGN>",
)
}
2 changes: 1 addition & 1 deletion pkg/extractor/extractor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestSourceAndLine(t *testing.T) {
input := convertReaderToBatches("test", strings.NewReader(testData), 1)
ex, err := New(input, &Config{
Regex: `(\d+)`,
Extract: "{src} {line} val:{1} {bad} {$}",
Extract: "{src} {line} val:{1} {bad} {@}",
Workers: 1,
})
assert.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/extractor/sliceSpaceExpressionContext.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (s *SliceSpaceExpressionContext) GetKey(key string) string {
return s.json(false, true)
case ".#", "#.":
return s.json(true, true)
case "$":
case "*":
return s.array()
}

Expand Down