Skip to content

Commit

Permalink
sdktypes: make NewInteger and NewFloat easier to use (#330)
Browse files Browse the repository at this point in the history
now accept any integer or flow, no need for explicit casting
  • Loading branch information
itayd committed May 23, 2024
1 parent 2ba1a50 commit 1fd0d39
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion integrations/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ func toStruct(r *http.Response) (sdktypes.Value, error) {
sdktypes.NewStringValue("http_response"),
map[string]sdktypes.Value{
"url": sdktypes.NewStringValue(r.Request.URL.String()),
"status_code": sdktypes.NewIntegerValue(int64(r.StatusCode)),
"status_code": sdktypes.NewIntegerValue(r.StatusCode),
"headers": kittehs.Must1(sdktypes.NewDictValue(
kittehs.TransformMapToList(r.Header, func(k string, vs []string) sdktypes.DictItem {
return sdktypes.DictItem{
Expand Down
2 changes: 1 addition & 1 deletion internal/backend/akmodules/os/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func execute(ctx context.Context, name string, args []string, write map[string]a
retCtor,
map[string]sdktypes.Value{
"output": sdktypes.NewStringValue(string(out)),
"exitcode": sdktypes.NewIntegerValue(int64(rc)),
"exitcode": sdktypes.NewIntegerValue(rc),
"files": sdktypes.NewDictValueFromStringMap(files),
},
)
Expand Down
12 changes: 6 additions & 6 deletions internal/backend/akmodules/time/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ var ExecutorID = sdktypes.NewExecutorID(fixtures.NewBuiltinIntegrationID("time")
func New() sdkexecutor.Executor {
return fixtures.NewBuiltinExecutor(
ExecutorID,
sdkmodule.ExportValue("nanosecond", sdkmodule.WithValue(sdktypes.NewIntegerValue(int64(time.Nanosecond)))),
sdkmodule.ExportValue("microsecond", sdkmodule.WithValue(sdktypes.NewIntegerValue(int64(time.Microsecond)))),
sdkmodule.ExportValue("millisecond", sdkmodule.WithValue(sdktypes.NewIntegerValue(int64(time.Millisecond)))),
sdkmodule.ExportValue("second", sdkmodule.WithValue(sdktypes.NewIntegerValue(int64(time.Second)))),
sdkmodule.ExportValue("minute", sdkmodule.WithValue(sdktypes.NewIntegerValue(int64(time.Minute)))),
sdkmodule.ExportValue("hour", sdkmodule.WithValue(sdktypes.NewIntegerValue(int64(time.Hour)))),
sdkmodule.ExportValue("nanosecond", sdkmodule.WithValue(sdktypes.NewIntegerValue(time.Nanosecond))),
sdkmodule.ExportValue("microsecond", sdkmodule.WithValue(sdktypes.NewIntegerValue(time.Microsecond))),
sdkmodule.ExportValue("millisecond", sdkmodule.WithValue(sdktypes.NewIntegerValue(time.Millisecond))),
sdkmodule.ExportValue("second", sdkmodule.WithValue(sdktypes.NewIntegerValue(time.Second))),
sdkmodule.ExportValue("minute", sdkmodule.WithValue(sdktypes.NewIntegerValue(time.Minute))),
sdkmodule.ExportValue("hour", sdkmodule.WithValue(sdktypes.NewIntegerValue(time.Hour))),

sdkmodule.ExportFunction("time", newTime, sdkmodule.WithArgs("year?", "month?", "day?", "hour?", "minute?", "second?", "nanosecond?", "location?"), sdkmodule.WithFlag(sdktypes.PureFunctionFlag)),
sdkmodule.ExportFunction("parse_time", parseTime, sdkmodule.WithArgs("s", "format?", "location?"), sdkmodule.WithFlag(sdktypes.PureFunctionFlag)),
Expand Down
2 changes: 1 addition & 1 deletion runtimes/starlarkrt/internal/values/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (vctx *Context) FromStarlarkValue(v starlark.Value) (sdktypes.Value, error)
// TODO: Not sure that starlark's assumption that string and bytes are the same in go.
return sdktypes.NewBytesValue([]byte(v)), nil
case starlark.Float:
return sdktypes.NewFloatValue(float64(v)), nil
return sdktypes.NewFloatValue(v), nil
case *Symbol:
s, err := sdktypes.ParseSymbol(string(*v))
if err != nil {
Expand Down
10 changes: 6 additions & 4 deletions sdk/sdktypes/value_scalar.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"errors"
"time"

"golang.org/x/exp/constraints"

"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/timestamppb"

Expand Down Expand Up @@ -118,8 +120,8 @@ func (IntegerValue) isConcreteValue() {}

func (s IntegerValue) Value() int64 { return s.read().V }

func NewIntegerValue(v int64) Value {
return forceFromProto[Value](&ValuePB{Integer: &IntegerValuePB{V: v}})
func NewIntegerValue[T constraints.Integer](v T) Value {
return forceFromProto[Value](&ValuePB{Integer: &IntegerValuePB{V: int64(v)}})
}

func (v Value) IsInteger() bool { return v.read().Integer != nil }
Expand Down Expand Up @@ -174,8 +176,8 @@ func (FloatValue) isConcreteValue() {}

func (s FloatValue) Value() float64 { return s.read().V }

func NewFloatValue(v float64) Value {
return forceFromProto[Value](&ValuePB{Float: &FloatValuePB{V: v}})
func NewFloatValue[T constraints.Float](v T) Value {
return forceFromProto[Value](&ValuePB{Float: &FloatValuePB{V: float64(v)}})
}

func (v Value) IsFloat() bool { return v.read().Float != nil }
Expand Down

0 comments on commit 1fd0d39

Please sign in to comment.