1
1
package fieldmask_utils_test
2
2
3
3
import (
4
+ "encoding/json"
4
5
"fmt"
5
6
"reflect"
6
7
"testing"
@@ -1990,7 +1991,7 @@ func TestStructToStruct_WithMultiTagComma(t *testing.T) {
1990
1991
}, dst )
1991
1992
}
1992
1993
1993
- func TestStructToMap_DiffentTypeWithSameDstKey (t * testing.T ) {
1994
+ func TestStructToMap_DifferentTypeWithSameDstKey (t * testing.T ) {
1994
1995
type BB struct {
1995
1996
Field int
1996
1997
}
@@ -2015,6 +2016,40 @@ func TestStructToMap_DiffentTypeWithSameDstKey(t *testing.T) {
2015
2016
require .Error (t , err )
2016
2017
}
2017
2018
2019
+ func TestStructToMap_EmptySrcSlice_JsonEncode (t * testing.T ) {
2020
+ type A struct {}
2021
+ type B struct {
2022
+ As []* A
2023
+ }
2024
+
2025
+ src := & B {[]* A {}}
2026
+ dst := make (map [string ]interface {})
2027
+
2028
+ mask := fieldmask_utils .MaskFromString ("As" )
2029
+ err := fieldmask_utils .StructToMap (mask , src , dst )
2030
+ require .NoError (t , err )
2031
+
2032
+ jsonStr , _ := json .Marshal (dst )
2033
+ assert .Equal (t , string (jsonStr ), "{\" As\" :[]}" )
2034
+ }
2035
+
2036
+ func TestStructToMap_NilSrcSlice_JsonEncode (t * testing.T ) {
2037
+ type A struct {}
2038
+ type B struct {
2039
+ As []* A
2040
+ }
2041
+
2042
+ src := & B {}
2043
+ dst := make (map [string ]interface {})
2044
+
2045
+ mask := fieldmask_utils .MaskFromString ("As" )
2046
+ err := fieldmask_utils .StructToMap (mask , src , dst )
2047
+ require .NoError (t , err )
2048
+
2049
+ jsonStr , _ := json .Marshal (dst )
2050
+ assert .Equal (t , string (jsonStr ), "{\" As\" :null}" )
2051
+ }
2052
+
2018
2053
func TestStructToStruct_CopySlice_WithDiffentAddr_WithDifferentFieldName (t * testing.T ) {
2019
2054
type A struct {
2020
2055
Field1 []int
0 commit comments