-
Notifications
You must be signed in to change notification settings - Fork 673
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
RodrigoVillar
wants to merge
3
commits into
ava-labs:master
Choose a base branch
from
RodrigoVillar:add-unmarshal-for-exportertype
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+128
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(): | ||
|
@@ -33,8 +41,23 @@ func (t ExporterType) MarshalJSON() ([]byte, error) { | |
return []byte(`"` + t.String() + `"`), nil | ||
} | ||
|
||
func (t *ExporterType) UnmarshalJSON(b []byte) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we add some test vectors for marshal and unmarshal? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done