Skip to content

Commit

Permalink
chore: add stringify support for Map and Array data
Browse files Browse the repository at this point in the history
  • Loading branch information
donch1989 committed Nov 5, 2024
1 parent f06429c commit ffcfd2f
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 15 deletions.
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 ffcfd2f

Please sign in to comment.