Skip to content

Commit 3b30877

Browse files
committed
Initial commit
0 parents  commit 3b30877

18 files changed

+519
-0
lines changed

.github/dependabot.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# To get started with Dependabot version updates, you'll need to specify which
2+
# package ecosystems to update and where the package manifests are located.
3+
# Please see the documentation for all configuration options:
4+
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
5+
6+
version: 2
7+
updates:
8+
- package-ecosystem: "gomod" # See documentation for possible values
9+
directory: "/" # Location of package manifests
10+
schedule:
11+
interval: "daily"
12+
13+
# Maintain dependencies for GitHub Actions
14+
- package-ecosystem: "github-actions"
15+
directory: "/"
16+
schedule:
17+
interval: "daily"

.github/workflows/coverage.yml

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
name: Coverage
2+
on:
3+
push:
4+
tags:
5+
- v*
6+
branches:
7+
- master
8+
pull_request:
9+
10+
jobs:
11+
test:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Install Go
15+
uses: actions/setup-go@v2.1.3
16+
with:
17+
go-version: 1.16
18+
19+
- name: Get Go environment
20+
id: go-env
21+
run: |
22+
echo "::set-output name=cache::$(go env GOCACHE)"
23+
echo "::set-output name=modcache::$(go env GOMODCACHE)"
24+
- name: Set up cache
25+
uses: actions/cache@v2.1.4
26+
with:
27+
path: |
28+
${{ steps.go-env.outputs.cache }}
29+
${{ steps.go-env.outputs.modcache }}
30+
key: test-${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
31+
restore-keys: |
32+
test-${{ runner.os }}-go-
33+
34+
- name: Checkout code
35+
uses: actions/checkout@v2.3.4
36+
37+
- name: Download dependencies
38+
run: go mod download && go mod tidy
39+
40+
- name: Run tests with coverage
41+
run: go test -v -coverpkg=./... -coverprofile=profile.out ./...
42+
43+
- name: Extract cover profile
44+
run: go tool cover -func profile.out
45+
46+
- name: Upload artifact
47+
uses: actions/upload-artifact@v2.2.2
48+
with:
49+
name: coverage
50+
path: profile.out
51+
if-no-files-found: error
52+
retention-days: 1
53+
54+
upload:
55+
runs-on: ubuntu-latest
56+
needs:
57+
- test
58+
steps:
59+
- name: Checkout code
60+
uses: actions/checkout@v2.3.4
61+
62+
- name: Download artifact
63+
uses: actions/download-artifact@v2.0.8
64+
with:
65+
name: coverage
66+
67+
- name: Send coverage
68+
uses: codecov/codecov-action@v1.3.1
69+
with:
70+
file: profile.out

.github/workflows/lint.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Lint
2+
on:
3+
push:
4+
tags:
5+
- v*
6+
branches:
7+
- master
8+
pull_request:
9+
workflow_dispatch:
10+
11+
jobs:
12+
golangci-lint:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout code
16+
uses: actions/checkout@v2.3.4
17+
18+
- name: Install Go
19+
uses: actions/setup-go@v2.1.3
20+
with:
21+
go-version: 1.16
22+
23+
- name: Lint
24+
uses: golangci/golangci-lint-action@v2.5.1
25+
with:
26+
version: latest
27+
args: --timeout 5m
28+
skip-go-installation: true

.github/workflows/pr-extra.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Extra
2+
on:
3+
push:
4+
tags:
5+
- v*
6+
branches:
7+
- master
8+
pull_request:
9+
workflow_dispatch:
10+
11+
jobs:
12+
vulns:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout code
16+
uses: actions/checkout@v2.3.4
17+
18+
- name: Install Go
19+
uses: actions/setup-go@v2.1.3
20+
with:
21+
go-version: 1.16
22+
23+
- name: List dependencies
24+
run: go list -json -m all > go.list
25+
26+
- name: Run nancy
27+
uses: sonatype-nexus-community/nancy-github-action@v1.0.2

.gitignore

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
### Go template
2+
# Binaries for programs and plugins
3+
*.exe
4+
*.exe~
5+
*.dll
6+
*.so
7+
*.dylib
8+
9+
# Env files
10+
*.env
11+
12+
# Test binary, built with `go test -c`
13+
*.test
14+
15+
# Output of the go coverage tool, specifically when used with LiteIDE
16+
*.out
17+
18+
# IntelliJ project files
19+
.idea
20+
*.iml
21+
out
22+
gen

README.md

1.11 KB

sun

Sun is a Starlark module with Python builtins.

Installation

go get github.com/tdakkota/sun

Usage

package main

import (
	"fmt"

	"go.starlark.net/starlark"

	"github.com/tdakkota/sun"
)

func main() {
	code := "list(filter(lambda x: x % 2 == 0, range(10)))"

	// Eval Starlark expresion.
	thread := &starlark.Thread{Name: "main"}
	result, err := starlark.Eval(thread, "example.star", code, sun.Module.Members)
	if err != nil {
		panic(err)
	}

	fmt.Println(result)
	// Output:
	// [0, 2, 4, 6, 8]
}

args.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package sun
2+
3+
import (
4+
"fmt"
5+
6+
"go.starlark.net/starlark"
7+
)
8+
9+
func wantArgs(fnName string, args starlark.Tuple, kwargs []starlark.Tuple, count int) error {
10+
switch {
11+
case len(kwargs) > 0:
12+
return fmt.Errorf("%s does not accept keyword arguments", fnName)
13+
case len(args) != count:
14+
return fmt.Errorf("%s: got %d arguments, want exactly %d", fnName, len(args), count)
15+
default:
16+
return nil
17+
}
18+
}

callable.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package sun
2+
3+
import (
4+
"go.starlark.net/starlark"
5+
)
6+
7+
func callable(
8+
thread *starlark.Thread,
9+
b *starlark.Builtin,
10+
args starlark.Tuple,
11+
kwargs []starlark.Tuple,
12+
) (starlark.Value, error) {
13+
if err := wantArgs(b.Name(), args, kwargs, 1); err != nil {
14+
return nil, err
15+
}
16+
17+
if _, ok := args[0].(starlark.Callable); ok {
18+
return starlark.True, nil
19+
}
20+
return starlark.False, nil
21+
}

callable_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package sun
2+
3+
import (
4+
"testing"
5+
6+
"go.starlark.net/starlark"
7+
)
8+
9+
func TestCallable(t *testing.T) {
10+
runTestData(t, "callable.star", starlark.StringDict{
11+
"callable": starlark.NewBuiltin("callable", callable),
12+
})
13+
}

doc.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Package sun contains Python builtins implementations for Starlark.
2+
package sun

example_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package sun_test
2+
3+
import (
4+
"fmt"
5+
6+
"go.starlark.net/starlark"
7+
8+
"github.com/tdakkota/sun"
9+
)
10+
11+
func Example_filter() {
12+
code := "list(filter(lambda x: x % 2 == 0, range(10)))"
13+
thread := &starlark.Thread{Name: "main"}
14+
result, err := starlark.Eval(thread, "example.star", code, sun.Module.Members)
15+
if err != nil {
16+
panic(err)
17+
}
18+
19+
fmt.Println(result)
20+
// Output:
21+
// [0, 2, 4, 6, 8]
22+
}

filter.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package sun
2+
3+
import (
4+
"fmt"
5+
6+
"go.starlark.net/starlark"
7+
)
8+
9+
type filterFunc = func(x starlark.Value) (starlark.Value, error)
10+
11+
type filterIter struct {
12+
thread *starlark.Thread
13+
function filterFunc
14+
iterator starlark.Iterator
15+
}
16+
17+
func (f filterIter) Next(p *starlark.Value) bool {
18+
var x starlark.Value
19+
for {
20+
if !f.iterator.Next(&x) {
21+
return false
22+
}
23+
24+
v, err := f.function(x)
25+
if err != nil {
26+
return false
27+
}
28+
29+
if v.Truth() {
30+
*p = x
31+
return true
32+
}
33+
}
34+
}
35+
36+
func (f filterIter) Done() {
37+
f.iterator.Done()
38+
}
39+
40+
type filterObject struct {
41+
thread *starlark.Thread
42+
function filterFunc
43+
iterable starlark.Iterable
44+
iterator starlark.Iterator
45+
}
46+
47+
func (f filterObject) String() string {
48+
return fmt.Sprintf("<filter object>")
49+
}
50+
51+
func (f filterObject) Type() string {
52+
return "filter"
53+
}
54+
55+
func (f filterObject) Freeze() {
56+
f.iterable.Freeze()
57+
}
58+
59+
func (f filterObject) Truth() starlark.Bool {
60+
return starlark.True
61+
}
62+
63+
func (f filterObject) Hash() (uint32, error) {
64+
return 0, fmt.Errorf("unhashable type: filter")
65+
}
66+
67+
func (f filterObject) Iterate() starlark.Iterator {
68+
return filterIter{
69+
thread: f.thread,
70+
function: f.function,
71+
iterator: f.iterator,
72+
}
73+
}
74+
75+
func filter(
76+
thread *starlark.Thread,
77+
b *starlark.Builtin,
78+
args starlark.Tuple,
79+
kwargs []starlark.Tuple,
80+
) (starlark.Value, error) {
81+
var (
82+
function filterFunc
83+
iterable starlark.Iterable
84+
)
85+
if err := wantArgs(b.Name(), args, kwargs, 2); err != nil {
86+
return nil, err
87+
}
88+
89+
switch fn := args[0].(type) {
90+
case starlark.Callable:
91+
function = func(x starlark.Value) (starlark.Value, error) {
92+
return starlark.Call(thread, fn, starlark.Tuple{x}, nil)
93+
}
94+
case starlark.NoneType:
95+
function = func(x starlark.Value) (starlark.Value, error) {
96+
return x.Truth(), nil
97+
}
98+
default:
99+
return nil, fmt.Errorf("got %s, want callable", fn.Type())
100+
}
101+
102+
iterable, ok := args[1].(starlark.Iterable)
103+
if !ok {
104+
return nil, fmt.Errorf("got %s, want iterable", args[1].Type())
105+
}
106+
107+
return &filterObject{
108+
thread: thread,
109+
function: function,
110+
iterable: iterable,
111+
iterator: iterable.Iterate(),
112+
}, nil
113+
}

filter_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package sun
2+
3+
import (
4+
"testing"
5+
6+
"go.starlark.net/starlark"
7+
)
8+
9+
func TestFilter(t *testing.T) {
10+
runTestData(t, "filter.star", starlark.StringDict{
11+
"filter": starlark.NewBuiltin("filter", filter),
12+
})
13+
}

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/tdakkota/sun
2+
3+
go 1.16
4+
5+
require go.starlark.net v0.0.0-20210312235212-74c10e2c17dc

0 commit comments

Comments
 (0)