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

fix: add support for marshalling and unmarshalling bytes #173

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

HarikrishnanBalagopal
Copy link

@HarikrishnanBalagopal HarikrishnanBalagopal commented Feb 22, 2023

fixes #172

def fibonacci(n):
    res = list(range(n))
    for i in res[2:]:
        res[i] = res[i-2] + res[i-1]
    return bytes(res)

print(fibonacci(4))
package main

import (
	"fmt"

	"github.com/qri-io/starlib/util"
	"go.starlark.net/starlark"
)

func main() {
	// Execute Starlark program in a file.
	thread := &starlark.Thread{Name: "my thread"}
	globals, err := starlark.ExecFile(thread, "fibonacci.star", nil, nil)
	if err != nil {
		panic(err)
	}

	// Retrieve a module global.
	fibonacci := globals["fibonacci"]

	// Call Starlark function from Go.
	v, err := starlark.Call(thread, fibonacci, starlark.Tuple{starlark.MakeInt(10)}, nil)
	if err != nil {
		panic(err)
	}
	vI, err := util.Unmarshal(v)
	if err != nil {
		panic(err)
	}
	fmt.Printf("fibonacci(10) = %+v\n", vI) // fibonacci(10) = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
}
$ go run main.go 

errbadtype: bytes
panic: unrecognized starlark type: bytes

goroutine 1 [running]:
main.main()
	/Users/user/Desktop/test-star/main.go:28 +0x185
exit status 2
$ echo 'after replacing with my branch in go.mod'
$ go mod tidy
go: downloading github.com/HarikrishnanBalagopal/starlib v0.5.0-alpha.0
$ go run main.go 

fibonacci(10) = [0 1 1 2 3 5 8 13 21 34]

Signed-off-by: Harikrishnan Balagopal <harikrishmenon@gmail.com>
Signed-off-by: Harikrishnan Balagopal <harikrishmenon@gmail.com>
Signed-off-by: Harikrishnan Balagopal <harikrishmenon@gmail.com>
@HarikrishnanBalagopal HarikrishnanBalagopal changed the title fix: add support for unmarshalling Starlark bytes as a Golang value fix: add support for marshalling and unmarshalling bytes Feb 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug: Marshal and Unmarshal don't support bytes
1 participant