52 lines
957 B
Go
52 lines
957 B
Go
|
package paths
|
||
|
|
||
|
import (
|
||
|
"github.com/d5/tengo/v2"
|
||
|
"path"
|
||
|
)
|
||
|
|
||
|
var Module = &tengo.BuiltinModule {
|
||
|
Attrs: moduleMap,
|
||
|
}
|
||
|
|
||
|
var moduleMap = 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
|
||
|
}
|
||
|
}
|
||
|
|