Skip to content

Commit 11dffd6

Browse files
authored
[chore] Change to use wrapper of jsoniter.Iterator to simplify ReadXYZ calls (#13498)
This is a preparation to auto-generate the UnmarshalJSON using pdatagen. By doing this we simplify to have all funcs on the Iterator and not having some on the json package some on the Iterator. Also, this is less error prune since it forces that all numbers are correctly handle the string option. Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
1 parent 95e5ac6 commit 11dffd6

File tree

93 files changed

+890
-870
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+890
-870
lines changed

internal/cmd/pdatagen/internal/templates/message_test.go.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func Test{{ .structName }}_MarshalAndUnmarshalJSON(t *testing.T) {
4646
{{- else }}
4747
src.marshalJSONStream(stream)
4848
{{- end }}
49-
require.NoError(t, stream.Error)
49+
require.NoError(t, stream.Error())
5050

5151
iter := json.BorrowIterator(stream.Buffer())
5252
defer json.ReturnIterator(iter)
@@ -56,7 +56,7 @@ func Test{{ .structName }}_MarshalAndUnmarshalJSON(t *testing.T) {
5656
{{- else }}
5757
dest.unmarshalJSONIter(iter)
5858
{{- end }}
59-
require.NoError(t, iter.Error)
59+
require.NoError(t, iter.Error())
6060

6161
assert.Equal(t, src, dest)
6262
}

pdata/internal/data/bytesid.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ package data // import "go.opentelemetry.io/collector/pdata/internal/data"
66
import (
77
"encoding/hex"
88

9-
jsoniter "github.com/json-iterator/go"
9+
"go.opentelemetry.io/collector/pdata/internal/json"
1010
)
1111

1212
// unmarshalJSON inflates trace id from hex string, possibly enclosed in quotes.
1313
// Called by Protobuf JSON deserialization.
14-
func unmarshalJSON(dst []byte, iter *jsoniter.Iterator) {
14+
func unmarshalJSON(dst []byte, iter *json.Iterator) {
1515
src := iter.ReadStringAsSlice()
1616
if len(src) == 0 {
1717
return

pdata/internal/data/bytesid_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,23 @@ func TestUnmarshalJSON(t *testing.T) {
1818

1919
id := [16]byte{}
2020
unmarshalJSON(id[:], iter.ResetBytes([]byte(`""`)))
21-
require.NoError(t, iter.Error)
21+
require.NoError(t, iter.Error())
2222
assert.Equal(t, [16]byte{}, id)
2323

2424
idBytes := [16]byte{0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78}
2525
unmarshalJSON(id[:], iter.ResetBytes([]byte(`"12345678123456781234567812345678"`)))
26-
require.NoError(t, iter.Error)
26+
require.NoError(t, iter.Error())
2727
assert.Equal(t, idBytes, id)
2828

2929
unmarshalJSON(id[:], iter.ResetBytes([]byte(`"nothex"`)))
30-
require.Error(t, iter.Error)
30+
require.Error(t, iter.Error())
3131

3232
unmarshalJSON(id[:], iter.ResetBytes([]byte(`"1"`)))
33-
require.Error(t, iter.Error)
33+
require.Error(t, iter.Error())
3434

3535
unmarshalJSON(id[:], iter.ResetBytes([]byte(`"123"`)))
36-
require.Error(t, iter.Error)
36+
require.Error(t, iter.Error())
3737

3838
unmarshalJSON(id[:], iter.ResetBytes([]byte(`"`)))
39-
require.Error(t, iter.Error)
39+
require.Error(t, iter.Error())
4040
}

pdata/internal/data/profileid.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"errors"
99

1010
"github.com/gogo/protobuf/proto"
11-
jsoniter "github.com/json-iterator/go"
1211

1312
"go.opentelemetry.io/collector/pdata/internal/json"
1413
)
@@ -73,7 +72,7 @@ func (tid ProfileID) MarshalJSONStream(dest *json.Stream) {
7372
}
7473

7574
// UnmarshalJSONIter decodes ProfileID from hex string.
76-
func (tid *ProfileID) UnmarshalJSONIter(iter *jsoniter.Iterator) {
75+
func (tid *ProfileID) UnmarshalJSONIter(iter *json.Iterator) {
7776
*tid = [profileIDSize]byte{}
7877
unmarshalJSON(tid[:], iter)
7978
}

pdata/internal/data/profileid_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ func TestProfileIDMarshalAndUnmarshalJSON(t *testing.T) {
6363
defer json.ReturnStream(stream)
6464
src := ProfileID([16]byte{0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78})
6565
src.MarshalJSONStream(stream)
66-
require.NoError(t, stream.Error)
66+
require.NoError(t, stream.Error())
6767

6868
iter := json.BorrowIterator(stream.Buffer())
6969
defer json.ReturnIterator(iter)
7070
dest := ProfileID{}
7171
dest.UnmarshalJSONIter(iter)
72-
require.NoError(t, iter.Error)
72+
require.NoError(t, iter.Error())
7373

7474
assert.Equal(t, src, dest)
7575
}

pdata/internal/data/spanid.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"errors"
99

1010
"github.com/gogo/protobuf/proto"
11-
jsoniter "github.com/json-iterator/go"
1211

1312
"go.opentelemetry.io/collector/pdata/internal/json"
1413
)
@@ -73,7 +72,7 @@ func (sid SpanID) MarshalJSONStream(dest *json.Stream) {
7372
}
7473

7574
// UnmarshalJSONIter decodes SpanID from hex string.
76-
func (sid *SpanID) UnmarshalJSONIter(iter *jsoniter.Iterator) {
75+
func (sid *SpanID) UnmarshalJSONIter(iter *json.Iterator) {
7776
*sid = [spanIDSize]byte{}
7877
unmarshalJSON(sid[:], iter)
7978
}

pdata/internal/data/spanid_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ func TestSpanIDMarshalAndUnmarshalJSON(t *testing.T) {
6666
defer json.ReturnStream(stream)
6767
src := SpanID([8]byte{0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78})
6868
src.MarshalJSONStream(stream)
69-
require.NoError(t, stream.Error)
69+
require.NoError(t, stream.Error())
7070

7171
iter := json.BorrowIterator(stream.Buffer())
7272
defer json.ReturnIterator(iter)
7373
dest := SpanID{}
7474
dest.UnmarshalJSONIter(iter)
75-
require.NoError(t, iter.Error)
75+
require.NoError(t, iter.Error())
7676

7777
assert.Equal(t, src, dest)
7878
}

pdata/internal/data/traceid.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"errors"
99

1010
"github.com/gogo/protobuf/proto"
11-
jsoniter "github.com/json-iterator/go"
1211

1312
"go.opentelemetry.io/collector/pdata/internal/json"
1413
)
@@ -73,7 +72,7 @@ func (tid TraceID) MarshalJSONStream(dest *json.Stream) {
7372
}
7473

7574
// UnmarshalJSONIter decodes TraceID from hex string.
76-
func (tid *TraceID) UnmarshalJSONIter(iter *jsoniter.Iterator) {
75+
func (tid *TraceID) UnmarshalJSONIter(iter *json.Iterator) {
7776
*tid = [profileIDSize]byte{}
7877
unmarshalJSON(tid[:], iter)
7978
}

pdata/internal/data/traceid_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ func TestTraceIDMarshalAndUnmarshalJSON(t *testing.T) {
6363
defer json.ReturnStream(stream)
6464
src := TraceID([16]byte{0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78})
6565
src.MarshalJSONStream(stream)
66-
require.NoError(t, stream.Error)
66+
require.NoError(t, stream.Error())
6767

6868
iter := json.BorrowIterator(stream.Buffer())
6969
defer json.ReturnIterator(iter)
7070
dest := TraceID{}
7171
dest.UnmarshalJSONIter(iter)
72-
require.NoError(t, iter.Error)
72+
require.NoError(t, iter.Error())
7373

7474
assert.Equal(t, src, dest)
7575
}

pdata/internal/json/enum.go

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)