| 1 | package schemascip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/ |
| 2 | |
| 3 | import ( |
| 4 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 5 | "reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/" |
| 6 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 7 | "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 8 | "timescip-go gomod github.com/golang/go/src go1.25.0 time/" |
| 9 | ) |
| 10 | |
| 11 | type E1scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E1# struct { |
| 12 | F01scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E1#F01. int `schema:"f01"` |
| 13 | F02scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E1#F02. int `schema:"-"` |
| 14 | F03scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E1#F03. string `schema:"f03"` |
| 15 | F04scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E1#F04. string `schema:"f04,omitempty"` |
| 16 | F05scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E1#F05. bool `schema:"f05"` |
| 17 | F06scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E1#F06. bool `schema:"f06"` |
| 18 | F07scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E1#F07. *string `schema:"f07"` |
| 19 | F08scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E1#F08. *int8 `schema:"f08"` |
| 20 | F09scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E1#F09. float64 `schema:"f09"` |
| 21 | F10scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E1#F10. func() `schema:"f10"` |
| 22 | F11scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E1#F11. innerscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/inner# |
| 23 | } |
| 24 | type innerscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/inner# struct { |
| 25 | F12scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/inner#F12. int |
| 26 | } |
| 27 | |
| 28 | type SimpleStructForBenchmarkEncodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/SimpleStructForBenchmarkEncode# struct { |
| 29 | Ascip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/SimpleStructForBenchmarkEncode#A. string `schema:"a"` |
| 30 | Bscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/SimpleStructForBenchmarkEncode#B. int `schema:"b"` |
| 31 | Cscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/SimpleStructForBenchmarkEncode#C. bool `schema:"c"` |
| 32 | Dscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/SimpleStructForBenchmarkEncode#D. float64 `schema:"d"` |
| 33 | Escip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/SimpleStructForBenchmarkEncode#E. struct { |
| 34 | Fscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/SimpleStructForBenchmarkEncode#$anon_3e287ec81a7fa4e4#F. float64 `schema:"f"` |
| 35 | } `schema:"e"` |
| 36 | } |
| 37 | |
| 38 | func TestFilledscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/TestFilled().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 39 | f07 := "seven" |
| 40 | var f08 int8 = 8 |
| 41 | s := &E1scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E1#{ |
| 42 | F01scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E1#F01.: 1, |
| 43 | F02scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E1#F02.: 2, |
| 44 | F03scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E1#F03.: "three", |
| 45 | F04scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E1#F04.: "four", |
| 46 | F05scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E1#F05.: true, |
| 47 | F06scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E1#F06.: false, |
| 48 | F07scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E1#F07.: &f07, |
| 49 | F08scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E1#F08.: &f08, |
| 50 | F09scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E1#F09.: 1.618, |
| 51 | F10scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E1#F10.: func() {}, |
| 52 | F11scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E1#F11.: innerscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/inner#{12}, |
| 53 | } |
| 54 | |
| 55 | vals := make(map[string][]string) |
| 56 | errs := NewEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewEncoder().().Encodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#Encode().(s, vals) |
| 57 | |
| 58 | valExistsscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valExists().(t, "f01", "1", vals) |
| 59 | valNotExistsscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valNotExists().(t, "f02", vals) |
| 60 | valExistsscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valExists().(t, "f03", "three", vals) |
| 61 | valExistsscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valExists().(t, "f05", "true", vals) |
| 62 | valExistsscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valExists().(t, "f06", "false", vals) |
| 63 | valExistsscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valExists().(t, "f07", "seven", vals) |
| 64 | valExistsscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valExists().(t, "f08", "8", vals) |
| 65 | valExistsscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valExists().(t, "f09", "1.618000", vals) |
| 66 | valExistsscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valExists().(t, "F12", "12", vals) |
| 67 | |
| 68 | emptyErr := MultiErrorscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/MultiError#{} |
| 69 | if errs.Error() == emptyErr.Errorscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/MultiError#Error().() { |
| 70 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected error got %v", errs) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | type Aascip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Aa# int |
| 75 | |
| 76 | type E3scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E3# struct { |
| 77 | F01scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E3#F01. bool `schema:"f01"` |
| 78 | F02scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E3#F02. float32 `schema:"f02"` |
| 79 | F03scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E3#F03. float64 `schema:"f03"` |
| 80 | F04scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E3#F04. int `schema:"f04"` |
| 81 | F05scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E3#F05. int8 `schema:"f05"` |
| 82 | F06scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E3#F06. int16 `schema:"f06"` |
| 83 | F07scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E3#F07. int32 `schema:"f07"` |
| 84 | F08scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E3#F08. int64 `schema:"f08"` |
| 85 | F09scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E3#F09. string `schema:"f09"` |
| 86 | F10scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E3#F10. uint `schema:"f10"` |
| 87 | F11scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E3#F11. uint8 `schema:"f11"` |
| 88 | F12scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E3#F12. uint16 `schema:"f12"` |
| 89 | F13scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E3#F13. uint32 `schema:"f13"` |
| 90 | F14scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E3#F14. uint64 `schema:"f14"` |
| 91 | F15scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E3#F15. Aascip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Aa# `schema:"f15"` |
| 92 | } |
| 93 | |
| 94 | // Test compatibility with default decoder types. |
| 95 | func TestCompatscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/TestCompat().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 96 | src := &E3scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E3#{ |
| 97 | F01scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E3#F01.: true, |
| 98 | F02scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E3#F02.: 4.2, |
| 99 | F03scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E3#F03.: 4.3, |
| 100 | F04scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E3#F04.: -42, |
| 101 | F05scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E3#F05.: -43, |
| 102 | F06scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E3#F06.: -44, |
| 103 | F07scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E3#F07.: -45, |
| 104 | F08scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E3#F08.: -46, |
| 105 | F09scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E3#F09.: "foo", |
| 106 | F10scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E3#F10.: 42, |
| 107 | F11scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E3#F11.: 43, |
| 108 | F12scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E3#F12.: 44, |
| 109 | F13scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E3#F13.: 45, |
| 110 | F14scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E3#F14.: 46, |
| 111 | F15scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E3#F15.: 1, |
| 112 | } |
| 113 | dst := &E3scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E3#{} |
| 114 | |
| 115 | vals := make(map[string][]string) |
| 116 | encoder := NewEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewEncoder().() |
| 117 | decoder := NewDecoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewDecoder().() |
| 118 | |
| 119 | encoder.RegisterEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#RegisterEncoder().(src.F15scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E3#F15., func(reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Valuescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#) string { return "1" }) |
| 120 | decoder.RegisterConverterscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Decoder#RegisterConverter().(src.F15scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E3#F15., func(string) reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Valuescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value# { return reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.25.0 reflect/ValueOf().(1) }) |
| 121 | |
| 122 | err := encoder.Encodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#Encode().(src, vals) |
| 123 | if err != nil { |
| 124 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Encoder has non-nil error: %v", err) |
| 125 | } |
| 126 | err = decoder.Decodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Decoder#Decode().(dst, vals) |
| 127 | if err != nil { |
| 128 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Decoder has non-nil error: %v", err) |
| 129 | } |
| 130 | |
| 131 | if *src != *dst { |
| 132 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Decoder-Encoder compatibility: expected %v, got %v\n", src, dst) |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | func TestEmptyscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/TestEmpty().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 137 | s := &E1scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E1#{ |
| 138 | F01scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E1#F01.: 1, |
| 139 | F02scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E1#F02.: 2, |
| 140 | F03scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E1#F03.: "three", |
| 141 | } |
| 142 | |
| 143 | estr := "schema: encoder not found for <nil>" |
| 144 | vals := make(map[string][]string) |
| 145 | err := NewEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewEncoder().().Encodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#Encode().(s, vals) |
| 146 | if err.Error() != estr { |
| 147 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected: %s, got %v", estr, err) |
| 148 | } |
| 149 | |
| 150 | valExistsscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valExists().(t, "f03", "three", vals) |
| 151 | valNotExistsscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valNotExists().(t, "f04", vals) |
| 152 | } |
| 153 | |
| 154 | func TestStructscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/TestStruct().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 155 | estr := "schema: interface must be a struct" |
| 156 | vals := make(map[string][]string) |
| 157 | err := NewEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewEncoder().().Encodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#Encode().("hello world", vals) |
| 158 | |
| 159 | if err.Error() != estr { |
| 160 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected: %s, got %v", estr, err) |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | func TestSlicesscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/TestSlices().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 165 | type oneAsWord int |
| 166 | ones := []oneAsWord{1, 2} |
| 167 | s1 := &struct { |
| 168 | ones []oneAsWord `schema:"ones"` |
| 169 | ints []int `schema:"ints"` |
| 170 | nonempty []int `schema:"nonempty"` |
| 171 | empty []int `schema:"empty,omitempty"` |
| 172 | }{ones, []int{1, 1}, []int{}, []int{}} |
| 173 | vals := make(map[string][]string) |
| 174 | |
| 175 | encoder := NewEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewEncoder().() |
| 176 | encoder.RegisterEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#RegisterEncoder().(ones[0], func(v reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Valuescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#) string { return "one" }) |
| 177 | err := encoder.Encodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#Encode().(s1, vals) |
| 178 | if err != nil { |
| 179 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Encoder has non-nil error: %v", err) |
| 180 | } |
| 181 | |
| 182 | valsExistscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valsExist().(t, "ones", []string{"one", "one"}, vals) |
| 183 | valsExistscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valsExist().(t, "ints", []string{"1", "1"}, vals) |
| 184 | valsExistscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valsExist().(t, "nonempty", []string{}, vals) |
| 185 | valNotExistsscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valNotExists().(t, "empty", vals) |
| 186 | } |
| 187 | |
| 188 | func TestCompatSlicesscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/TestCompatSlices().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 189 | type oneAsWord int |
| 190 | type s1 struct { |
| 191 | Ones []oneAsWord `schema:"ones"` |
| 192 | Ints []int `schema:"ints"` |
| 193 | } |
| 194 | ones := []oneAsWord{1, 1} |
| 195 | src := &s1{ones, []int{1, 1}} |
| 196 | vals := make(map[string][]string) |
| 197 | dst := &s1{} |
| 198 | |
| 199 | encoder := NewEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewEncoder().() |
| 200 | encoder.RegisterEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#RegisterEncoder().(ones[0], func(v reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Valuescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#) string { return "one" }) |
| 201 | |
| 202 | decoder := NewDecoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewDecoder().() |
| 203 | decoder.RegisterConverterscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Decoder#RegisterConverter().(ones[0], func(s string) reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Valuescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value# { |
| 204 | if s == "one" { |
| 205 | return reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.25.0 reflect/ValueOf().(1) |
| 206 | } |
| 207 | return reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.25.0 reflect/ValueOf().(2) |
| 208 | }) |
| 209 | |
| 210 | err := encoder.Encodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#Encode().(src, vals) |
| 211 | if err != nil { |
| 212 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Encoder has non-nil error: %v", err) |
| 213 | } |
| 214 | err = decoder.Decodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Decoder#Decode().(dst, vals) |
| 215 | if err != nil { |
| 216 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Dncoder has non-nil error: %v", err) |
| 217 | } |
| 218 | |
| 219 | if len(src.Ints) != len(dst.Ints) || len(src.Ones) != len(dst.Ones) { |
| 220 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Expected %v, got %v", src, dst) |
| 221 | } |
| 222 | |
| 223 | for i, v := range src.Ones { |
| 224 | if dst.Ones[i] != v { |
| 225 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Expected %v, got %v", v, dst.Ones[i]) |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | for i, v := range src.Ints { |
| 230 | if dst.Ints[i] != v { |
| 231 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Expected %v, got %v", v, dst.Ints[i]) |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | func TestRegisterEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/TestRegisterEncoder().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 237 | type oneAsWord int |
| 238 | type twoAsWord int |
| 239 | type oneSliceAsWord []int |
| 240 | |
| 241 | s1 := &struct { |
| 242 | oneAsWord |
| 243 | twoAsWord |
| 244 | oneSliceAsWord |
| 245 | }{1, 2, []int{1, 1}} |
| 246 | v1 := make(map[string][]string) |
| 247 | |
| 248 | encoder := NewEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewEncoder().() |
| 249 | encoder.RegisterEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#RegisterEncoder().(s1.oneAsWord, func(v reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Valuescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#) string { return "one" }) |
| 250 | encoder.RegisterEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#RegisterEncoder().(s1.twoAsWord, func(v reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Valuescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#) string { return "two" }) |
| 251 | encoder.RegisterEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#RegisterEncoder().(s1.oneSliceAsWord, func(v reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Valuescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#) string { return "one" }) |
| 252 | |
| 253 | err := encoder.Encodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#Encode().(s1, v1) |
| 254 | if err != nil { |
| 255 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Encoder has non-nil error: %v", err) |
| 256 | } |
| 257 | |
| 258 | valExistsscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valExists().(t, "oneAsWord", "one", v1) |
| 259 | valExistsscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valExists().(t, "twoAsWord", "two", v1) |
| 260 | valExistsscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valExists().(t, "oneSliceAsWord", "one", v1) |
| 261 | } |
| 262 | |
| 263 | func TestEncoderOrderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/TestEncoderOrder().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 264 | type builtinEncoderSimple int |
| 265 | type builtinEncoderSimpleOverridden int |
| 266 | type builtinEncoderSlice []int |
| 267 | type builtinEncoderSliceOverridden []int |
| 268 | type builtinEncoderStruct struct{ nr int } |
| 269 | type builtinEncoderStructOverridden struct{ nr int } |
| 270 | |
| 271 | s1 := &struct { |
| 272 | builtinEncoderSimple `schema:"simple"` |
| 273 | builtinEncoderSimpleOverridden `schema:"simple_overridden"` |
| 274 | builtinEncoderSlice `schema:"slice"` |
| 275 | builtinEncoderSliceOverridden `schema:"slice_overridden"` |
| 276 | builtinEncoderStruct `schema:"struct"` |
| 277 | builtinEncoderStructOverridden `schema:"struct_overridden"` |
| 278 | }{ |
| 279 | 1, |
| 280 | 1, |
| 281 | []int{2}, |
| 282 | []int{2}, |
| 283 | builtinEncoderStruct{3}, |
| 284 | builtinEncoderStructOverridden{3}, |
| 285 | } |
| 286 | v1 := make(map[string][]string) |
| 287 | |
| 288 | encoder := NewEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewEncoder().() |
| 289 | encoder.RegisterEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#RegisterEncoder().(s1.builtinEncoderSimpleOverridden, func(v reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Valuescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#) string { return "one" }) |
| 290 | encoder.RegisterEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#RegisterEncoder().(s1.builtinEncoderSliceOverridden, func(v reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Valuescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#) string { return "two" }) |
| 291 | encoder.RegisterEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#RegisterEncoder().(s1.builtinEncoderStructOverridden, func(v reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Valuescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#) string { return "three" }) |
| 292 | |
| 293 | err := encoder.Encodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#Encode().(s1, v1) |
| 294 | if err != nil { |
| 295 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Encoder has non-nil error: %v", err) |
| 296 | } |
| 297 | |
| 298 | valExistsscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valExists().(t, "simple", "1", v1) |
| 299 | valExistsscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valExists().(t, "simple_overridden", "one", v1) |
| 300 | valExistsscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valExists().(t, "slice", "2", v1) |
| 301 | valExistsscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valExists().(t, "slice_overridden", "two", v1) |
| 302 | valExistsscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valExists().(t, "nr", "3", v1) |
| 303 | valExistsscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valExists().(t, "struct_overridden", "three", v1) |
| 304 | } |
| 305 | |
| 306 | func valExistsscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valExists().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#, key string, expect string, result map[string][]string) { |
| 307 | valsExistscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valsExist().(t, key, []string{expect}, result) |
| 308 | } |
| 309 | |
| 310 | func valsExistscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valsExist().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#, key string, expect []string, result map[string][]string) { |
| 311 | vals, ok := result[key] |
| 312 | if !ok { |
| 313 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Key not found. Expected: %s", key) |
| 314 | } |
| 315 | |
| 316 | if len(expect) != len(vals) { |
| 317 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Expected: %v, got: %v", expect, vals) |
| 318 | } |
| 319 | |
| 320 | for i, v := range expect { |
| 321 | if vals[i] != v { |
| 322 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Unexpected value. Expected: %v, got %v", v, vals[i]) |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | func valNotExistsscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valNotExists().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#, key string, result map[string][]string) { |
| 328 | if val, ok := result[key]; ok { |
| 329 | t.Errorscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Error().("Key not omitted. Expected: empty; got: " + val[0] + ".") |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | func valsLengthscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valsLength().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#, expectedLength int, result map[string][]string) { |
| 334 | length := len(result) |
| 335 | if length != expectedLength { |
| 336 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected length of %v, but got %v", expectedLength, length) |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | func noErrorscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/noError().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#, err error) { |
| 341 | if err != nil { |
| 342 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Unexpected error. Got %v", err) |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | type E4scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E4# struct { |
| 347 | IDscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E4#ID. string `json:"id"` |
| 348 | } |
| 349 | |
| 350 | func TestEncoderSetAliasTagscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/TestEncoderSetAliasTag().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 351 | data := map[string][]string{} |
| 352 | |
| 353 | s := E4scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E4#{ |
| 354 | IDscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E4#ID.: "foo", |
| 355 | } |
| 356 | encoder := NewEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewEncoder().() |
| 357 | encoder.SetAliasTagscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#SetAliasTag().("json") |
| 358 | err := encoder.Encodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#Encode().(&s, data) |
| 359 | if err != nil { |
| 360 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Failed to encode: %v", err) |
| 361 | } |
| 362 | valExistsscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valExists().(t, "id", "foo", data) |
| 363 | } |
| 364 | |
| 365 | type E5scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E5# struct { |
| 366 | F01scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E5#F01. int `schema:"f01,omitempty"` |
| 367 | F02scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E5#F02. string `schema:"f02,omitempty"` |
| 368 | F03scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E5#F03. *string `schema:"f03,omitempty"` |
| 369 | F04scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E5#F04. *int8 `schema:"f04,omitempty"` |
| 370 | F05scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E5#F05. float64 `schema:"f05,omitempty"` |
| 371 | F06scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E5#F06. E5F06scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E5F06# `schema:"f06,omitempty"` |
| 372 | F07scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E5#F07. E5F06scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E5F06# `schema:"f07,omitempty"` |
| 373 | F08scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E5#F08. []string `schema:"f08,omitempty"` |
| 374 | F09scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E5#F09. []string `schema:"f09,omitempty"` |
| 375 | } |
| 376 | |
| 377 | type E5F06scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E5F06# struct { |
| 378 | F0601scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E5F06#F0601. string `schema:"f0601,omitempty"` |
| 379 | } |
| 380 | |
| 381 | func TestEncoderWithOmitemptyscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/TestEncoderWithOmitempty().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 382 | vals := map[string][]string{} |
| 383 | |
| 384 | s := E5scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E5#{ |
| 385 | F02scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E5#F02.: "test", |
| 386 | F07scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E5#F07.: E5F06scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E5F06#{ |
| 387 | F0601scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E5F06#F0601.: "test", |
| 388 | }, |
| 389 | F09scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E5#F09.: []string{"test"}, |
| 390 | } |
| 391 | |
| 392 | encoder := NewEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewEncoder().() |
| 393 | err := encoder.Encodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#Encode().(&s, vals) |
| 394 | if err != nil { |
| 395 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Failed to encode: %v", err) |
| 396 | } |
| 397 | |
| 398 | valNotExistsscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valNotExists().(t, "f01", vals) |
| 399 | valExistsscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valExists().(t, "f02", "test", vals) |
| 400 | valNotExistsscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valNotExists().(t, "f03", vals) |
| 401 | valNotExistsscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valNotExists().(t, "f04", vals) |
| 402 | valNotExistsscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valNotExists().(t, "f05", vals) |
| 403 | valNotExistsscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valNotExists().(t, "f06", vals) |
| 404 | valExistsscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valExists().(t, "f0601", "test", vals) |
| 405 | valNotExistsscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valNotExists().(t, "f08", vals) |
| 406 | valsExistscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valsExist().(t, "f09", []string{"test"}, vals) |
| 407 | } |
| 408 | |
| 409 | type E6scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E6# struct { |
| 410 | F01scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E6#F01. *innerscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/inner# |
| 411 | F02scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E6#F02. *innerscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/inner# |
| 412 | F03scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E6#F03. *innerscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/inner# `schema:",omitempty"` |
| 413 | } |
| 414 | |
| 415 | func TestStructPointerscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/TestStructPointer().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 416 | vals := map[string][]string{} |
| 417 | s := E6scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E6#{ |
| 418 | F01scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/E6#F01.: &innerscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/inner#{2}, |
| 419 | } |
| 420 | |
| 421 | encoder := NewEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewEncoder().() |
| 422 | err := encoder.Encodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#Encode().(&s, vals) |
| 423 | if err != nil { |
| 424 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Failed to encode: %v", err) |
| 425 | } |
| 426 | valExistsscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valExists().(t, "F12", "2", vals) |
| 427 | valExistsscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valExists().(t, "F02", "null", vals) |
| 428 | valNotExistsscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valNotExists().(t, "F03", vals) |
| 429 | } |
| 430 | |
| 431 | func TestRegisterEncoderCustomArrayTypescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/TestRegisterEncoderCustomArrayType().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 432 | type CustomInt []int |
| 433 | type S1 struct { |
| 434 | SomeInts CustomInt `schema:",omitempty"` |
| 435 | } |
| 436 | |
| 437 | ss := []S1{ |
| 438 | {}, |
| 439 | {CustomInt{}}, |
| 440 | {CustomInt{1, 2, 3}}, |
| 441 | } |
| 442 | |
| 443 | for s := range ss { |
| 444 | vals := map[string][]string{} |
| 445 | |
| 446 | encoder := NewEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewEncoder().() |
| 447 | encoder.RegisterEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#RegisterEncoder().(CustomInt{}, func(value reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Valuescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#) string { |
| 448 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprint().(value.Interfacescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Interface().()) |
| 449 | }) |
| 450 | |
| 451 | err := encoder.Encodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#Encode().(ss[s], vals) |
| 452 | if err != nil { |
| 453 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Failed to encode: %v", err) |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | func TestRegisterEncoderStructIsZeroscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/TestRegisterEncoderStructIsZero().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 459 | type S1 struct { |
| 460 | SomeTime1 timescip-go gomod github.com/golang/go/src go1.25.0 time/.Timescip-go gomod github.com/golang/go/src go1.25.0 time/Time# `schema:"tim1,omitempty"` |
| 461 | SomeTime2 timescip-go gomod github.com/golang/go/src go1.25.0 time/.Timescip-go gomod github.com/golang/go/src go1.25.0 time/Time# `schema:"tim2,omitempty"` |
| 462 | } |
| 463 | |
| 464 | ss := []*S1{ |
| 465 | { |
| 466 | SomeTime1: timescip-go gomod github.com/golang/go/src go1.25.0 time/.Datescip-go gomod github.com/golang/go/src go1.25.0 time/Date().(2020, 8, 4, 13, 30, 1, 0, timescip-go gomod github.com/golang/go/src go1.25.0 time/.UTCscip-go gomod github.com/golang/go/src go1.25.0 time/UTC.), |
| 467 | }, |
| 468 | } |
| 469 | |
| 470 | for s := range ss { |
| 471 | vals := map[string][]string{} |
| 472 | |
| 473 | encoder := NewEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewEncoder().() |
| 474 | encoder.RegisterEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#RegisterEncoder().(timescip-go gomod github.com/golang/go/src go1.25.0 time/.Timescip-go gomod github.com/golang/go/src go1.25.0 time/Time#{}, func(value reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Valuescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#) string { |
| 475 | tv, _ := reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.TypeAssertscip-go gomod github.com/golang/go/src go1.25.0 reflect/TypeAssert().[timescip-go gomod github.com/golang/go/src go1.25.0 time/.Timescip-go gomod github.com/golang/go/src go1.25.0 time/Time#](value) |
| 476 | return tv.Formatscip-go gomod github.com/golang/go/src go1.25.0 time/Time#Format().(timescip-go gomod github.com/golang/go/src go1.25.0 time/.RFC3339Nanoscip-go gomod github.com/golang/go/src go1.25.0 time/RFC3339Nano.) |
| 477 | }) |
| 478 | |
| 479 | err := encoder.Encodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#Encode().(ss[s], vals) |
| 480 | if err != nil { |
| 481 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Encoder has non-nil error: %v", err) |
| 482 | } |
| 483 | |
| 484 | ta, ok := vals["tim1"] |
| 485 | if !ok { |
| 486 | t.Errorscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Error().("expected tim1 to be present") |
| 487 | } |
| 488 | |
| 489 | if len(ta) != 1 { |
| 490 | t.Errorscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Error().("expected tim1 to be present") |
| 491 | } |
| 492 | |
| 493 | if ta[0] != "2020-08-04T13:30:01Z" { |
| 494 | t.Errorscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Error().("expected correct tim1 time") |
| 495 | } |
| 496 | |
| 497 | _, ok = vals["tim2"] |
| 498 | if ok { |
| 499 | t.Errorscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Error().("expected tim1 not to be present") |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | func TestRegisterEncoderWithPtrTypescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/TestRegisterEncoderWithPtrType().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 505 | type CustomTime struct { |
| 506 | time timescip-go gomod github.com/golang/go/src go1.25.0 time/.Timescip-go gomod github.com/golang/go/src go1.25.0 time/Time# |
| 507 | } |
| 508 | |
| 509 | type S1 struct { |
| 510 | DateStart *CustomTime |
| 511 | DateEnd *CustomTime |
| 512 | Empty *CustomTime `schema:"empty,omitempty"` |
| 513 | } |
| 514 | |
| 515 | ss := S1{ |
| 516 | DateStart: &CustomTime{time: timescip-go gomod github.com/golang/go/src go1.25.0 time/.Nowscip-go gomod github.com/golang/go/src go1.25.0 time/Now().()}, |
| 517 | DateEnd: nil, |
| 518 | } |
| 519 | |
| 520 | encoder := NewEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewEncoder().() |
| 521 | encoder.RegisterEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#RegisterEncoder().(&CustomTime{}, func(value reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Valuescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#) string { |
| 522 | if value.IsNilscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#IsNil().() { |
| 523 | return "" |
| 524 | } |
| 525 | |
| 526 | custom, _ := reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.TypeAssertscip-go gomod github.com/golang/go/src go1.25.0 reflect/TypeAssert().[*CustomTime](value) |
| 527 | return custom.time.Stringscip-go gomod github.com/golang/go/src go1.25.0 time/Time#String().() |
| 528 | }) |
| 529 | |
| 530 | vals := map[string][]string{} |
| 531 | err := encoder.Encodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#Encode().(ss, vals) |
| 532 | |
| 533 | noErrorscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/noError().(t, err) |
| 534 | valsLengthscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valsLength().(t, 2, vals) |
| 535 | valExistsscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valExists().(t, "DateStart", ss.DateStart.time.Stringscip-go gomod github.com/golang/go/src go1.25.0 time/Time#String().(), vals) |
| 536 | valExistsscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/valExists().(t, "DateEnd", "", vals) |
| 537 | } |
| 538 | |
| 539 | func TestTimeDurationEncodingscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/TestTimeDurationEncoding().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 540 | type DurationStruct struct { |
| 541 | Timeout timescip-go gomod github.com/golang/go/src go1.25.0 time/.Durationscip-go gomod github.com/golang/go/src go1.25.0 time/Duration# `schema:"timeout"` |
| 542 | } |
| 543 | |
| 544 | vals := map[string][]string{} |
| 545 | testData := DurationStruct{ |
| 546 | Timeout: 3 * timescip-go gomod github.com/golang/go/src go1.25.0 time/.Minutescip-go gomod github.com/golang/go/src go1.25.0 time/Minute., |
| 547 | } |
| 548 | |
| 549 | enc := NewEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewEncoder().() |
| 550 | enc.RegisterEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#RegisterEncoder().(timescip-go gomod github.com/golang/go/src go1.25.0 time/.Durationscip-go gomod github.com/golang/go/src go1.25.0 time/Duration#(0), func(v reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Valuescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#) string { |
| 551 | d, _ := reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.TypeAssertscip-go gomod github.com/golang/go/src go1.25.0 reflect/TypeAssert().[timescip-go gomod github.com/golang/go/src go1.25.0 time/.Durationscip-go gomod github.com/golang/go/src go1.25.0 time/Duration#](v) |
| 552 | return d.Stringscip-go gomod github.com/golang/go/src go1.25.0 time/Duration#String().() // "3m0s" |
| 553 | }) |
| 554 | |
| 555 | err := enc.Encodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#Encode().(&testData, vals) |
| 556 | if err != nil { |
| 557 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Failed to encode time.Duration: %v", err) |
| 558 | } |
| 559 | |
| 560 | got, ok := vals["timeout"] |
| 561 | if !ok || len(got) < 1 { |
| 562 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Encoded map missing key 'timeout'") |
| 563 | } |
| 564 | if got[0] != (3 * timescip-go gomod github.com/golang/go/src go1.25.0 time/.Minutescip-go gomod github.com/golang/go/src go1.25.0 time/Minute.).Stringscip-go gomod github.com/golang/go/src go1.25.0 time/Duration#String().() { |
| 565 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected %q, got %q", (3 * timescip-go gomod github.com/golang/go/src go1.25.0 time/.Minutescip-go gomod github.com/golang/go/src go1.25.0 time/Minute.).Stringscip-go gomod github.com/golang/go/src go1.25.0 time/Duration#String().(), got[0]) |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | // Test for omitempty with zero time.Duration. |
| 570 | func TestTimeDurationOmitEmptyscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/TestTimeDurationOmitEmpty().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 571 | type DurationStruct struct { |
| 572 | Timeout timescip-go gomod github.com/golang/go/src go1.25.0 time/.Durationscip-go gomod github.com/golang/go/src go1.25.0 time/Duration# `schema:"timeout,omitempty"` |
| 573 | } |
| 574 | |
| 575 | vals := map[string][]string{} |
| 576 | testData := DurationStruct{ |
| 577 | Timeout: 0, |
| 578 | } |
| 579 | |
| 580 | enc := NewEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewEncoder().() |
| 581 | enc.RegisterEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#RegisterEncoder().(timescip-go gomod github.com/golang/go/src go1.25.0 time/.Durationscip-go gomod github.com/golang/go/src go1.25.0 time/Duration#(0), func(v reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Valuescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#) string { |
| 582 | d, _ := reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.TypeAssertscip-go gomod github.com/golang/go/src go1.25.0 reflect/TypeAssert().[timescip-go gomod github.com/golang/go/src go1.25.0 time/.Durationscip-go gomod github.com/golang/go/src go1.25.0 time/Duration#](v) |
| 583 | return d.Stringscip-go gomod github.com/golang/go/src go1.25.0 time/Duration#String().() |
| 584 | }) |
| 585 | |
| 586 | err := enc.Encodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#Encode().(&testData, vals) |
| 587 | if err != nil { |
| 588 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Failed to encode time.Duration: %v", err) |
| 589 | } |
| 590 | // Should be omitted since 0 for time.Duration is "zero" and tagged as omitempty |
| 591 | if _, found := vals["timeout"]; found { |
| 592 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected 'timeout' to be omitted, but it was present: %v", vals["timeout"]) |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | func TestEncoderZeroAndNonZeroFieldsscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/TestEncoderZeroAndNonZeroFields().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 597 | type ZeroTestStruct struct { |
| 598 | A string `schema:"a,omitempty"` |
| 599 | B int `schema:"b,omitempty"` |
| 600 | C float64 `schema:"c,omitempty"` |
| 601 | D bool `schema:"d,omitempty"` |
| 602 | E *int `schema:"e,omitempty"` |
| 603 | F *string `schema:"f,omitempty"` |
| 604 | G string `schema:"g"` // no omitempty |
| 605 | } |
| 606 | |
| 607 | vals := map[string][]string{} |
| 608 | intVal := 42 |
| 609 | strVal := "Hello" |
| 610 | s := ZeroTestStruct{ |
| 611 | A: "", |
| 612 | B: 0, |
| 613 | C: 0.0, |
| 614 | D: false, |
| 615 | E: &intVal, |
| 616 | F: &strVal, |
| 617 | G: "MustEncode", |
| 618 | } |
| 619 | |
| 620 | enc := NewEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewEncoder().() |
| 621 | err := enc.Encodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#Encode().(&s, vals) |
| 622 | if err != nil { |
| 623 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Encoding error: %v", err) |
| 624 | } |
| 625 | |
| 626 | // Fields A, B, C, D are zero and should be omitted |
| 627 | if _, found := vals["a"]; found { |
| 628 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected 'a' to be omitted for zero string") |
| 629 | } |
| 630 | if _, found := vals["b"]; found { |
| 631 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected 'b' to be omitted for zero int") |
| 632 | } |
| 633 | if _, found := vals["c"]; found { |
| 634 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected 'c' to be omitted for zero float") |
| 635 | } |
| 636 | if _, found := vals["d"]; found { |
| 637 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected 'd' to be omitted for false bool") |
| 638 | } |
| 639 | |
| 640 | // E is a pointer to an int, so it should appear |
| 641 | gotE, found := vals["e"] |
| 642 | if !found { |
| 643 | t.Errorscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Error().("Expected 'e' to be present") |
| 644 | } else if len(gotE) != 1 || gotE[0] != "42" { |
| 645 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected '42', got %v", gotE) |
| 646 | } |
| 647 | |
| 648 | // F is a pointer to string, so it should appear |
| 649 | gotF, found := vals["f"] |
| 650 | if !found { |
| 651 | t.Errorscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Error().("Expected 'f' to be present") |
| 652 | } else if len(gotF) != 1 || gotF[0] != "Hello" { |
| 653 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected 'Hello', got %v", gotF) |
| 654 | } |
| 655 | |
| 656 | // G has no omitempty tag and must be encoded |
| 657 | gotG, found := vals["g"] |
| 658 | if !found { |
| 659 | t.Errorscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Error().("Expected 'g' to be present") |
| 660 | } else if len(gotG) != 1 || gotG[0] != "MustEncode" { |
| 661 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected 'MustEncode', got %v", gotG) |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | func BenchmarkSimpleStructEncodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/BenchmarkSimpleStructEncode().(b *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Bscip-go gomod github.com/golang/go/src go1.25.0 testing/B#) { |
| 666 | s := SimpleStructForBenchmarkEncodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/SimpleStructForBenchmarkEncode#{ |
| 667 | Ascip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/SimpleStructForBenchmarkEncode#A.: "abc", |
| 668 | Bscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/SimpleStructForBenchmarkEncode#B.: 123, |
| 669 | Cscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/SimpleStructForBenchmarkEncode#C.: true, |
| 670 | Dscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/SimpleStructForBenchmarkEncode#D.: 3.14, |
| 671 | Escip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/SimpleStructForBenchmarkEncode#E.: struct { |
| 672 | F float64 `schema:"f"` |
| 673 | }{F: 6.28}, |
| 674 | } |
| 675 | enc := NewEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewEncoder().() |
| 676 | |
| 677 | vals := map[string][]string{} |
| 678 | for b.Loopscip-go gomod github.com/golang/go/src go1.25.0 testing/B#Loop().() { |
| 679 | _ = enc.Encodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#Encode().(&s, vals) |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | func BenchmarkSimpleStructEncodeParallelscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/BenchmarkSimpleStructEncodeParallel().(b *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Bscip-go gomod github.com/golang/go/src go1.25.0 testing/B#) { |
| 684 | s := SimpleStructForBenchmarkEncodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/SimpleStructForBenchmarkEncode#{ |
| 685 | Ascip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/SimpleStructForBenchmarkEncode#A.: "abc", |
| 686 | Bscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/SimpleStructForBenchmarkEncode#B.: 123, |
| 687 | Cscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/SimpleStructForBenchmarkEncode#C.: true, |
| 688 | Dscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/SimpleStructForBenchmarkEncode#D.: 3.14, |
| 689 | Escip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/SimpleStructForBenchmarkEncode#E.: struct { |
| 690 | F float64 `schema:"f"` |
| 691 | }{F: 6.28}, |
| 692 | } |
| 693 | enc := NewEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewEncoder().() |
| 694 | |
| 695 | b.ResetTimerscip-go gomod github.com/golang/go/src go1.25.0 testing/B#ResetTimer().() |
| 696 | b.RunParallelscip-go gomod github.com/golang/go/src go1.25.0 testing/B#RunParallel().(func(pb *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.PBscip-go gomod github.com/golang/go/src go1.25.0 testing/PB#) { |
| 697 | vals := map[string][]string{} |
| 698 | for pb.Nextscip-go gomod github.com/golang/go/src go1.25.0 testing/PB#Next().() { |
| 699 | _ = enc.Encodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#Encode().(&s, vals) |
| 700 | } |
| 701 | }) |
| 702 | } |
| 703 | |
| 704 | type LargeStructForBenchmarkEncodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/LargeStructForBenchmarkEncode# struct { |
| 705 | F1scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/LargeStructForBenchmarkEncode#F1. string `schema:"f1"` |
| 706 | F2scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/LargeStructForBenchmarkEncode#F2. string `schema:"f2"` |
| 707 | F3scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/LargeStructForBenchmarkEncode#F3. int `schema:"f3"` |
| 708 | F4scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/LargeStructForBenchmarkEncode#F4. int `schema:"f4"` |
| 709 | F5scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/LargeStructForBenchmarkEncode#F5. []string `schema:"f5"` |
| 710 | F6scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/LargeStructForBenchmarkEncode#F6. []int `schema:"f6"` |
| 711 | F7scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/LargeStructForBenchmarkEncode#F7. float64 `schema:"f7"` |
| 712 | F8scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/LargeStructForBenchmarkEncode#F8. bool `schema:"f8"` |
| 713 | F9scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/LargeStructForBenchmarkEncode#F9. struct { |
| 714 | N1scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/LargeStructForBenchmarkEncode#$anon_c2b25aff0a2ad188#N1. timescip-go gomod github.com/golang/go/src go1.25.0 time/.Timescip-go gomod github.com/golang/go/src go1.25.0 time/Time# `schema:"n1"` |
| 715 | N2scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/LargeStructForBenchmarkEncode#$anon_c2b25aff0a2ad188#N2. string `schema:"n2"` |
| 716 | } `schema:"f9"` |
| 717 | } |
| 718 | |
| 719 | func BenchmarkLargeStructEncodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/BenchmarkLargeStructEncode().(b *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Bscip-go gomod github.com/golang/go/src go1.25.0 testing/B#) { |
| 720 | s := LargeStructForBenchmarkEncodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/LargeStructForBenchmarkEncode#{ |
| 721 | F1scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/LargeStructForBenchmarkEncode#F1.: "Lorem", F2scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/LargeStructForBenchmarkEncode#F2.: "Ipsum", F3scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/LargeStructForBenchmarkEncode#F3.: 123, F4scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/LargeStructForBenchmarkEncode#F4.: 456, |
| 722 | F5scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/LargeStructForBenchmarkEncode#F5.: []string{"A", "B", "C", "D"}, |
| 723 | F6scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/LargeStructForBenchmarkEncode#F6.: []int{10, 20, 30, 40}, |
| 724 | F7scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/LargeStructForBenchmarkEncode#F7.: 3.14159, F8scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/LargeStructForBenchmarkEncode#F8.: true, |
| 725 | F9scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/LargeStructForBenchmarkEncode#F9.: struct { |
| 726 | N1 timescip-go gomod github.com/golang/go/src go1.25.0 time/.Timescip-go gomod github.com/golang/go/src go1.25.0 time/Time# `schema:"n1"` |
| 727 | N2 string `schema:"n2"` |
| 728 | }{ |
| 729 | N1: timescip-go gomod github.com/golang/go/src go1.25.0 time/.Datescip-go gomod github.com/golang/go/src go1.25.0 time/Date().(2025, 1, 1, 12, 0, 0, 0, timescip-go gomod github.com/golang/go/src go1.25.0 time/.UTCscip-go gomod github.com/golang/go/src go1.25.0 time/UTC.), |
| 730 | N2: "NestedStringValue", |
| 731 | }, |
| 732 | } |
| 733 | enc := NewEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewEncoder().() |
| 734 | |
| 735 | // Optionally register a custom encoder for time.Time |
| 736 | enc.RegisterEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#RegisterEncoder().(timescip-go gomod github.com/golang/go/src go1.25.0 time/.Timescip-go gomod github.com/golang/go/src go1.25.0 time/Time#{}, func(v reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Valuescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#) string { |
| 737 | tVal, _ := reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.TypeAssertscip-go gomod github.com/golang/go/src go1.25.0 reflect/TypeAssert().[timescip-go gomod github.com/golang/go/src go1.25.0 time/.Timescip-go gomod github.com/golang/go/src go1.25.0 time/Time#](v) |
| 738 | return tVal.Formatscip-go gomod github.com/golang/go/src go1.25.0 time/Time#Format().(timescip-go gomod github.com/golang/go/src go1.25.0 time/.RFC3339scip-go gomod github.com/golang/go/src go1.25.0 time/RFC3339.) |
| 739 | }) |
| 740 | |
| 741 | vals := map[string][]string{} |
| 742 | for b.Loopscip-go gomod github.com/golang/go/src go1.25.0 testing/B#Loop().() { |
| 743 | _ = enc.Encodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#Encode().(&s, vals) |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | func BenchmarkLargeStructEncodeParallelscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/BenchmarkLargeStructEncodeParallel().(b *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Bscip-go gomod github.com/golang/go/src go1.25.0 testing/B#) { |
| 748 | s := LargeStructForBenchmarkEncodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/LargeStructForBenchmarkEncode#{ |
| 749 | F1scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/LargeStructForBenchmarkEncode#F1.: "Lorem", F2scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/LargeStructForBenchmarkEncode#F2.: "Ipsum", F3scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/LargeStructForBenchmarkEncode#F3.: 123, F4scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/LargeStructForBenchmarkEncode#F4.: 456, |
| 750 | F5scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/LargeStructForBenchmarkEncode#F5.: []string{"A", "B", "C", "D"}, |
| 751 | F6scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/LargeStructForBenchmarkEncode#F6.: []int{10, 20, 30, 40}, |
| 752 | F7scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/LargeStructForBenchmarkEncode#F7.: 3.14159, F8scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/LargeStructForBenchmarkEncode#F8.: true, |
| 753 | F9scip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/LargeStructForBenchmarkEncode#F9.: struct { |
| 754 | N1 timescip-go gomod github.com/golang/go/src go1.25.0 time/.Timescip-go gomod github.com/golang/go/src go1.25.0 time/Time# `schema:"n1"` |
| 755 | N2 string `schema:"n2"` |
| 756 | }{ |
| 757 | N1: timescip-go gomod github.com/golang/go/src go1.25.0 time/.Datescip-go gomod github.com/golang/go/src go1.25.0 time/Date().(2025, 1, 1, 12, 0, 0, 0, timescip-go gomod github.com/golang/go/src go1.25.0 time/.UTCscip-go gomod github.com/golang/go/src go1.25.0 time/UTC.), |
| 758 | N2: "NestedStringValue", |
| 759 | }, |
| 760 | } |
| 761 | enc := NewEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewEncoder().() |
| 762 | enc.RegisterEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#RegisterEncoder().(timescip-go gomod github.com/golang/go/src go1.25.0 time/.Timescip-go gomod github.com/golang/go/src go1.25.0 time/Time#{}, func(v reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Valuescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#) string { |
| 763 | tVal, _ := reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.TypeAssertscip-go gomod github.com/golang/go/src go1.25.0 reflect/TypeAssert().[timescip-go gomod github.com/golang/go/src go1.25.0 time/.Timescip-go gomod github.com/golang/go/src go1.25.0 time/Time#](v) |
| 764 | return tVal.Formatscip-go gomod github.com/golang/go/src go1.25.0 time/Time#Format().(timescip-go gomod github.com/golang/go/src go1.25.0 time/.RFC3339scip-go gomod github.com/golang/go/src go1.25.0 time/RFC3339.) |
| 765 | }) |
| 766 | |
| 767 | b.ResetTimerscip-go gomod github.com/golang/go/src go1.25.0 testing/B#ResetTimer().() |
| 768 | b.RunParallelscip-go gomod github.com/golang/go/src go1.25.0 testing/B#RunParallel().(func(pb *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.PBscip-go gomod github.com/golang/go/src go1.25.0 testing/PB#) { |
| 769 | vals := map[string][]string{} |
| 770 | for pb.Nextscip-go gomod github.com/golang/go/src go1.25.0 testing/PB#Next().() { |
| 771 | _ = enc.Encodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#Encode().(&s, vals) |
| 772 | } |
| 773 | }) |
| 774 | } |
| 775 | |
| 776 | func BenchmarkTimeDurationEncodingscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/BenchmarkTimeDurationEncoding().(b *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Bscip-go gomod github.com/golang/go/src go1.25.0 testing/B#) { |
| 777 | type DurationStruct struct { |
| 778 | Timeout timescip-go gomod github.com/golang/go/src go1.25.0 time/.Durationscip-go gomod github.com/golang/go/src go1.25.0 time/Duration# `schema:"timeout"` |
| 779 | } |
| 780 | |
| 781 | testData := DurationStruct{ |
| 782 | Timeout: 5 * timescip-go gomod github.com/golang/go/src go1.25.0 time/.Secondscip-go gomod github.com/golang/go/src go1.25.0 time/Second., |
| 783 | } |
| 784 | |
| 785 | enc := NewEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewEncoder().() |
| 786 | enc.RegisterEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#RegisterEncoder().(timescip-go gomod github.com/golang/go/src go1.25.0 time/.Durationscip-go gomod github.com/golang/go/src go1.25.0 time/Duration#(0), func(v reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Valuescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#) string { |
| 787 | d, _ := reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.TypeAssertscip-go gomod github.com/golang/go/src go1.25.0 reflect/TypeAssert().[timescip-go gomod github.com/golang/go/src go1.25.0 time/.Durationscip-go gomod github.com/golang/go/src go1.25.0 time/Duration#](v) |
| 788 | return d.Stringscip-go gomod github.com/golang/go/src go1.25.0 time/Duration#String().() |
| 789 | }) |
| 790 | |
| 791 | vals := map[string][]string{} |
| 792 | for b.Loopscip-go gomod github.com/golang/go/src go1.25.0 testing/B#Loop().() { |
| 793 | _ = enc.Encodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#Encode().(&testData, vals) |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | // A plan build racing SetAliasTag/RegisterEncoder must not re-insert a |
| 798 | // stale plan after the invalidation; once the reconfiguration call returns, |
| 799 | // every subsequent Encode must observe it. |
| 800 | func TestEncoderReconfigureDuringEncodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/TestEncoderReconfigureDuringEncode().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 801 | type T1 struct { |
| 802 | A string `schema:"schemaname" url:"urlname"` |
| 803 | } |
| 804 | for i := 0; i < 300; i++ { |
| 805 | e := NewEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewEncoder().() |
| 806 | done := make(chan struct{}) |
| 807 | go func() { |
| 808 | defer close(done) |
| 809 | dst := map[string][]string{} |
| 810 | _ = e.Encodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#Encode().(T1{A: "x"}, dst) |
| 811 | }() |
| 812 | e.SetAliasTagscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#SetAliasTag().("url") |
| 813 | <-done |
| 814 | |
| 815 | dst := map[string][]string{} |
| 816 | if err := e.Encodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#Encode().(T1{A: "x"}, dst); err != nil { |
| 817 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 818 | } |
| 819 | if _, ok := dst["urlname"]; !ok { |
| 820 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("iteration %d: encode after SetAliasTag returned stale plan: %v", i, dst) |
| 821 | } |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | // Errors inside recursed structs (value and pointer) must be collected under |
| 826 | // the nested type's name. |
| 827 | func TestEncoderNestedErrorsscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/TestEncoderNestedErrors().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 828 | type Bad struct { |
| 829 | M map[string]string `schema:"m"` |
| 830 | } |
| 831 | type S struct { |
| 832 | V Bad `schema:"v"` |
| 833 | P *Bad `schema:"p"` |
| 834 | } |
| 835 | enc := NewEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewEncoder().() |
| 836 | dst := map[string][]string{} |
| 837 | err := enc.Encodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#Encode().(S{V: Bad{M: map[string]string{"a": "b"}}, P: &Bad{}}, dst) |
| 838 | if err == nil { |
| 839 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().("expected error for unsupported nested field") |
| 840 | } |
| 841 | if !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(err.Error(), "encoder not found") { |
| 842 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("unexpected error: %v", err) |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | // An encoding plan stored by a build that raced a reconfiguration carries |
| 847 | // the old generation and must never be served after the reconfiguration |
| 848 | // returned, even if it lands in the cache. |
| 849 | func TestEncoderStaleCacheEntryIgnoredscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/TestEncoderStaleCacheEntryIgnored().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 850 | type T1 struct { |
| 851 | A string `schema:"schemaname" url:"urlname"` |
| 852 | } |
| 853 | e := NewEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewEncoder().() |
| 854 | typ := reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.TypeOfscip-go gomod github.com/golang/go/src go1.25.0 reflect/TypeOf().(T1{}) |
| 855 | |
| 856 | dst := map[string][]string{} |
| 857 | if err := e.Encodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#Encode().(T1{A: "x"}, dst); err != nil { |
| 858 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 859 | } |
| 860 | stalePlan, ok := e.encCachescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#encCache..Loadscip-go gomod github.com/golang/go/src go1.25.0 sync/Map#Load().(typ) |
| 861 | if !ok { |
| 862 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().("expected cached plan") |
| 863 | } |
| 864 | |
| 865 | e.SetAliasTagscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#SetAliasTag().("url") |
| 866 | // Simulate a delayed store from a build that raced the reconfiguration. |
| 867 | e.encCachescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#encCache..Storescip-go gomod github.com/golang/go/src go1.25.0 sync/Map#Store().(typ, stalePlan) |
| 868 | |
| 869 | dst = map[string][]string{} |
| 870 | if err := e.Encodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#Encode().(T1{A: "y"}, dst); err != nil { |
| 871 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 872 | } |
| 873 | if _, ok := dst["urlname"]; !ok { |
| 874 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("stale plan served: %v", dst) |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | // Pins the four cases the removed isValidStructPointer helper used to |
| 879 | // classify, now decided by the cached plan's recurseStructPtr flag plus a |
| 880 | // runtime nil check: valid struct pointers recurse, nil struct pointers |
| 881 | // encode as "null", struct values recurse, and pointers to non-structs |
| 882 | // encode their element (or "null" when nil). |
| 883 | func TestEncodeStructPointerClassificationscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/TestEncodeStructPointerClassification().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 884 | type Inner struct { |
| 885 | X string `schema:"x"` |
| 886 | } |
| 887 | type S struct { |
| 888 | SP *Inner `schema:"sp"` // valid struct pointer -> recursed |
| 889 | NP *Inner `schema:"np"` // nil struct pointer -> "null" |
| 890 | SV Inner `schema:"sv"` // struct value -> recursed |
| 891 | IP *int `schema:"ip"` // pointer to non-struct -> element |
| 892 | NIP *int `schema:"nip"` // nil pointer to non-struct -> "null" |
| 893 | } |
| 894 | |
| 895 | seven := 7 |
| 896 | src := S{ |
| 897 | SP: &Inner{X: "from-ptr"}, |
| 898 | SV: Inner{X: "from-val"}, |
| 899 | IP: &seven, |
| 900 | } |
| 901 | // SP and SV both recurse and write under the inner field's alias; SV is |
| 902 | // encoded after SP overwrote nothing (values append under "x"). |
| 903 | dst := map[string][]string{} |
| 904 | if err := NewEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewEncoder().().Encodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#Encode().(src, dst); err != nil { |
| 905 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 906 | } |
| 907 | |
| 908 | if got := dst["x"]; len(got) != 2 || got[0] != "from-ptr" || got[1] != "from-val" { |
| 909 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("struct pointer/value recursion: expected [from-ptr from-val] under \"x\", got %v", got) |
| 910 | } |
| 911 | if _, ok := dst["sp"]; ok { |
| 912 | t.Errorscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Error().("valid struct pointer must recurse, not encode under its own alias") |
| 913 | } |
| 914 | if got := dst["np"]; len(got) != 1 || got[0] != "null" { |
| 915 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("nil struct pointer: expected [null], got %v", got) |
| 916 | } |
| 917 | if got := dst["ip"]; len(got) != 1 || got[0] != "7" { |
| 918 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("pointer to non-struct: expected [7], got %v", got) |
| 919 | } |
| 920 | if got := dst["nip"]; len(got) != 1 || got[0] != "null" { |
| 921 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("nil pointer to non-struct: expected [null], got %v", got) |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | // Encoding into a nil destination map used to panic and crash the caller. |
| 926 | func TestEncodeNilDstReturnsErrorscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/TestEncodeNilDstReturnsError().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 927 | type S struct { |
| 928 | A string `schema:"a"` |
| 929 | } |
| 930 | err := NewEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewEncoder().().Encodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#Encode().(S{A: "x"}, nil) |
| 931 | if err == nil || !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(err.Error(), "dst map must not be nil") { |
| 932 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("expected nil-dst error, got %v", err) |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | // Non-nil pointers to unsupported element types used to invoke a nil inner |
| 937 | // encoder and panic, crashing the caller; they must error instead, while |
| 938 | // nil pointers keep encoding as "null". |
| 939 | func TestEncodePointerToUnsupportedscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/TestEncodePointerToUnsupported().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 940 | type S struct { |
| 941 | M *map[string]string `schema:"m"` |
| 942 | L *[]int `schema:"l"` |
| 943 | A string `schema:"a"` |
| 944 | } |
| 945 | |
| 946 | m := map[string]string{"k": "v"} |
| 947 | l := []int{1} |
| 948 | dst := map[string][]string{} |
| 949 | err := NewEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewEncoder().().Encodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#Encode().(S{M: &m, L: &l, A: "x"}, dst) |
| 950 | if err == nil || !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(err.Error(), "encoder not found") { |
| 951 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("expected encoder-not-found error, got %v", err) |
| 952 | } |
| 953 | if got := dst["a"]; len(got) != 1 || got[0] != "x" { |
| 954 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("supported sibling must still encode: %v", dst) |
| 955 | } |
| 956 | |
| 957 | // Nil pointers to unsupported types keep the historical "null" output. |
| 958 | dst = map[string][]string{} |
| 959 | if err := NewEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewEncoder().().Encodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#Encode().(S{A: "x"}, dst); err != nil { |
| 960 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 961 | } |
| 962 | if got := dst["m"]; len(got) != 1 || got[0] != "null" { |
| 963 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("nil *map: expected [null], got %v", dst["m"]) |
| 964 | } |
| 965 | if got := dst["l"]; len(got) != 1 || got[0] != "null" { |
| 966 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("nil *slice: expected [null], got %v", dst["l"]) |
| 967 | } |
| 968 | |
| 969 | // omitempty suppresses the null. |
| 970 | type SO struct { |
| 971 | M *map[string]string `schema:"m,omitempty"` |
| 972 | } |
| 973 | dst = map[string][]string{} |
| 974 | if err := NewEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewEncoder().().Encodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#Encode().(SO{}, dst); err != nil { |
| 975 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 976 | } |
| 977 | if _, ok := dst["m"]; ok { |
| 978 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("omitempty nil pointer must be skipped: %v", dst) |
| 979 | } |
| 980 | } |
| 981 | |
| 982 | // Panics from user-registered encoders (or reflection) must surface as |
| 983 | // errors, mirroring Decode's recovery, instead of crashing the caller. |
| 984 | func TestEncodeRecoversEncoderPanicscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/TestEncodeRecoversEncoderPanic().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 985 | type PT struct{ V int } |
| 986 | type S struct { |
| 987 | P PT `schema:"p"` |
| 988 | } |
| 989 | enc := NewEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewEncoder().() |
| 990 | enc.RegisterEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#RegisterEncoder().(PT{}, func(reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Valuescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#) string { panic("boom") }) |
| 991 | err := enc.Encodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#Encode().(S{P: PT{V: 1}}, map[string][]string{}) |
| 992 | if err == nil || !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(err.Error(), "panic while encoding: boom") { |
| 993 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("expected recovered panic error, got %v", err) |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | // An empty slice whose element type has no encoder (e.g. []*Struct) must be |
| 998 | // skipped under omitempty and emitted empty otherwise, not spuriously error; |
| 999 | // only a non-empty such slice errors. Non-slice unencodable fields still error. |
| 1000 | func TestEncodeEmptySliceUnencodableElemscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/TestEncodeEmptySliceUnencodableElem().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 1001 | type Inner struct { |
| 1002 | A string `schema:"a"` |
| 1003 | } |
| 1004 | |
| 1005 | // omitempty + nil slice -> skipped, no error. |
| 1006 | type SOmit struct { |
| 1007 | Children []*Inner `schema:"children,omitempty"` |
| 1008 | Name string `schema:"name"` |
| 1009 | } |
| 1010 | dst := map[string][]string{} |
| 1011 | if err := NewEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewEncoder().().Encodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#Encode().(SOmit{Name: "bob"}, dst); err != nil { |
| 1012 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("omitempty empty slice should not error: %v", err) |
| 1013 | } |
| 1014 | if _, ok := dst["children"]; ok { |
| 1015 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("omitempty empty slice should be skipped, got %v", dst["children"]) |
| 1016 | } |
| 1017 | if got := dst["name"]; len(got) != 1 || got[0] != "bob" { |
| 1018 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("sibling not encoded: %v", dst) |
| 1019 | } |
| 1020 | |
| 1021 | // omitempty + non-nil empty slice -> also skipped (the check is Len-based, |
| 1022 | // not a nil check). |
| 1023 | dst = map[string][]string{} |
| 1024 | if err := NewEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewEncoder().().Encodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#Encode().(SOmit{Children: make([]*Inner, 0), Name: "bob"}, dst); err != nil { |
| 1025 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("omitempty non-nil empty slice should not error: %v", err) |
| 1026 | } |
| 1027 | if _, ok := dst["children"]; ok { |
| 1028 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("omitempty non-nil empty slice should be skipped, got %v", dst["children"]) |
| 1029 | } |
| 1030 | |
| 1031 | // no omitempty + nil slice -> emitted empty, no error. |
| 1032 | type SPlain struct { |
| 1033 | Children []*Inner `schema:"children"` |
| 1034 | Name string `schema:"name"` |
| 1035 | } |
| 1036 | dst = map[string][]string{} |
| 1037 | if err := NewEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewEncoder().().Encodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#Encode().(SPlain{Name: "bob"}, dst); err != nil { |
| 1038 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("empty slice should not error: %v", err) |
| 1039 | } |
| 1040 | if got, ok := dst["children"]; !ok || len(got) != 0 { |
| 1041 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("empty slice should emit empty, got %v (present=%v)", got, ok) |
| 1042 | } |
| 1043 | |
| 1044 | // no omitempty + non-nil empty slice -> also emitted empty, no error. |
| 1045 | dst = map[string][]string{} |
| 1046 | if err := NewEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewEncoder().().Encodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#Encode().(SPlain{Children: make([]*Inner, 0), Name: "bob"}, dst); err != nil { |
| 1047 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("non-nil empty slice should not error: %v", err) |
| 1048 | } |
| 1049 | if got, ok := dst["children"]; !ok || len(got) != 0 { |
| 1050 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("non-nil empty slice should emit empty, got %v (present=%v)", got, ok) |
| 1051 | } |
| 1052 | |
| 1053 | // non-empty slice of unencodable element -> error. |
| 1054 | dst = map[string][]string{} |
| 1055 | err := NewEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewEncoder().().Encodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#Encode().(SPlain{Children: []*Inner{{A: "x"}}, Name: "bob"}, dst) |
| 1056 | if err == nil || !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(err.Error(), "encoder not found") { |
| 1057 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("non-empty unencodable-elem slice should error, got %v", err) |
| 1058 | } |
| 1059 | |
| 1060 | // []*Struct with nil elements encodes each nil as "null" (as historically), |
| 1061 | // matching a scalar nil *Struct; a non-nil element is an error. |
| 1062 | dst = map[string][]string{} |
| 1063 | if err := NewEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewEncoder().().Encodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#Encode().(SPlain{Children: []*Inner{nil, nil}, Name: "bob"}, dst); err != nil { |
| 1064 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("[]*Struct nil elements should encode as null, got %v", err) |
| 1065 | } |
| 1066 | if got := dst["children"]; len(got) != 2 || got[0] != "null" || got[1] != "null" { |
| 1067 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("nil elements: expected [null null], got %v", got) |
| 1068 | } |
| 1069 | |
| 1070 | // A value-struct slice ([]Struct) is unencodable and errors even when empty. |
| 1071 | type SVal struct { |
| 1072 | Items []Inner `schema:"items"` |
| 1073 | } |
| 1074 | if err := NewEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewEncoder().().Encodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#Encode().(SVal{}, map[string][]string{}); err == nil { |
| 1075 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("empty []Struct (value) should error, matching historical behavior") |
| 1076 | } |
| 1077 | |
| 1078 | // non-slice unencodable field (map) still errors regardless of emptiness. |
| 1079 | type SMap struct { |
| 1080 | M map[string]string `schema:"m"` |
| 1081 | } |
| 1082 | if err := NewEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewEncoder().().Encodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#Encode().(SMap{}, map[string][]string{}); err == nil { |
| 1083 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("empty map field should still error 'encoder not found'") |
| 1084 | } |
| 1085 | if err := NewEncoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewEncoder().().Encodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Encoder#Encode().(SMap{M: map[string]string{"k": "v"}}, map[string][]string{}); err == nil { |
| 1086 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("non-empty map field should error 'encoder not found'") |
| 1087 | } |
| 1088 | } |