Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add path package #167

Merged
merged 1 commit into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 134 additions & 0 deletions pkg/path/path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package path

import (
"path"

"github.com/google/go-jsonnet"
"github.com/google/go-jsonnet/ast"
"github.com/lintnet/go-jsonnet-native-functions/util"
)

func Base(name string) *jsonnet.NativeFunction {
return &jsonnet.NativeFunction{
Name: name,
Params: ast.Identifiers{"path"},
Func: func(s []any) (any, error) {
p, ok := s[0].(string)
if !ok {
return []any{
"", util.NewErrorf("path must be a string: %v", s[0]),
}, nil
}
return []any{path.Base(p), nil}, nil
},
}
}

func Clean(name string) *jsonnet.NativeFunction {
return &jsonnet.NativeFunction{
Name: name,
Params: ast.Identifiers{"path"},
Func: func(s []any) (any, error) {
p, ok := s[0].(string)
if !ok {
return []any{
"", util.NewErrorf("path must be a string: %v", s[0]),
}, nil
}
return []any{path.Clean(p), nil}, nil
},
}
}

func Dir(name string) *jsonnet.NativeFunction {
return &jsonnet.NativeFunction{
Name: name,
Params: ast.Identifiers{"path"},
Func: func(s []any) (any, error) {
p, ok := s[0].(string)
if !ok {
return []any{
"", util.NewErrorf("path must be a string: %v", s[0]),
}, nil
}
return []any{path.Dir(p), nil}, nil
},
}
}

func Ext(name string) *jsonnet.NativeFunction {
return &jsonnet.NativeFunction{
Name: name,
Params: ast.Identifiers{"path"},
Func: func(s []any) (any, error) {
p, ok := s[0].(string)
if !ok {
return []any{
"", util.NewErrorf("path must be a string: %v", s[0]),
}, nil
}
return []any{path.Ext(p), nil}, nil
},
}
}

func IsAbs(name string) *jsonnet.NativeFunction {
return &jsonnet.NativeFunction{
Name: name,
Params: ast.Identifiers{"path"},
Func: func(s []any) (any, error) {
p, ok := s[0].(string)
if !ok {
return []any{
false, util.NewErrorf("path must be a string: %v", s[0]),
}, nil
}
return []any{path.IsAbs(p), nil}, nil
},
}
}

func Match(name string) *jsonnet.NativeFunction {
return &jsonnet.NativeFunction{
Name: name,
Params: ast.Identifiers{"pattern", "name"},
Func: func(s []any) (any, error) {
pattern, ok := s[0].(string)
if !ok {
return []any{
false, util.NewErrorf("pattern must be a string: %v", s[0]),
}, nil
}
name, ok := s[1].(string)
if !ok {
return []any{
false, util.NewErrorf("name must be a string: %v", s[1]),
}, nil
}
matched, err := path.Match(pattern, name)
if err != nil {
return []any{
false, util.NewError(err.Error()),
}, nil
}
return []any{matched, nil}, nil
},
}
}

func Split(name string) *jsonnet.NativeFunction {
return &jsonnet.NativeFunction{
Name: name,
Params: ast.Identifiers{"path"},
Func: func(s []any) (any, error) {
p, ok := s[0].(string)
if !ok {
return []any{
"", "", util.NewErrorf("path must be a string: %v", s[0]),
}, nil
}
dir, file := path.Split(p)
return []any{dir, file, nil}, nil
},
}
}
241 changes: 241 additions & 0 deletions pkg/path/path_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
package path_test

import (
"fmt"
"testing"

"github.com/google/go-jsonnet"
"github.com/lintnet/go-jsonnet-native-functions/pkg/path"
"github.com/lintnet/go-jsonnet-native-functions/testutil"
"github.com/lintnet/go-jsonnet-native-functions/util"
)

func TestBase(t *testing.T) {
t.Parallel()
data := []struct {
name string
path any
exp []any
}{
{
name: "normal",
path: `"foo/hello.txt"`,
exp: []any{"hello.txt", nil},
},
{
name: "path must be a string",
path: 0,
exp: []any{"", util.NewError("path must be a string: 0")},
},
}
vm := jsonnet.MakeVM()
vm.NativeFunction(path.Base("path.base"))

for _, d := range data {
t.Run(d.name, func(t *testing.T) {
t.Parallel()
code := fmt.Sprintf(`std.native("path.base")(%v)`, d.path)
testutil.Check(t, vm, code, d.exp)
})
}
}

