Skip to content

Commit

Permalink
add NS support
Browse files Browse the repository at this point in the history
  • Loading branch information
haohanyang committed Oct 21, 2024
1 parent 5e6b058 commit 814c9cb
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
16 changes: 12 additions & 4 deletions pkg/plugin/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,12 @@ func NewColumn(rowIndex int, name string, value *dynamodb.AttributeValue) (*Colu
field = data.NewField(name, nil, make([]*json.RawMessage, rowIndex+1))
field.Set(rowIndex, v)
} else if value.NS != nil {
field = data.NewField(name, nil, make([]*string, rowIndex+1))
field.Set(rowIndex, aws.String("[NS]"))
v, err := numberSetToJson(value)
if err != nil {
return nil, err
}
field = data.NewField(name, nil, make([]*json.RawMessage, rowIndex+1))
field.Set(rowIndex, v)
} else if value.BS != nil {
field = data.NewField(name, nil, make([]*string, rowIndex+1))
field.Set(rowIndex, aws.String("[BS]"))
Expand Down Expand Up @@ -162,10 +166,14 @@ func (c *Column) AppendValue(value *dynamodb.AttributeValue) error {
}
c.Field.Append(v)
} else if value.NS != nil {
if c.Type() != data.FieldTypeNullableString {
if c.Type() != data.FieldTypeNullableJSON {
return fmt.Errorf("field %s should have type %s, but got %s", c.Name, c.Type().ItemTypeString(), "NS")
}
c.Field.Append(aws.String("[NS]"))
v, err := numberSetToJson(value)
if err != nil {
return err
}
c.Field.Append(v)
} else if value.BS != nil {
if c.Type() != data.FieldTypeNullableString {
return fmt.Errorf("field %s should have type %s, but got %s", c.Name, c.Type().ItemTypeString(), "BS")
Expand Down
22 changes: 22 additions & 0 deletions pkg/plugin/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,28 @@ func stringSetToJson(value *dynamodb.AttributeValue) (*json.RawMessage, error) {
return pointer(json.RawMessage(jsonString)), nil
}

func numberSetToJson(value *dynamodb.AttributeValue) (*json.RawMessage, error) {
l := make([]interface{}, len(value.NS))

for idx, n := range value.NS {
i, f, err := parseNumber(*n)
if err != nil {
return nil, err
}
if i != nil {
l[idx] = *i
} else {
l[idx] = *f
}
}

jsonString, err := json.Marshal(l)
if err != nil {
return nil, err
}
return pointer(json.RawMessage(jsonString)), nil
}

func pointer[K any](val K) *K {
return &val
}
24 changes: 24 additions & 0 deletions pkg/plugin/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,4 +368,28 @@ func TestOutputToDataFrame(t *testing.T) {
assertEqual(t, field.Type(), data.FieldTypeNullableJSON)
})

t.Run("NS", func(t *testing.T) {
rows := []DataRow{
{"myNS": &dynamodb.AttributeValue{
NS: []*string{aws.String("1.1"), aws.String("2")},
}},
{"myNS": &dynamodb.AttributeValue{
NS: []*string{aws.String("-2"), aws.String("-3.1")},
}},
}

output, err := outputFromItems(ctx, client, testTableName, rows, "myNS")
if err != nil {
t.Fatal(err)
}

frame, err := OutputToDataFrame("test", output)
if err != nil {
t.Fatal(err)
}
field := frame.Fields[0]
assertEqual(t, field.Name, "myNS")
assertEqual(t, field.Type(), data.FieldTypeNullableJSON)
})

}

0 comments on commit 814c9cb

Please sign in to comment.