Skip to content

Commit

Permalink
Fix yaml serialization from pb (#221)
Browse files Browse the repository at this point in the history
fix(api/schemas): remove owner from mandatory fields
fix(yaml): Protobuf to YAML conversion

Signed-off-by: Thibault Normand <[email protected]>
  • Loading branch information
Zenithar authored May 10, 2023
1 parent 5dd9f34 commit 090fc3c
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 11 deletions.
4 changes: 2 additions & 2 deletions api/jsonschema/harp.bundle.v1/RuleSet.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@
"description": "RuleSet name."
},
"owner": {
"type": "string",
"type": ["string", "null"],
"description": "RuleSet owner."
},
"description": {
"type": "string",
"description": "Short description for ruleset."
}
},
"required": ["name", "owner", "description"],
"required": ["name", "description"],
"additionalProperties": false,
"type": "object",
"title": "Rule Set Meta",
Expand Down
31 changes: 31 additions & 0 deletions pkg/sdk/convert/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"io"
"reflect"

"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"sigs.k8s.io/yaml"

"github.com/elastic/harp/pkg/sdk/types"
Expand All @@ -47,6 +49,35 @@ func YAMLtoJSON(r io.Reader) (io.Reader, error) {
return jsonReader, nil
}

// PBtoYAML converts a protobuf object to a YAML representation
func PBtoYAML(msg proto.Message) ([]byte, error) {
// Check arguments
if types.IsNil(msg) {
return nil, fmt.Errorf("msg is nil")
}

// Encode protbuf message as JSON
pb, err := protojson.Marshal(msg)
if err != nil {
return nil, fmt.Errorf("unable to encode protbuf message to JSON: %w", err)
}

// Decode input as JSON
var jsonObj interface{}
if errDecode := yaml.Unmarshal(pb, &jsonObj); errDecode != nil {
return nil, fmt.Errorf("unable to decode JSON input: %w", errDecode)
}

// Marshal as YAML
out, errEncode := yaml.Marshal(jsonObj)
if errEncode != nil {
return nil, fmt.Errorf("unable to produce YAML output: %w", errEncode)
}

// No error
return out, nil
}

// -----------------------------------------------------------------------------

// loadFromYAML reads YAML definition and returns the PB struct.
Expand Down
32 changes: 32 additions & 0 deletions pkg/sdk/convert/yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,40 @@ package convert
import (
"reflect"
"testing"

bundlev1 "github.com/elastic/harp/api/gen/go/harp/bundle/v1"
"github.com/google/go-cmp/cmp"
)

func Test_PBtoYAML(t *testing.T) {
spec := &bundlev1.Patch{
ApiVersion: "harp.elastic.co/v1",
Kind: "BundlePatch",
Meta: &bundlev1.PatchMeta{
Name: "test-patch",
},
Spec: &bundlev1.PatchSpec{
Rules: []*bundlev1.PatchRule{
{
Package: &bundlev1.PatchPackage{},
Selector: &bundlev1.PatchSelector{},
},
},
},
}

expectedOutput := []byte("apiVersion: harp.elastic.co/v1\nkind: BundlePatch\nmeta:\n name: test-patch\nspec:\n rules:\n - package: {}\n selector: {}\n")

out, err := PBtoYAML(spec)
if err != nil {
t.Error(err)
}

if report := cmp.Diff(string(out), string(expectedOutput)); report != "" {
t.Errorf("unexpected conversion output:\n%v", report)
}
}

func Test_convertMapStringInterface(t *testing.T) {
type args struct {
val interface{}
Expand Down
7 changes: 3 additions & 4 deletions pkg/tasks/bundle/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ import (
"errors"
"fmt"

"sigs.k8s.io/yaml"

"github.com/elastic/harp/pkg/bundle"
"github.com/elastic/harp/pkg/bundle/compare"
"github.com/elastic/harp/pkg/sdk/convert"
"github.com/elastic/harp/pkg/sdk/types"
"github.com/elastic/harp/pkg/tasks"
)
Expand Down Expand Up @@ -100,8 +99,8 @@ func (t *DiffTask) Run(ctx context.Context) error {
return fmt.Errorf("unable to convert oplog as a bundle patch: %w", err)
}

// Marshal YAML Patch
out, err := yaml.Marshal(patch)
// Marshal as YAML
out, err := convert.PBtoYAML(patch)
if err != nil {
return fmt.Errorf("unable to marshal patch as YAML: %w", err)
}
Expand Down
9 changes: 4 additions & 5 deletions pkg/tasks/to/ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ import (
"context"
"fmt"

"sigs.k8s.io/yaml"

"github.com/elastic/harp/pkg/bundle"
"github.com/elastic/harp/pkg/bundle/ruleset"
"github.com/elastic/harp/pkg/sdk/convert"
"github.com/elastic/harp/pkg/tasks"
)

Expand Down Expand Up @@ -55,9 +54,9 @@ func (t *RuleSetTask) Run(ctx context.Context) error {
}

// Marshal as YAML
ruleSetSpec, err := yaml.Marshal(rs)
out, err := convert.PBtoYAML(rs)
if err != nil {
return fmt.Errorf("unable to generate YAML descriptor: %w", err)
return fmt.Errorf("unable to marshal as YAML: %w", err)
}

// Create output writer
Expand All @@ -67,7 +66,7 @@ func (t *RuleSetTask) Run(ctx context.Context) error {
}

// Write output
fmt.Fprintln(writer, string(ruleSetSpec))
fmt.Fprintln(writer, string(out))

// No error
return nil
Expand Down

0 comments on commit 090fc3c

Please sign in to comment.