-
-
Notifications
You must be signed in to change notification settings - Fork 814
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'stashapp:develop' into line-break-titles
- Loading branch information
Showing
97 changed files
with
3,378 additions
and
487 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"strings" | ||
) | ||
|
||
// JSONNumberToNumber converts a JSON number to either a float64 or int64. | ||
func jsonNumberToNumber(n json.Number) interface{} { | ||
if strings.Contains(string(n), ".") { | ||
f, _ := n.Float64() | ||
return f | ||
} | ||
ret, _ := n.Int64() | ||
return ret | ||
} | ||
|
||
// ConvertMapJSONNumbers converts all JSON numbers in a map to either float64 or int64. | ||
func convertMapJSONNumbers(m map[string]interface{}) (ret map[string]interface{}) { | ||
if m == nil { | ||
return nil | ||
} | ||
|
||
ret = make(map[string]interface{}) | ||
for k, v := range m { | ||
if n, ok := v.(json.Number); ok { | ||
ret[k] = jsonNumberToNumber(n) | ||
} else if mm, ok := v.(map[string]interface{}); ok { | ||
ret[k] = convertMapJSONNumbers(mm) | ||
} else { | ||
ret[k] = v | ||
} | ||
} | ||
|
||
return ret | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestConvertMapJSONNumbers(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input map[string]interface{} | ||
expected map[string]interface{} | ||
}{ | ||
{ | ||
name: "Convert JSON numbers to numbers", | ||
input: map[string]interface{}{ | ||
"int": json.Number("12"), | ||
"float": json.Number("12.34"), | ||
"string": "foo", | ||
}, | ||
expected: map[string]interface{}{ | ||
"int": int64(12), | ||
"float": 12.34, | ||
"string": "foo", | ||
}, | ||
}, | ||
{ | ||
name: "Convert JSON numbers to numbers in nested maps", | ||
input: map[string]interface{}{ | ||
"foo": map[string]interface{}{ | ||
"int": json.Number("56"), | ||
"float": json.Number("56.78"), | ||
"nested-string": "bar", | ||
}, | ||
"int": json.Number("12"), | ||
"float": json.Number("12.34"), | ||
"string": "foo", | ||
}, | ||
expected: map[string]interface{}{ | ||
"foo": map[string]interface{}{ | ||
"int": int64(56), | ||
"float": 56.78, | ||
"nested-string": "bar", | ||
}, | ||
"int": int64(12), | ||
"float": 12.34, | ||
"string": "foo", | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result := convertMapJSONNumbers(tt.input) | ||
assert.Equal(t, tt.expected, result) | ||
}) | ||
} | ||
} |
Oops, something went wrong.