-
-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
138 additions
and
2 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
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 |
---|---|---|
|
@@ -15,6 +15,8 @@ var ( | |
FuncMerge, | ||
FuncReverse, | ||
FuncTypeOf, | ||
FuncMax, | ||
FuncMin, | ||
) | ||
) | ||
|
||
|
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,32 @@ | ||
package execution | ||
|
||
import ( | ||
"github.com/tomwright/dasel/v3/model" | ||
) | ||
|
||
// FuncMax is a function that returns the highest number. | ||
var FuncMax = NewFunc( | ||
"max", | ||
func(data *model.Value, args model.Values) (*model.Value, error) { | ||
res := model.NewNullValue() | ||
for _, arg := range args { | ||
if res.IsNull() { | ||
res = arg | ||
continue | ||
} | ||
gt, err := arg.GreaterThan(res) | ||
if err != nil { | ||
return nil, err | ||
} | ||
gtBool, err := gt.BoolValue() | ||
if err != nil { | ||
return nil, err | ||
} | ||
if gtBool { | ||
res = arg | ||
} | ||
} | ||
return res, nil | ||
}, | ||
ValidateArgsMin(1), | ||
) |
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,22 @@ | ||
package execution_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/tomwright/dasel/v3/model" | ||
) | ||
|
||
func TestFuncMax(t *testing.T) { | ||
t.Run("int", testCase{ | ||
s: `max(1, 2, 3)`, | ||
out: model.NewIntValue(3), | ||
}.run) | ||
t.Run("float", testCase{ | ||
s: `max(1f, 2.5, 3.5)`, | ||
out: model.NewFloatValue(3.5), | ||
}.run) | ||
t.Run("mixed", testCase{ | ||
s: `max(1, 2f)`, | ||
out: model.NewFloatValue(2), | ||
}.run) | ||
} |
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,32 @@ | ||
package execution | ||
|
||
import ( | ||
"github.com/tomwright/dasel/v3/model" | ||
) | ||
|
||
// FuncMin is a function that returns the smalled number. | ||
var FuncMin = NewFunc( | ||
"min", | ||
func(data *model.Value, args model.Values) (*model.Value, error) { | ||
res := model.NewNullValue() | ||
for _, arg := range args { | ||
if res.IsNull() { | ||
res = arg | ||
continue | ||
} | ||
lt, err := arg.LessThan(res) | ||
if err != nil { | ||
return nil, err | ||
} | ||
ltBool, err := lt.BoolValue() | ||
if err != nil { | ||
return nil, err | ||
} | ||
if ltBool { | ||
res = arg | ||
} | ||
} | ||
return res, nil | ||
}, | ||
ValidateArgsMin(1), | ||
) |
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,22 @@ | ||
package execution_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/tomwright/dasel/v3/model" | ||
) | ||
|
||
func TestFuncMin(t *testing.T) { | ||
t.Run("int", testCase{ | ||
s: `min(1, 2, 3)`, | ||
out: model.NewIntValue(1), | ||
}.run) | ||
t.Run("float", testCase{ | ||
s: `min(1f, 2.5, 3.5)`, | ||
out: model.NewFloatValue(1), | ||
}.run) | ||
t.Run("mixed", testCase{ | ||
s: `min(1, 2f)`, | ||
out: model.NewIntValue(1), | ||
}.run) | ||
} |
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
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