Skip to content

Commit

Permalink
add go/starlark helper methods for dataconv/types types (#105)
Browse files Browse the repository at this point in the history
* more helpers on that

* add empty

* add tests for string go

* testing

* clean up lint

* fix lint
  • Loading branch information
hyorigo authored Jun 23, 2024
1 parent 4ef73d8 commit 8c364a9
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 15 deletions.
44 changes: 39 additions & 5 deletions dataconv/types/hybrid.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,21 +155,31 @@ func (p *StringOrBytes) Unpack(v starlark.Value) error {
return nil
}

// GoBytes returns the Go byte slice representation of the StringOrBytes.
func (p StringOrBytes) GoBytes() []byte {
return []byte(p)
}

// GoString returns the Go string representation of the StringOrBytes.
func (p StringOrBytes) GoString() string {
return string(p)
}

// GoBytes returns the Go byte slice representation of the StringOrBytes.
func (p StringOrBytes) GoBytes() []byte {
return []byte(p)
}

// StarlarkString returns the Starlark string representation of the StringOrBytes.
func (p StringOrBytes) StarlarkString() starlark.String {
return starlark.String(p)
}

// StarlarkBytes returns the Starlark bytes representation of the StringOrBytes.
func (p StringOrBytes) StarlarkBytes() starlark.Bytes {
return starlark.Bytes(p)
}

// IsEmpty returns true if the underlying value is an empty string.
func (p StringOrBytes) IsEmpty() bool {
return p.GoString() == emptyStr
}

// NullableStringOrBytes is an Unpacker that converts a Starlark None or string to Go's string.
type NullableStringOrBytes struct {
str *string
Expand Down Expand Up @@ -210,6 +220,30 @@ func (p *NullableStringOrBytes) GoString() string {
return *p.str
}

// GoBytes returns the Go byte slice representation of the NullableStringOrBytes, if the underlying value is nil, it returns nil.
func (p *NullableStringOrBytes) GoBytes() []byte {
if p == nil || p.str == nil {
return nil
}
return []byte(*p.str)
}

// StarlarkString returns the Starlark string representation of the NullableStringOrBytes, if the underlying value is nil, it returns a Starlark string with an empty string.
func (p *NullableStringOrBytes) StarlarkString() starlark.String {
if p == nil || p.str == nil {
return ""
}
return starlark.String(*p.str)
}

// StarlarkBytes returns the Starlark bytes representation of the NullableStringOrBytes, if the underlying value is nil, it returns a Starlark bytes with an empty string.
func (p *NullableStringOrBytes) StarlarkBytes() starlark.Bytes {
if p == nil || p.str == nil {
return ""
}
return starlark.Bytes(*p.str)
}

// IsNull returns true if the underlying value is nil.
func (p *NullableStringOrBytes) IsNull() bool {
return p == nil || p.str == nil
Expand Down
46 changes: 38 additions & 8 deletions dataconv/types/hybrid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"math"
"reflect"
"runtime"
"testing"

Expand Down Expand Up @@ -437,12 +438,14 @@ func TestStringOrBytes_Stringer(t *testing.T) {
v StringOrBytes
wantGo string
wantStar starlark.String
empty bool
}{
{
name: "empty",
v: "",
wantGo: "",
wantStar: starlark.String(""),
empty: true,
},
{
name: "string",
Expand All @@ -468,6 +471,12 @@ func TestStringOrBytes_Stringer(t *testing.T) {
if got := tt.v.StarlarkString(); got != tt.wantStar {
t.Errorf("StringOrBytes.StarlarkString() = %v, want %v", got, tt.wantStar)
}
if got := tt.v.StarlarkBytes(); got != starlark.Bytes(tt.wantStar) {
t.Errorf("StringOrBytes.StarlarkBytes() = %v, want %v", got, starlark.Bytes(tt.wantStar))
}
if got := tt.v.IsEmpty(); got != tt.empty {
t.Errorf("StringOrBytes.IsEmpty() = %v, want %v", got, tt.empty)
}
})
}
}
Expand Down Expand Up @@ -517,50 +526,71 @@ func TestNullableStringOrBytes_Methods(t *testing.T) {
tests := []struct {
name string
str *NullableStringOrBytes
wantStr string
wantGoStr string
wantGoBytes []byte
wantStarStr starlark.String
wantIsNull bool
wantIsEmpty bool
}{
{
name: "nil",
str: nil,
wantStr: "",
wantGoStr: "",
wantGoBytes: nil,
wantStarStr: starlark.String(""),
wantIsNull: true,
wantIsEmpty: true,
},
{
name: "nil value",
str: &NullableStringOrBytes{},
wantStr: "",
wantGoStr: "",
wantGoBytes: nil,
wantStarStr: starlark.String(""),
wantIsNull: true,
wantIsEmpty: true,
},
{
name: "no default",
str: NewNullableStringOrBytesNoDefault(),
wantStr: "",
wantGoStr: "",
wantGoBytes: nil,
wantStarStr: starlark.String(""),
wantIsNull: true,
wantIsEmpty: true,
},
{
name: "empty string",
str: NewNullableStringOrBytes(""),
wantStr: "",
wantGoStr: "",
wantGoBytes: []byte{},
wantStarStr: starlark.String(""),
wantIsNull: false,
wantIsEmpty: true,
},
{
name: "non-empty string",
str: NewNullableStringOrBytes("hello"),
wantStr: "hello",
wantGoStr: "hello",
wantGoBytes: []byte("hello"),
wantStarStr: starlark.String("hello"),
wantIsNull: false,
wantIsEmpty: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotStr := tt.str.GoString(); gotStr != tt.wantStr {
t.Errorf("NullableStringOrBytes.GoString() = %v, want %v", gotStr, tt.wantStr)
if gotStr := tt.str.GoString(); gotStr != tt.wantGoStr {
t.Errorf("NullableStringOrBytes.GoString() = %v, want %v", gotStr, tt.wantGoStr)
}
if gotBytes := tt.str.GoBytes(); !reflect.DeepEqual(gotBytes, tt.wantGoBytes) {
t.Errorf("NullableStringOrBytes.GoBytes() = %v, want %v", gotBytes, tt.wantGoBytes)
}
if gotStr := tt.str.StarlarkString(); gotStr != tt.wantStarStr {
t.Errorf("NullableStringOrBytes.StarlarkString() = %v, want %v", gotStr, tt.wantStarStr)
}
if gotStr := tt.str.StarlarkBytes(); gotStr != starlark.Bytes(tt.wantStarStr) {
t.Errorf("NullableStringOrBytes.StarlarkBytes() = %v, want %v", gotStr, starlark.Bytes(tt.wantStarStr))
}
if gotIsNull := tt.str.IsNull(); gotIsNull != tt.wantIsNull {
t.Errorf("NullableStringOrBytes.IsNull() = %v, want %v", gotIsNull, tt.wantIsNull)
Expand Down
4 changes: 2 additions & 2 deletions lib/atom/atom.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var (
)

func newInt(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var value int64 = 0
var value int64
if err := starlark.UnpackArgs(b.Name(), args, kwargs, "value?", &value); err != nil {
return nil, err
}
Expand Down Expand Up @@ -184,7 +184,7 @@ type AtomicString struct {
}

func newString(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var value string = ""
var value string
if err := starlark.UnpackArgs(b.Name(), args, kwargs, "value?", &value); err != nil {
return nil, err
}
Expand Down

0 comments on commit 8c364a9

Please sign in to comment.