Skip to content

Commit

Permalink
chore: move jsonyaml to separate package
Browse files Browse the repository at this point in the history
Signed-off-by: Radoslav Dimitrov <radoslav@stacklok.com>
  • Loading branch information
rdimitrov committed Oct 2, 2023
1 parent 959361e commit 9ba5cf8
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 12 deletions.
4 changes: 2 additions & 2 deletions cmd/dev/app/rule_type/rule_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
"github.com/stacklok/mediator/internal/engine"
"github.com/stacklok/mediator/internal/engine/eval/rego"
"github.com/stacklok/mediator/internal/providers"
"github.com/stacklok/mediator/internal/util"
"github.com/stacklok/mediator/internal/util/jsonyaml"
mediatorv1 "github.com/stacklok/mediator/pkg/api/protobuf/go/mediator/v1"
)

Expand Down Expand Up @@ -186,7 +186,7 @@ func readEntityFromFile(fpath string, entType mediatorv1.Entity) (protoreflect.P

// We transcode to JSON so we can decode it straight to the protobuf structure
w := &bytes.Buffer{}
if err := util.TranscodeYAMLToJSON(f, w); err != nil {
if err := jsonyaml.TranscodeYAMLToJSON(f, w); err != nil {
return nil, fmt.Errorf("error converting yaml to json: %w", err)
}

Expand Down
4 changes: 2 additions & 2 deletions internal/engine/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

"github.com/stacklok/mediator/internal/db"
"github.com/stacklok/mediator/internal/entities"
"github.com/stacklok/mediator/internal/util"
"github.com/stacklok/mediator/internal/util/jsonyaml"
pb "github.com/stacklok/mediator/pkg/api/protobuf/go/mediator/v1"
)

Expand Down Expand Up @@ -56,7 +56,7 @@ func (e *RuleValidationError) Error() string {
// ParseYAML parses a YAML pipeline policy and validates it
func ParseYAML(r io.Reader) (*pb.Policy, error) {
w := &bytes.Buffer{}
if err := util.TranscodeYAMLToJSON(r, w); err != nil {
if err := jsonyaml.TranscodeYAMLToJSON(r, w); err != nil {
return nil, fmt.Errorf("error converting yaml to json: %w", err)
}
return ParseJSON(w)
Expand Down
3 changes: 2 additions & 1 deletion internal/util/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import (
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/stacklok/mediator/internal/db"
"github.com/stacklok/mediator/internal/util/jsonyaml"
pb "github.com/stacklok/mediator/pkg/api/protobuf/go/mediator/v1"
)

Expand Down Expand Up @@ -328,7 +329,7 @@ func GetYamlFromProto(msg protoreflect.ProtoMessage) (string, error) {
if err != nil {
return "", err
}
yamlResult, err := ConvertJsonToYaml(rawMsg)
yamlResult, err := jsonyaml.ConvertJsonToYaml(rawMsg)
if err != nil {
return "", err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
// It does make a good example of how to use the generated client code
// for others to use as a reference.

package util
// Package jsonyaml contains utility functions for converting to/from json and yaml
package jsonyaml

import (
"bytes"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
// It does make a good example of how to use the generated client code
// for others to use as a reference.

package util_test
package jsonyaml_test

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/stacklok/mediator/internal/util"
"github.com/stacklok/mediator/internal/util/jsonyaml"
)

func TestConvertYAMLToJSON(t *testing.T) {
Expand Down Expand Up @@ -69,7 +69,7 @@ bar:
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

got, err := util.ConvertYamlToJson(tt.yamlCase)
got, err := jsonyaml.ConvertYamlToJson(tt.yamlCase)

if tt.wantErr {
assert.Error(t, err, "expected error")
Expand Down Expand Up @@ -121,7 +121,7 @@ foo: bar
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

got, err := util.ConvertJsonToYaml([]byte(tt.jsonCase))
got, err := jsonyaml.ConvertJsonToYaml([]byte(tt.jsonCase))

if tt.wantErr {
assert.Error(t, err, "expected error")
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/protobuf/go/mediator/v1/rule_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ import (
"fmt"
"io"

"github.com/stacklok/mediator/internal/util"
"github.com/stacklok/mediator/internal/util/jsonyaml"
)

// ParseRuleType parses a rule type from a reader
func ParseRuleType(r io.Reader) (*RuleType, error) {
// We transcode to JSON so we can decode it straight to the protobuf structure
w := &bytes.Buffer{}
if err := util.TranscodeYAMLToJSON(r, w); err != nil {
if err := jsonyaml.TranscodeYAMLToJSON(r, w); err != nil {
return nil, fmt.Errorf("error converting yaml to json: %w", err)
}

Expand Down

0 comments on commit 9ba5cf8

Please sign in to comment.