Skip to content
Merged
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
30 changes: 30 additions & 0 deletions marshallers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ package publiccode
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"reflect"
"strings"

yaml "github.com/goccy/go-yaml"
"github.com/goccy/go-yaml/ast"
"github.com/goccy/go-yaml/token"
)

type URL url.URL //nolint:recvcheck // String must use value receiver to implement fmt.Stringer on URL
Expand Down Expand Up @@ -91,3 +95,29 @@ func (a *UrlOrUrlArray) UnmarshalYAML(unmarshal func(any) error) error {

return nil
}

// marshalYAML quotes strings that would change when decoded as plain scalars.
func marshalYAML(v any) ([]byte, error) {
node, err := yaml.ValueToNode(v)
if err != nil {
return nil, fmt.Errorf("encoding YAML node: %w", err)
}

ast.Walk(stringQuoter{}, node)

return []byte(node.String() + "\n"), nil
}

type stringQuoter struct{}

//nolint:ireturn // ast.Visitor requires this return type.
func (q stringQuoter) Visit(node ast.Node) ast.Visitor {
if s, ok := node.(*ast.StringNode); ok {
switch strings.ToLower(s.Value) {
case ".nan", ".inf", "+.inf", "-.inf":
s.Token.Type = token.DoubleQuoteType
}
}

return q
}
3 changes: 1 addition & 2 deletions v0.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package publiccode
import (
"fmt"

yaml "github.com/goccy/go-yaml"
urlutil "github.com/publiccodeyml/libpubliccode/v5/internal"
)

Expand Down Expand Up @@ -174,7 +173,7 @@ func (p PublicCodeV0) ToYAML() ([]byte, error) {
p.It = nil
}

b, err := yaml.Marshal(p)
b, err := marshalYAML(p)
if err != nil {
return nil, fmt.Errorf("marshaling to YAML: %w", err)
}
Expand Down
3 changes: 1 addition & 2 deletions v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package publiccode
import (
"fmt"

yaml "github.com/goccy/go-yaml"
urlutil "github.com/publiccodeyml/libpubliccode/v5/internal"
)

Expand Down Expand Up @@ -117,7 +116,7 @@ func (p PublicCodeV1) Version() uint {
}

func (p PublicCodeV1) ToYAML() ([]byte, error) {
b, err := yaml.Marshal(p)
b, err := marshalYAML(p)
if err != nil {
return nil, fmt.Errorf("marshaling to YAML: %w", err)
}
Expand Down
83 changes: 83 additions & 0 deletions yaml_export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package publiccode

import (
"bytes"
"encoding/json"
"os"
"reflect"
"strings"
"testing"

"github.com/goccy/go-yaml"
)

func TestToYAMLPreservesSpecialFloatText(t *testing.T) {
input, err := os.ReadFile("testdata/v0/valid/valid.minimal.yml")
if err != nil {
t.Fatal(err)
}
parser, err := NewParser(ParserConfig{DisableExternalChecks: true})
if err != nil {
t.Fatal(err)
}

for _, value := range []string{
".nan", ".NaN", ".NAN", ".inf", ".Inf", ".INF",
"-.inf", "-.Inf", "-.INF", "+.inf", "+.Inf", "+.INF",
"NaN", "+Inf", "-Inf", "true", "null", "1.0",
} {
t.Run(value, func(t *testing.T) {
quoted, err := json.Marshal(value)
if err != nil {
t.Fatal(err)
}
source := strings.ReplaceAll(string(input), "Medusa", string(quoted))
source = strings.ReplaceAll(source, "Just one feature", string(quoted))
before, err := parser.ParseStream(strings.NewReader(source))
if err != nil {
t.Fatal(err)
}
parsed, ok := before.(PublicCodeV0)
if !ok {
t.Fatalf("parsed type = %T, want PublicCodeV0", before)
}
if parsed.Name != value {
t.Fatalf("parsed name = %q, want %q", parsed.Name, value)
}
exported, err := before.ToYAML()
if err != nil {
t.Fatal(err)
}
after, err := parser.ParseStream(bytes.NewReader(exported))
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(before, after) {
t.Errorf("document changed after YAML export:\nbefore: %#v\nafter: %#v", before, after)
}

// Version 1 is not supported by ParseStream yet, but has its own exporter.
v1 := PublicCodeV1{
Name: value,
Description: map[string]DescV1{
"en": {LocalisedName: &value, Features: &[]string{value}, LongDescription: "First line\nsecond line\n"},
},
}
exported, err = v1.ToYAML()
if err != nil {
t.Fatal(err)
}
var reread PublicCodeV1
if err := yaml.Unmarshal(exported, &reread); err != nil {
t.Fatal(err)
}
if reread.Name != value || reread.Description["en"].LocalisedName == nil ||
*reread.Description["en"].LocalisedName != value ||
reread.Description["en"].Features == nil ||
!reflect.DeepEqual(*reread.Description["en"].Features, []string{value}) ||
reread.Description["en"].LongDescription != v1.Description["en"].LongDescription {
t.Errorf("version 1 document changed after YAML export: %s", exported)
}
})
}
}