-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #122 from jexia/feat/template-strings
Introducing template strings and strconcat function
- Loading branch information
Showing
8 changed files
with
262 additions
and
6 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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package functions | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/jexia/semaphore/pkg/prettyerr" | ||
"github.com/jexia/semaphore/pkg/specs" | ||
"github.com/jexia/semaphore/pkg/specs/types" | ||
) | ||
|
||
type wrapErr struct { | ||
Inner error | ||
} | ||
|
||
func (i wrapErr) Unwrap() error { | ||
return i.Inner | ||
} | ||
|
||
// ErrInvalidArgument is thrown when a given argument is invalid | ||
type ErrInvalidArgument struct { | ||
wrapErr | ||
Function string | ||
Property *specs.Property | ||
Expected types.Type | ||
} | ||
|
||
// Error returns a description of the given error as a string | ||
func (e ErrInvalidArgument) Error() string { | ||
return fmt.Sprintf("invalid argument %s in %s expected %s", e.Property.Type, e.Function, e.Expected) | ||
} | ||
|
||
// Prettify returns the prettified version of the given error | ||
func (e ErrInvalidArgument) Prettify() prettyerr.Error { | ||
return prettyerr.Error{ | ||
Code: "InvalidArgument", | ||
Message: e.Error(), | ||
Details: map[string]interface{}{ | ||
"Function": e.Function, | ||
"Type": e.Property.Type, | ||
"Expected": e.Expected, | ||
}, | ||
} | ||
} |
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,8 @@ | ||
package functions | ||
|
||
import "github.com/jexia/semaphore/pkg/functions" | ||
|
||
// Default represents the default functions collection | ||
var Default = functions.Custom{ | ||
"strconcat": Strconcat, | ||
} |
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,60 @@ | ||
package functions | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/jexia/semaphore/pkg/functions" | ||
"github.com/jexia/semaphore/pkg/references" | ||
"github.com/jexia/semaphore/pkg/specs" | ||
"github.com/jexia/semaphore/pkg/specs/labels" | ||
"github.com/jexia/semaphore/pkg/specs/types" | ||
) | ||
|
||
// Strconcat compiles the given arguments and constructs a new executable | ||
// function for the given arguments. | ||
func Strconcat(args ...*specs.Property) (*specs.Property, functions.Exec, error) { | ||
result := &specs.Property{ | ||
Name: "concat", | ||
Type: types.String, | ||
Label: labels.Optional, | ||
} | ||
|
||
for _, arg := range args { | ||
if arg.Type != types.String { | ||
return nil, nil, ErrInvalidArgument{ | ||
Property: arg, | ||
Expected: types.String, | ||
Function: "strconcat", | ||
} | ||
} | ||
} | ||
|
||
handle := func(store references.Store) error { | ||
result := strings.Builder{} | ||
|
||
for _, arg := range args { | ||
var value string | ||
|
||
if arg.Default != nil { | ||
value = arg.Default.(string) | ||
} | ||
|
||
if arg.Reference != nil { | ||
ref := store.Load(arg.Reference.Resource, arg.Reference.Path) | ||
if ref != nil { | ||
value = ref.Value.(string) | ||
} | ||
} | ||
|
||
_, err := result.WriteString(value) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
store.StoreValue("", ".", result.String()) | ||
return nil | ||
} | ||
|
||
return result, handle, 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
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