func TestClean(t *testing.T) {
t.Parallel()
data := []struct {
name string
path any
exp []any
}{
{
name: "normal",
path: `"foo/../hello.txt"`,
exp: []any{"hello.txt", nil},
},
{
name: "path must be a string",
path: 0,
exp: []any{"", util.NewError("path must be a string: 0")},
},
}
vm := jsonnet.MakeVM()
vm.NativeFunction(path.Clean("path.clean"))

for _, d := range data {
t.Run(d.name, func(t *testing.T) {
t.Parallel()
code := fmt.Sprintf(`std.native("path.clean")(%v)`, d.path)
testutil.Check(t, vm, code, d.exp)
})
}
}

func TestDir(t *testing.T) {
t.Parallel()
data := []struct {
name string
path any
exp []any
}{
{
name: "normal",
path: `"foo/bar/hello.txt"`,
exp: []any{"foo/bar", nil},
},
{
name: "path must be a string",
path: 0,
exp: []any{"", util.NewError("path must be a string: 0")},
},
}
vm := jsonnet.MakeVM()
vm.NativeFunction(path.Dir("path.Dir"))

for _, d := range data {
t.Run(d.name, func(t *testing.T) {
t.Parallel()
code := fmt.Sprintf(`std.native("path.Dir")(%v)`, d.path)
testutil.Check(t, vm, code, d.exp)
})
}
}

func TestExt(t *testing.T) {
t.Parallel()
data := []struct {
name string
path any
exp []any
}{
{
name: "normal",
path: `"foo/bar/hello.txt"`,
exp: []any{".txt", nil},
},
{
name: "path must be a string",
path: 0,
exp: []any{"", util.NewError("path must be a string: 0")},
},
}
vm := jsonnet.MakeVM()
vm.NativeFunction(path.Ext("path.Ext"))

for _, d := range data {
t.Run(d.name, func(t *testing.T) {
t.Parallel()
code := fmt.Sprintf(`std.native("path.Ext")(%v)`, d.path)
testutil.Check(t, vm, code, d.exp)
})
}
}

func TestIsAbs(t *testing.T) {
t.Parallel()
data := []struct {
name string
path any
exp []any
}{
{
name: "true",
path: `"/foo/bar/hello.txt"`,
exp: []any{true, nil},
},
{
name: "false",
path: `"foo/bar/hello.txt"`,
exp: []any{false, nil},
},
{
name: "path must be a string",
path: 0,
exp: []any{false, util.NewError("path must be a string: 0")},
},
}
vm := jsonnet.MakeVM()
vm.NativeFunction(path.IsAbs("path.IsAbs"))

for _, d := range data {
t.Run(d.name, func(t *testing.T) {
t.Parallel()
code := fmt.Sprintf(`std.native("path.IsAbs")(%v)`, d.path)
testutil.Check(t, vm, code, d.exp)
})
}
}

func TestMatch(t *testing.T) {
t.Parallel()
data := []struct {
name string
pattern any
n any
exp []any
}{
{
name: "true",
pattern: `"a*"`,
n: `"abs"`,
exp: []any{true, nil},
},
{
name: "false",
pattern: `"a*/b"`,
n: `"a/c/b"`,
exp: []any{false, nil},
},
{
name: "pattern must be a string",
pattern: 0,
n: `"abs"`,
exp: []any{false, util.NewError("pattern must be a string: 0")},
},
{
name: "name must be a string",
pattern: `"a*"`,
n: 0,
exp: []any{false, util.NewError("name must be a string: 0")},
},
}
vm := jsonnet.MakeVM()
vm.NativeFunction(path.Match("path.Match"))

for _, d := range data {
t.Run(d.name, func(t *testing.T) {
t.Parallel()
code := fmt.Sprintf(`std.native("path.Match")(%v, %v)`, d.pattern, d.n)
testutil.Check(t, vm, code, d.exp)
})
}
}

func TestSplit(t *testing.T) {
t.Parallel()
data := []struct {
name string
path any
exp []any
}{
{
name: "normal",
path: `"static/myfile.css"`,
exp: []any{"static/", "myfile.css", nil},
},
{
name: "path must be a string",
path: 0,
exp: []any{"", "", util.NewError("path must be a string: 0")},
},
}
vm := jsonnet.MakeVM()
vm.NativeFunction(path.Split("path.Split"))

for _, d := range data {
t.Run(d.name, func(t *testing.T) {
t.Parallel()
code := fmt.Sprintf(`std.native("path.Split")(%v)`, d.path)
testutil.Check(t, vm, code, d.exp)
})
}
}