1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- package paths
- import (
- "github.com/d5/tengo/v2"
- "path"
- )
- var Module = map[string] tengo.Object {
- "base" : &tengo.UserFunction{
- Name: "base",
- Value: wrapOneArg(path.Base),
- },
- "ext" : &tengo.UserFunction{
- Name: "ext",
- Value: wrapOneArg(path.Ext),
- },
- "dir" : &tengo.UserFunction{
- Name: "dir",
- Value: wrapOneArg(path.Dir),
- },
- "clean" : &tengo.UserFunction{
- Name: "clean",
- Value: wrapOneArg(path.Clean),
- },
- }
- func wrapOneArg(
- fn func(string) string,
- ) func(...tengo.Object) (tengo.Object, error) {
- return func(args ...tengo.Object) (tengo.Object, error) {
- if len(args) != 1 {
- return nil, tengo.ErrWrongNumArguments
- }
- str, ok := tengo.ToString(args[0])
- if !ok {
- return nil, tengo.ErrInvalidArgumentType{
- Name: "first",
- Expected: "string(compatible)",
- Found: args[0].TypeName(),
- }
- }
- return &tengo.String{
- Value: fn(str),
- }, nil
- }
- }
|