Skip to content

Commit 55d4c47

Browse files
committed
refactor: reformatted code and solved golangci-lint issues
1 parent 536d8ff commit 55d4c47

File tree

7 files changed

+165
-150
lines changed

7 files changed

+165
-150
lines changed

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ linters:
150150
threshold: 100
151151

152152
nestif:
153-
min-complexity: 4
153+
min-complexity: 5
154154

155155
nilnil:
156156
only-two: true

decoder/protojson/decoder.go

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@ import (
55

66
"google.golang.org/protobuf/encoding/protojson"
77

8-
gojsondecoder "github.com/ralvarezdev/go-json/decoder"
98
goreflect "github.com/ralvarezdev/go-reflect"
9+
10+
gojsondecoder "github.com/ralvarezdev/go-json/decoder"
1011
)
1112

1213
type (
1314
Decoder struct {
1415
unmarshalOptions protojson.UnmarshalOptions
15-
cache bool
16-
cachedMappers map[string]*Mapper
16+
cache bool
17+
cachedMappers map[string]*Mapper
1718
}
18-
19+
1920
// Options are the additional settings for the decoder implementation
2021
Options struct {
2122
// cache indicates whether to cache the precompute unmarshal by reflection functions
@@ -24,13 +25,13 @@ type (
2425
)
2526

2627
// NewOptions creates a new Options instance
27-
//
28+
//
2829
// Parameters:
29-
//
30-
// - cache: indicates whether to cache the precompute unmarshal by reflection functions
31-
//
30+
//
31+
// - cache: indicates whether to cache the precompute unmarshal by reflection functions
32+
//
3233
// Returns:
33-
//
34+
//
3435
// - *Options: the new Options instance
3536
func NewOptions(
3637
cache bool,
@@ -41,10 +42,10 @@ func NewOptions(
4142
}
4243

4344
// NewDecoder creates a new Decoder instance
44-
//
45+
//
4546
// Parameters:
46-
//
47-
// - options: the additional settings for the decoder implementation
47+
//
48+
// - options: the additional settings for the decoder implementation
4849
//
4950
// Returns:
5051
//
@@ -55,7 +56,7 @@ func NewDecoder(options *Options) *Decoder {
5556
if options != nil {
5657
cache = options.cache
5758
}
58-
59+
5960
// Initialize unmarshal options
6061
unmarshalOptions := protojson.UnmarshalOptions{
6162
DiscardUnknown: true,
@@ -92,7 +93,7 @@ func (d Decoder) Decode(
9293
if err != nil {
9394
return err
9495
}
95-
96+
9697
return d.DecodeReader(parsedReader, dest)
9798
}
9899

@@ -119,18 +120,18 @@ func (d Decoder) DecodeReader(
119120
if dest == nil {
120121
return gojsondecoder.ErrNilDestination
121122
}
122-
123+
123124
// Read all body from the reader
124125
body, err := io.ReadAll(reader)
125126
if err != nil {
126127
return err
127128
}
128-
129+
129130
// Check if the cache is enabled and use cached mapper if available
130-
if d.cache && d.cachedMappers != nil {
131+
if d.cache && d.cachedMappers != nil {
131132
// Get the unique type identifier for the destination
132133
uniqueTypeReference := goreflect.UniqueTypeReference(dest)
133-
134+
134135
// Check if there is a cached mapper for the destination type
135136
if mapper, found := d.cachedMappers[uniqueTypeReference]; found {
136137
return mapper.UnmarshalByReflection(
@@ -140,24 +141,24 @@ func (d Decoder) DecodeReader(
140141
)
141142
}
142143
}
143-
144+
144145
// Initialize the cache map if caching is enabled
145146
if d.cache && d.cachedMappers == nil {
146147
d.cachedMappers = make(map[string]*Mapper)
147148
}
148-
149+
149150
// Create a new mapper for the destination type
150151
mapper, err := NewMapper(dest)
151152
if err != nil {
152153
return err
153-
}
154+
}
154155

155156
// Store the mapper in the cache if caching is enabled
156157
if d.cache {
157158
uniqueTypeReference := goreflect.UniqueTypeReference(dest)
158159
d.cachedMappers[uniqueTypeReference] = mapper
159160
}
160-
161+
161162
// Unmarshal the body into the destination using the mapper
162163
return mapper.UnmarshalByReflection(
163164
body,

decoder/protojson/errors.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import (
55
)
66

77
const (
8-
ErrFieldNotHandled = "field not handled on decoding: %s"
8+
ErrFieldNotHandled = "field not handled on decoding: %s"
9+
ErrFieldNotProtoMessage = "field %s is not a proto message"
910
)
1011

1112
var (
12-
ErrNilReader = errors.New("nil reader")
13-
ErrNilMapper = errors.New("decoder mapper is nil")
14-
ErrNilDestinationInstance = errors.New("nil destination instance")
15-
ErrNilDestination = errors.New("nil destination")
16-
)
13+
ErrDestinationNotProtoMessage = errors.New("destination is not a proto message")
14+
ErrNilReader = errors.New("nil reader")
15+
ErrNilMapper = errors.New("decoder mapper is nil")
16+
ErrNilDestinationInstance = errors.New("nil destination instance")
17+
ErrNilDestination = errors.New("nil destination")
18+
)

0 commit comments

Comments
 (0)