-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve function handling, provide helpers to make writing funtions e…
…asier
- Loading branch information
Showing
37 changed files
with
795 additions
and
940 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
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
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 |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// SPDX-FileCopyrightText: 2023 Christoph Mewes | ||
// SPDX-License-Identifier: MIT | ||
|
||
package builtin | ||
|
||
import ( | ||
"errors" | ||
|
||
"go.xrstf.de/rudi/pkg/coalescing" | ||
"go.xrstf.de/rudi/pkg/equality" | ||
"go.xrstf.de/rudi/pkg/eval/types" | ||
) | ||
|
||
func eqFunction(ctx types.Context, args []any) (any, error) { | ||
return equality.Equal(ctx.Coalesce(), args[0], args[1]) | ||
} | ||
|
||
func likeFunction(ctx types.Context, args []any) (any, error) { | ||
return equality.Equal(coalescing.NewHumane(), args[0], args[1]) | ||
} | ||
|
||
func identicalFunction(ctx types.Context, args []any) (any, error) { | ||
return equality.Equal(coalescing.NewStrict(), args[0], args[1]) | ||
} | ||
|
||
func ltCoalescer(ctx types.Context, args []any) (any, error) { | ||
compared, err := equality.Compare(ctx.Coalesce(), args[0], args[1]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
switch compared { | ||
case equality.IsEqual: | ||
return false, nil | ||
case equality.IsSmaller: | ||
return true, nil | ||
case equality.IsGreater: | ||
return false, nil | ||
case equality.Unorderable: | ||
return false, errors.New("cannot order the given arguments") | ||
default: | ||
panic("Unexpected comparison result.") | ||
} | ||
} | ||
|
||
func lteCoalescer(ctx types.Context, args []any) (any, error) { | ||
compared, err := equality.Compare(ctx.Coalesce(), args[0], args[1]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
switch compared { | ||
case equality.IsEqual: | ||
return true, nil | ||
case equality.IsSmaller: | ||
return true, nil | ||
case equality.IsGreater: | ||
return false, nil | ||
case equality.Unorderable: | ||
return false, errors.New("cannot order the given arguments") | ||
default: | ||
panic("Unexpected comparison result.") | ||
} | ||
} | ||
|
||
func gtCoalescer(ctx types.Context, args []any) (any, error) { | ||
compared, err := equality.Compare(ctx.Coalesce(), args[0], args[1]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
switch compared { | ||
case equality.IsEqual: | ||
return false, nil | ||
case equality.IsSmaller: | ||
return false, nil | ||
case equality.IsGreater: | ||
return true, nil | ||
case equality.Unorderable: | ||
return false, errors.New("cannot order the given arguments") | ||
default: | ||
panic("Unexpected comparison result.") | ||
} | ||
} | ||
|
||
func gteCoalescer(ctx types.Context, args []any) (any, error) { | ||
compared, err := equality.Compare(ctx.Coalesce(), args[0], args[1]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
switch compared { | ||
case equality.IsEqual: | ||
return true, nil | ||
case equality.IsSmaller: | ||
return false, nil | ||
case equality.IsGreater: | ||
return true, nil | ||
case equality.Unorderable: | ||
return false, errors.New("cannot order the given arguments") | ||
default: | ||
panic("Unexpected comparison result.") | ||
} | ||
} |
File renamed without changes.
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
File renamed without changes.
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,21 @@ | ||
// SPDX-FileCopyrightText: 2023 Christoph Mewes | ||
// SPDX-License-Identifier: MIT | ||
|
||
package builtin | ||
|
||
import ( | ||
"time" | ||
|
||
"go.xrstf.de/rudi/pkg/eval/types" | ||
) | ||
|
||
func nowFunction(ctx types.Context, args []any) (any, error) { | ||
formatString, err := ctx.Coalesce().ToString(args[0]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
formatted := time.Now().Format(string(formatString)) | ||
|
||
return formatted, 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,38 @@ | ||
// SPDX-FileCopyrightText: 2023 Christoph Mewes | ||
// SPDX-License-Identifier: MIT | ||
|
||
package builtin | ||
|
||
import ( | ||
"encoding/base64" | ||
"fmt" | ||
|
||
"go.xrstf.de/rudi/pkg/eval/types" | ||
) | ||
|
||
// (to-base64 VAL:string) | ||
func toBase64Function(ctx types.Context, args []any) (any, error) { | ||
str, err := ctx.Coalesce().ToString(args[0]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
encoded := base64.StdEncoding.EncodeToString([]byte(str)) | ||
|
||
return encoded, nil | ||
} | ||
|
||
// (from-base64 VAL:string) | ||
func fromBase64Function(ctx types.Context, args []any) (any, error) { | ||
str, err := ctx.Coalesce().ToString(args[0]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
decoded, err := base64.StdEncoding.DecodeString(string(str)) | ||
if err != nil { | ||
return nil, fmt.Errorf("not valid base64: %w", err) | ||
} | ||
|
||
return string(decoded), nil | ||
} |
File renamed without changes.
Oops, something went wrong.