Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encoding/yaml: Add support for multi-document serialization #177

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions encoding/yaml/README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
# yaml
yaml provides functions for working with yaml data
yaml provides functions for working with yaml data.

## Functions

#### `dumps(obj) string`
serialize obj to a yaml string
#### `dumps(obj, [obj, ...]) string`
Serialize one or more objects to a yaml string.

If more than one object is provided, the returned string will use
YAML's [Multi-Document](https://yaml.org/spec/1.2.2/#example-two-documents-in-a-stream)
format.

**parameters:**

| name | type | description |
|------|------|-------------|
| `obj` | `object` | input object |
| name | type | description |
| ----- | -------- | --------------- |
| `obj` | `object` | input object(s) |


#### `loads(source) object`
read a source yaml string to a starlark object
Read a source yaml string to a Starlark object.

**parameters:**

| name | type | description |
|------|------|-------------|
| name | type | description |
| -------- | -------- | ------------------------- |
| `source` | `string` | input string of yaml data |


9 changes: 5 additions & 4 deletions encoding/yaml/testdata/test.star
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

load('encoding/yaml.star', 'yaml')
load('assert.star', 'assert')
load("encoding/yaml.star", "yaml")
load("assert.star", "assert")

yaml_list = """- Apple
- Orange
Expand All @@ -9,7 +8,7 @@ yaml_list = """- Apple
"""

native_list = ["Apple", "Orange", "Strawberry", "Mango"]
assert.eq(yaml.loads(yaml_list), native_list)
assert.eq(yaml.loads(yaml_list), native_list)
assert.eq(yaml.dumps(native_list), yaml_list)

yaml_dict = """martin:
Expand All @@ -22,3 +21,5 @@ native_dict = {"martin": {"name": "Martin D'vloper", "job": "Developer", "skill"
assert.eq(yaml.loads(yaml_dict), native_dict)
assert.eq(yaml.dumps(native_dict), yaml_dict)

multidoc_yaml = "---\n".join([yaml_list, yaml_dict])
assert.eq(yaml.dumps(native_list, native_dict), multidoc_yaml)
38 changes: 27 additions & 11 deletions encoding/yaml/yaml.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package yaml

import (
"bytes"
"sync"

"github.com/qri-io/starlib/util"
Expand Down Expand Up @@ -56,24 +57,39 @@ func Loads(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kw

// Dumps serializes a starlark object to a yaml string
func Dumps(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var (
source starlark.Value
)
values := make([]starlark.Value, len(args))
valuePtrs := make([]interface{}, len(args))

err := starlark.UnpackArgs("dumps", args, kwargs, "source", &source)
if err != nil {
return starlark.None, err
for idx, _ := range values {
valuePtrs[idx] = &values[idx]
}

val, err := util.Unmarshal(source)
err := starlark.UnpackPositionalArgs("dumps", args, kwargs, 1, valuePtrs...)
if err != nil {
return starlark.None, err
}

data, err := yaml.Marshal(val)
if err != nil {
return starlark.None, err
buffer := new(bytes.Buffer)
for idx, value := range values {

goValue, err := util.Unmarshal(value)
if err != nil {
return starlark.None, err
}

rawBytes, err := yaml.Marshal(goValue)
if err != nil {
return starlark.None, err
}

buffer.Write(rawBytes)

// Add the YAML document separator if, and only if,
// there are more values to be serialized.
if idx < len(values)-1 {
buffer.WriteString("---\n")
}
}

return starlark.String(string(data)), nil
return starlark.String(buffer.String()), nil
}