main.go 896 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package paths
  2. import (
  3. "github.com/d5/tengo/v2"
  4. "path"
  5. )
  6. var Module = map[string] tengo.Object {
  7. "base" : &tengo.UserFunction{
  8. Name: "base",
  9. Value: wrapOneArg(path.Base),
  10. },
  11. "ext" : &tengo.UserFunction{
  12. Name: "ext",
  13. Value: wrapOneArg(path.Ext),
  14. },
  15. "dir" : &tengo.UserFunction{
  16. Name: "dir",
  17. Value: wrapOneArg(path.Dir),
  18. },
  19. "clean" : &tengo.UserFunction{
  20. Name: "clean",
  21. Value: wrapOneArg(path.Clean),
  22. },
  23. }
  24. func wrapOneArg(
  25. fn func(string) string,
  26. ) func(...tengo.Object) (tengo.Object, error) {
  27. return func(args ...tengo.Object) (tengo.Object, error) {
  28. if len(args) != 1 {
  29. return nil, tengo.ErrWrongNumArguments
  30. }
  31. str, ok := tengo.ToString(args[0])
  32. if !ok {
  33. return nil, tengo.ErrInvalidArgumentType{
  34. Name: "first",
  35. Expected: "string(compatible)",
  36. Found: args[0].TypeName(),
  37. }
  38. }
  39. return &tengo.String{
  40. Value: fn(str),
  41. }, nil
  42. }
  43. }