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

Add unmarshal for ExporterType #3565

Open
wants to merge 3 commits 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
34 changes: 32 additions & 2 deletions trace/exporter_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,22 @@ import (
)

const (
GRPC ExporterType = iota + 1
NoOp ExporterType = iota
GRPC
HTTP
)

var errUnknownExporterType = errors.New("unknown exporter type")
var (
errUnknownExporterType = errors.New("unknown exporter type")
errInvalidFormat = errors.New("invalid format")
)

func ExporterTypeFromString(exporterTypeStr string) (ExporterType, error) {
switch strings.ToLower(exporterTypeStr) {
case NoOp.String():
return 0, nil
case "null":
return 0, nil
case GRPC.String():
return GRPC, nil
case HTTP.String():
Expand All @@ -33,8 +41,23 @@ func (t ExporterType) MarshalJSON() ([]byte, error) {
return []byte(`"` + t.String() + `"`), nil
}

func (t *ExporterType) UnmarshalJSON(b []byte) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By convention, "null" == string(b) should be a noop.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add some test vectors for marshal and unmarshal?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

exporterTypeStr, err := stripQuotes(string(b))
if err != nil {
return err
}
exporterType, err := ExporterTypeFromString(exporterTypeStr)
if err != nil {
return err
}
*t = exporterType
return nil
}

func (t ExporterType) String() string {
switch t {
case NoOp:
return ""
case GRPC:
return "grpc"
case HTTP:
Expand All @@ -43,3 +66,10 @@ func (t ExporterType) String() string {
return "unknown"
}
}

func stripQuotes(s string) (string, error) {
if len(s) < 2 || s[0] != '"' || s[len(s)-1] != '"' {
return "", errInvalidFormat
}
return s[1 : len(s)-1], nil
}
96 changes: 96 additions & 0 deletions trace/exporter_type_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package trace

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/require"
)

func TestMarshalUnmarshal(t *testing.T) {
tests := []struct {
Name string
ExporterType ExporterType
ExpectedErr error
}{
{
Name: "GRPC",
ExporterType: GRPC,
},
{
Name: "HTTP",
ExporterType: HTTP,
},
{
Name: "NoOp",
ExporterType: NoOp,
},
}

for _, tt := range tests {
t.Run(tt.Name, func(t *testing.T) {
require := require.New(t)

b, err := json.Marshal(tt.ExporterType)
require.NoError(err)

var et ExporterType
require.NoError(json.Unmarshal(b, &et))

require.Equal(tt.ExporterType, et)
})
}
}

func TestUnmarshal(t *testing.T) {
tests := []struct {
Name string
Str string
ExpectedError error
}{
{
Name: "NoQuotes",
Str: "grpc",
ExpectedError: errInvalidFormat,
},
{
Name: "SingleLeftQuote",
Str: "\"grpc",
ExpectedError: errInvalidFormat,
},
{
Name: "SingleRightQuote",
Str: "grpc\"",
ExpectedError: errInvalidFormat,
},
{
Name: "MultipleQuotes",
Str: "\"\"grpc\"\"\"",
ExpectedError: errUnknownExporterType,
},
{
Name: "NullString",
Str: "\"null\"",
ExpectedError: nil,
},
{
Name: "EmptyString",
Str: "\"\"",
ExpectedError: nil,
},
}

for _, tt := range tests {
t.Run(tt.Name, func(t *testing.T) {
require := require.New(t)

var et ExporterType

err := et.UnmarshalJSON([]byte(tt.Str))
require.ErrorIs(err, tt.ExpectedError)
})
}
}