Skip to content

Commit

Permalink
chore: add stringify support for Map and Array data (#817)
Browse files Browse the repository at this point in the history
Because

 - we previously lacked stringify support for Map and Array data.

This commit

 - adds stringify support for Map and Array data.
  • Loading branch information
donch1989 authored Nov 5, 2024
1 parent 2cc986b commit 539753e
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/integration-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ jobs:
- name: Install k6
run: |
go install go.k6.io/xk6/cmd/xk6@v${{ env.XK6_VERSION }}
xk6 build v${{ env.K6_VERSION }} --with github.com/grafana/xk6-sql && sudo cp k6 /usr/bin
xk6 build v${{ env.K6_VERSION }} --with github.com/grafana/xk6-sql@v0.4.1 && sudo cp k6 /usr/bin
- name: Checkout (pipeline)
uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.dev
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ RUN --mount=target=. --mount=type=cache,target=/root/.cache/go-build --mount=typ

# k6
RUN --mount=target=. --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/go/pkg GOOS=$TARGETOS GOARCH=$TARGETARCH go install go.k6.io/xk6/cmd/xk6@v${XK6_VERSION}
RUN --mount=target=. --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/go/pkg GOOS=$TARGETOS GOARCH=$TARGETARCH xk6 build v${K6_VERSION} --with github.com/grafana/xk6-sql --output /usr/bin/k6
RUN --mount=target=. --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/go/pkg GOOS=$TARGETOS GOARCH=$TARGETARCH xk6 build v${K6_VERSION} --with github.com/grafana/xk6-sql@v0.4.1 --output /usr/bin/k6

# -- set up Go

Expand Down
14 changes: 14 additions & 0 deletions pkg/data/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package data

import (
"fmt"
"strings"

"google.golang.org/protobuf/types/known/structpb"

Expand Down Expand Up @@ -78,3 +79,16 @@ func (a Array) Equal(other format.Value) bool {
func (a Array) Length() format.Number {
return NewNumberFromInteger(len(a))
}

func (a Array) String() string {
segments := make([]string, 0, len(a))
for _, v := range a {
switch v := v.(type) {
case *stringData:
segments = append(segments, fmt.Sprintf("\"%s\"", v.String()))
default:
segments = append(segments, v.String())
}
}
return fmt.Sprintf("[%s]", strings.Join(segments, ", "))
}
6 changes: 0 additions & 6 deletions pkg/data/format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,20 @@ type Number interface {
Value
Integer() int
Float64() float64
String() string
}

type String interface {
Value
String() string
}

type Boolean interface {
Value
Boolean() bool
String() string
}

type ByteArray interface {
Value
ByteArray() []byte
String() string
}

type File interface {
Expand All @@ -46,13 +42,11 @@ type File interface {
ContentType() (t String)
Filename() (t String)
SourceURL() (t String)
String() string
}

type Document interface {
File

String() string
Text() (val String, err error)
PDF() (val Document, err error)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/data/format/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ type Value interface {
ToStructValue() (v *structpb.Value, err error)
Get(p *path.Path) (v Value, err error)
Equal(other Value) bool
String() string
}
14 changes: 14 additions & 0 deletions pkg/data/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package data

import (
"fmt"
"strings"

"google.golang.org/protobuf/types/known/structpb"

Expand Down Expand Up @@ -67,3 +68,16 @@ func (m Map) Equal(other format.Value) bool {
}
return false
}

func (m Map) String() string {
segments := make([]string, 0, len(m))
for k, v := range m {
switch v := v.(type) {
case *stringData:
segments = append(segments, fmt.Sprintf("\"%s\": \"%s\"", k, v.String()))
default:
segments = append(segments, fmt.Sprintf("\"%s\": %s", k, v.String()))
}
}
return fmt.Sprintf("{%s}", strings.Join(segments, ", "))
}
4 changes: 4 additions & 0 deletions pkg/data/null.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ func (n *nullData) Equal(other format.Value) bool {
}
return false
}

func (n *nullData) String() string {
return "null"
}
10 changes: 5 additions & 5 deletions pkg/data/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ func unmarshalStruct(m Map, v reflect.Value) error {
// unmarshalValue dispatches to type-specific unmarshal functions based on the value type.
func unmarshalValue(val format.Value, field reflect.Value, structField reflect.StructField) error {
switch v := val.(type) {
case format.File, format.Document, format.Image, format.Video, format.Audio:
case *fileData, *documentData, *imageData, *videoData, *audioData:
return unmarshalInterface(v, field, structField)
case format.Boolean:
case *booleanData:
return unmarshalBoolean(v, field)
case format.Number:
case *numberData:
return unmarshalNumber(v, field)
case format.String:
case *stringData:
return unmarshalString(v, field)
case Array:
if field.Type().Implements(reflect.TypeOf((*format.Value)(nil)).Elem()) {
Expand All @@ -95,7 +95,7 @@ func unmarshalValue(val format.Value, field reflect.Value, structField reflect.S
return nil
}
return unmarshalMap(v, field)
case format.Null:
case *nullData:
if field.Type().Implements(reflect.TypeOf((*format.Value)(nil)).Elem()) {
field.Set(reflect.ValueOf(v))
return nil
Expand Down
5 changes: 1 addition & 4 deletions pkg/recipe/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,7 @@ func Render(ctx context.Context, template format.Value, batchIdx int, wfm memory
}
return nil, err
}

if s, ok := v.(format.String); ok {
val += s.String()
}
val += v.String()
s = s[endIdx+1:]
}
return data.NewString(val), nil
Expand Down

0 comments on commit 539753e

Please sign in to comment.