oauthex: use internal JSON library for decoding. (#866)
This commit is contained in:
parent
150bca797d
commit
05277f6e3c
3 changed files with 55 additions and 4 deletions
|
|
@ -8,12 +8,25 @@ package json
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"io"
|
||||||
|
|
||||||
"github.com/segmentio/encoding/json"
|
"github.com/segmentio/encoding/json"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Unmarshal(data []byte, v any) error {
|
type Decoder struct {
|
||||||
dec := json.NewDecoder(bytes.NewReader(data))
|
dec *json.Decoder
|
||||||
dec.DontMatchCaseInsensitiveStructFields()
|
}
|
||||||
return dec.Decode(v)
|
|
||||||
|
func NewDecoder(r io.Reader) *Decoder {
|
||||||
|
dec := json.NewDecoder(r)
|
||||||
|
dec.DontMatchCaseInsensitiveStructFields()
|
||||||
|
return &Decoder{dec: dec}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Decoder) Decode(v any) error {
|
||||||
|
return d.dec.Decode(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Unmarshal(data []byte, v any) error {
|
||||||
|
return NewDecoder(bytes.NewReader(data)).Decode(v)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
|
|
@ -65,6 +66,43 @@ func TestUnmarshalCaseSensitivity(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNewDecoderCaseSensitivity(t *testing.T) {
|
||||||
|
type Target struct {
|
||||||
|
Field string `json:"field"`
|
||||||
|
TaggedField string `json:"custom_tag"`
|
||||||
|
}
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
json string
|
||||||
|
want Target
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "exact match",
|
||||||
|
json: `{"field": "value", "custom_tag": "tagged"}`,
|
||||||
|
want: Target{Field: "value", TaggedField: "tagged"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "case mismatch",
|
||||||
|
json: `{"Field": "value", "Custom_tag": "tagged"}`,
|
||||||
|
want: Target{},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
var got Target
|
||||||
|
dec := NewDecoder(strings.NewReader(tt.json))
|
||||||
|
if err := dec.Decode(&got); err != nil {
|
||||||
|
t.Fatalf("Decode failed: %v", err)
|
||||||
|
}
|
||||||
|
if diff := cmp.Diff(tt.want, got); diff != "" {
|
||||||
|
t.Errorf("Decode mismatch (-want +got):\n%s", diff)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestUnmarshalNullCharacter(t *testing.T) {
|
func TestUnmarshalNullCharacter(t *testing.T) {
|
||||||
type Target struct {
|
type Target struct {
|
||||||
Field string `json:"field"`
|
Field string `json:"field"`
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,6 @@ package oauthex
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"mime"
|
"mime"
|
||||||
|
|
@ -18,6 +17,7 @@ import (
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/modelcontextprotocol/go-sdk/internal/json"
|
||||||
"github.com/modelcontextprotocol/go-sdk/internal/util"
|
"github.com/modelcontextprotocol/go-sdk/internal/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue