Skip to content

Commit

Permalink
Remove impossible error, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
islamaliev committed Nov 21, 2024
1 parent d9d5788 commit 8db7571
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 48 deletions.
46 changes: 14 additions & 32 deletions client/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func ParseJSONBytes(data []byte) (JSON, error) {
if err != nil {
return nil, err
}
return NewJSONFromFastJSON(v)
return NewJSONFromFastJSON(v), nil
}

// ParseJSONString parses the given JSON string into a JSON value.
Expand All @@ -267,7 +267,7 @@ func ParseJSONString(data string) (JSON, error) {
if err != nil {
return nil, err
}
return NewJSONFromFastJSON(v)
return NewJSONFromFastJSON(v), nil
}

// NewJSON creates a JSON value from a Go value.
Expand All @@ -287,7 +287,7 @@ func NewJSON(v any) (JSON, error) {
}
switch val := v.(type) {
case *fastjson.Value:
return NewJSONFromFastJSON(val)
return NewJSONFromFastJSON(val), nil
case string:
return newJSONString(val), nil
case map[string]any:
Expand Down Expand Up @@ -388,50 +388,32 @@ func newJSONStringArray(v []string) JSON {
}

// NewJSONFromFastJSON creates a JSON value from a fastjson.Value.
// Returns error if the fastjson.Value contains invalid JSON types.
func NewJSONFromFastJSON(v *fastjson.Value) (JSON, error) {
func NewJSONFromFastJSON(v *fastjson.Value) JSON {
switch v.Type() {
case fastjson.TypeObject:
obj := make(map[string]JSON)
var err error
v.GetObject().Visit(func(k []byte, v *fastjson.Value) {
if err != nil {
return
}
val, newErr := NewJSONFromFastJSON(v)
if newErr != nil {
err = newErr
return
}
obj[string(k)] = val
obj[string(k)] = NewJSONFromFastJSON(v)
})
if err != nil {
return nil, err
}
return newJSONObject(obj), nil
return newJSONObject(obj)
case fastjson.TypeArray:
arr := make([]JSON, 0)
for _, item := range v.GetArray() {
el, err := NewJSONFromFastJSON(item)
if err != nil {
return nil, err
}
arr = append(arr, el)
arr = append(arr, NewJSONFromFastJSON(item))
}
return newJSONArray(arr), nil
return newJSONArray(arr)
case fastjson.TypeNumber:
return newJSONNumber(v.GetFloat64()), nil
return newJSONNumber(v.GetFloat64())
case fastjson.TypeString:
return newJSONString(string(v.GetStringBytes())), nil
return newJSONString(string(v.GetStringBytes()))
case fastjson.TypeTrue:
return newJSONBool(true), nil
return newJSONBool(true)
case fastjson.TypeFalse:
return newJSONBool(false), nil
return newJSONBool(false)
case fastjson.TypeNull:
return newJSONNull(), nil
default:
return nil, NewErrInvalidJSONPayload(v)
return newJSONNull()
}
return nil
}

// NewJSONFromMap creates a JSON object from a map[string]any.
Expand Down
47 changes: 31 additions & 16 deletions client/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestParseJSONAndMarshal_WithValidInput_ShouldMarshal(t *testing.T) {
if err != nil {
return nil, err
}
return NewJSONFromFastJSON(v)
return NewJSONFromFastJSON(v), nil
},
},
{
Expand Down Expand Up @@ -70,6 +70,11 @@ func TestParseJSONAndMarshal_WithValidInput_ShouldMarshal(t *testing.T) {
actualStr := strings.ReplaceAll(buf.String(), "\n", "")
expectedStr := strings.ReplaceAll(data, " ", "")
require.Equal(t, actualStr, expectedStr, "Expected %s, got %s", expectedStr, actualStr)

rawJSON, err := jsonObj.MarshalJSON()
require.NoError(t, err, "jsonObj.MarshalJSON() failed with error %v", err)
actualStr = strings.ReplaceAll(string(rawJSON), "\n", "")
require.Equal(t, actualStr, expectedStr, "Expected %s, got %s", expectedStr, actualStr)
})
}
}
Expand All @@ -87,17 +92,6 @@ func TestNewJSONAndMarshal_WithInvalidInput_ShouldFail(t *testing.T) {
name: "FromString",
fromFunc: ParseJSONString,
},
{
name: "FromFastJSON",
fromFunc: func(data string) (JSON, error) {
var p fastjson.Parser
v, err := p.Parse(data)
if err != nil {
return nil, err
}
return NewJSONFromFastJSON(v)
},
},
{
name: "FromMap",
fromFunc: func(data string) (JSON, error) {
Expand Down Expand Up @@ -289,9 +283,10 @@ func TestJSONNull_Methods_ShouldWorkAsExpected(t *testing.T) {

func TestNewJSON(t *testing.T) {
tests := []struct {
name string
input any
expected JSON
name string
input any
expected JSON
expectError bool
}{
{
name: "Nil",
Expand Down Expand Up @@ -453,13 +448,33 @@ func TestNewJSON(t *testing.T) {
input: []float64{1.0, 2.25, 3.5},
expected: newJSONArray([]JSON{newJSONNumber(1.0), newJSONNumber(2.25), newJSONNumber(3.5)}),
},
{
name: "AnyArrayWithInvalidElement",
input: []any{"valid", make(chan int)}, // channels can't be converted to JSON
expectError: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := NewJSON(tt.input)
require.NoError(t, err)
if tt.expectError {
require.Error(t, err, "Expected error, but got nil")
return
}
require.NoError(t, err, "NewJSON failed with error %v", err)
require.Equal(t, result, tt.expected)
})
}
}

func TestNewJSONFromMap_WithInvalidValue_ShouldFail(t *testing.T) {
// Map with an invalid value (channel cannot be converted to JSON)
input := map[string]any{
"valid": "value",
"invalid": make(chan int),
}

_, err := NewJSONFromMap(input)
require.Error(t, err)
}

0 comments on commit 8db7571

Please sign in to comment.