-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove unnecessary duplicate code and allocations for reading enums i…
…n JSON Signed-off-by: Bogdan <[email protected]>
- Loading branch information
1 parent
22ca5e0
commit acbd295
Showing
7 changed files
with
129 additions
and
128 deletions.
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
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,40 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package json // import "go.opentelemetry.io/collector/pdata/internal/json" | ||
|
||
import ( | ||
jsoniter "github.com/json-iterator/go" | ||
) | ||
|
||
// ReadEnumValue returns the enum integer value representation. Accepts both enum names and enum integer values. | ||
// See https://developers.google.com/protocol-buffers/docs/proto3#json. | ||
func ReadEnumValue(iter *jsoniter.Iterator, valueMap map[string]int32) int32 { | ||
switch iter.WhatIsNext() { | ||
case jsoniter.NumberValue: | ||
return iter.ReadInt32() | ||
case jsoniter.StringValue: | ||
val, ok := valueMap[iter.ReadString()] | ||
// Same behavior with official protbuf JSON decoder, | ||
// see https://github.com/open-telemetry/opentelemetry-proto-go/pull/81 | ||
if !ok { | ||
iter.ReportError("ReadEnumValue", "unknown string value") | ||
return 0 | ||
} | ||
return val | ||
default: | ||
iter.ReportError("ReadEnumValue", "unsupported value type") | ||
return 0 | ||
} | ||
} |
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,85 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package json | ||
|
||
import ( | ||
"testing" | ||
|
||
jsoniter "github.com/json-iterator/go" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestReadEnumValue(t *testing.T) { | ||
valueMap := map[string]int32{ | ||
"undefined": 0, | ||
"foo": 1, | ||
"bar": 2, | ||
} | ||
tests := []struct { | ||
name string | ||
jsonStr string | ||
want int32 | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "foo string", | ||
jsonStr: "\"foo\"\n", | ||
want: 1, | ||
}, | ||
{ | ||
name: "foo number", | ||
jsonStr: "1\n", | ||
want: 1, | ||
}, | ||
{ | ||
name: "unknown number", | ||
jsonStr: "5\n", | ||
want: 5, | ||
}, | ||
{ | ||
name: "bar string", | ||
jsonStr: "\"bar\"\n", | ||
want: 2, | ||
}, | ||
{ | ||
name: "bar number", | ||
jsonStr: "2\n", | ||
want: 2, | ||
}, | ||
{ | ||
name: "unknown string", | ||
jsonStr: "\"baz\"\n", | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "wrong type", | ||
jsonStr: "true", | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
iter := jsoniter.ConfigFastest.BorrowIterator([]byte(tt.jsonStr)) | ||
defer jsoniter.ConfigFastest.ReturnIterator(iter) | ||
val := ReadEnumValue(iter, valueMap) | ||
if tt.wantErr { | ||
assert.Error(t, iter.Error) | ||
return | ||
} | ||
assert.NoError(t, iter.Error) | ||
assert.Equal(t, tt.want, val) | ||
}) | ||
} | ||
} |
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
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
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
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