Skip to content

Commit af373fe

Browse files
committed
feat: forked from "github.com/ralvarezdev/go-net"
1 parent 70f9cbd commit af373fe

File tree

15 files changed

+856
-0
lines changed

15 files changed

+856
-0
lines changed

decoder/errors.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package decoder
2+
3+
import (
4+
"errors"
5+
)
6+
7+
var (
8+
ErrInvalidInstance = errors.New("invalid instance provided to create a reader")
9+
)

decoder/interfaces.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package decoder
2+
3+
import (
4+
"io"
5+
)
6+
7+
type (
8+
// Decoder interface
9+
Decoder interface {
10+
Decode(
11+
body interface{},
12+
dest interface{},
13+
) error
14+
DecodeReader(
15+
reader io.Reader,
16+
dest interface{},
17+
) error
18+
}
19+
)

decoder/json/decoder.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package json
2+
3+
import (
4+
"encoding/json"
5+
"io"
6+
7+
gojsondecoder "github.com/ralvarezdev/go-json/decoder"
8+
)
9+
10+
type (
11+
// Decoder struct
12+
Decoder struct{}
13+
)
14+
15+
// NewDecoder creates a new JSON decoder
16+
//
17+
// Returns:
18+
//
19+
// - *Decoder: The default decoder
20+
func NewDecoder() *Decoder {
21+
return &Decoder{}
22+
}
23+
24+
// Decode decodes the JSON body from an any value and stores it in the destination
25+
//
26+
// Parameters:
27+
//
28+
// - body: The body to decode
29+
// - dest: The destination to store the decoded body
30+
//
31+
// Returns:
32+
//
33+
// - error: The error if any
34+
func (d Decoder) Decode(
35+
body interface{},
36+
dest interface{},
37+
) error {
38+
// Check the body type
39+
reader, err := gojsondecoder.ToReader(body)
40+
if err != nil {
41+
return err
42+
}
43+
return d.DecodeReader(reader, dest)
44+
}
45+
46+
// DecodeReader decodes the JSON body and stores it in the destination
47+
//
48+
// Parameters:
49+
//
50+
// - reader: The reader to read the body from
51+
// - dest: The destination to store the decoded body
52+
//
53+
// Returns:
54+
//
55+
// - error: The error if any
56+
func (d Decoder) DecodeReader(
57+
reader io.Reader,
58+
dest interface{},
59+
) (err error) {
60+
// Check the decoder destination
61+
if dest == nil {
62+
return ErrNilDestination
63+
}
64+
65+
// Get the body of the request
66+
body, err := io.ReadAll(reader)
67+
if err != nil {
68+
return err
69+
}
70+
71+
// Decode JSON body into destination
72+
return json.Unmarshal(body, dest)
73+
}

decoder/json/errors.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package json
2+
3+
import (
4+
"errors"
5+
)
6+
7+
var (
8+
ErrCodeNilDestination string
9+
ErrCodeFailedToReadBody string
10+
)
11+
12+
var (
13+
ErrNilDestination = errors.New("json destination is nil")
14+
)

decoder/json/stream_decoder.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package json
2+
3+
import (
4+
"encoding/json"
5+
"io"
6+
7+
gojsondecoder "github.com/ralvarezdev/go-json/decoder"
8+
)
9+
10+
type (
11+
// StreamDecoder is the JSON decoder struct
12+
StreamDecoder struct{}
13+
)
14+
15+
// NewStreamDecoder creates a new JSON decoder
16+
//
17+
// Returns:
18+
//
19+
// - *StreamDecoder: The default decoder
20+
func NewStreamDecoder() *StreamDecoder {
21+
return &StreamDecoder{}
22+
}
23+
24+
// Decode decodes the JSON body from an any value and stores it in the destination
25+
//
26+
// Parameters:
27+
//
28+
// - body: The body to decode
29+
// - dest: The destination to store the decoded body
30+
//
31+
// Returns:
32+
//
33+
// - error: The error if any
34+
func (s StreamDecoder) Decode(
35+
body interface{},
36+
dest interface{},
37+
) error {
38+
// Check the body type
39+
reader, err := gojsondecoder.ToReader(body)
40+
if err != nil {
41+
return err
42+
}
43+
return s.DecodeReader(reader, dest)
44+
}
45+
46+
// DecodeReader decodes a JSON body from a reader into a destination
47+
//
48+
// Parameters:
49+
//
50+
// - reader: The reader to read the body from
51+
// - dest: The destination to store the decoded body
52+
//
53+
// Returns:
54+
//
55+
// - error: The error if any
56+
func (s StreamDecoder) DecodeReader(
57+
reader io.Reader,
58+
dest interface{},
59+
) error {
60+
// Check the decoder destination
61+
if dest == nil {
62+
return ErrNilDestination
63+
}
64+
65+
// Create the stream decoder
66+
decoder := json.NewDecoder(reader)
67+
decoder.DisallowUnknownFields()
68+
69+
// Decode JSON body into destination
70+
return decoder.Decode(dest)
71+
}

decoder/protojson/decoder.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package protojson
2+
3+
import (
4+
"io"
5+
6+
gojsondecoder "github.com/ralvarezdev/go-json/decoder"
7+
"google.golang.org/protobuf/encoding/protojson"
8+
)
9+
10+
type (
11+
Decoder struct {
12+
unmarshalOptions protojson.UnmarshalOptions
13+
}
14+
)
15+
16+
// NewDecoder creates a new Decoder instance
17+
//
18+
// Returns:
19+
//
20+
// - *Decoder: The decoder instance
21+
func NewDecoder() *Decoder {
22+
// Initialize unmarshal options
23+
unmarshalOptions := protojson.UnmarshalOptions{
24+
DiscardUnknown: true,
25+
AllowPartial: true,
26+
}
27+
28+
return &Decoder{
29+
unmarshalOptions: unmarshalOptions,
30+
}
31+
}
32+
33+
// Decode decodes the JSON body from an any value and stores it in the destination
34+
//
35+
// Parameters:
36+
//
37+
// - body: The body to decode
38+
// - dest: The destination to store the decoded body
39+
//
40+
// Returns:
41+
//
42+
// - error: The error if any
43+
func (d Decoder) Decode(
44+
body interface{},
45+
dest interface{},
46+
) error {
47+
// Check the body type
48+
reader, err := gojsondecoder.ToReader(body)
49+
if err != nil {
50+
return err
51+
}
52+
return d.DecodeReader(reader, dest)
53+
}
54+
55+
// DecodeReader decodes a JSON body from a reader into a destination
56+
//
57+
// Parameters:
58+
//
59+
// - reader: The io.Reader to read the body from
60+
// - dest: The destination to decode the body into
61+
//
62+
// Returns:
63+
//
64+
// - error: The error if any
65+
func (d Decoder) DecodeReader(
66+
reader io.Reader,
67+
dest interface{},
68+
) error {
69+
return UnmarshalByReflection(
70+
reader,
71+
dest,
72+
&d.unmarshalOptions,
73+
)
74+
}

0 commit comments

Comments
 (0)