Skip to content

Commit daeb3b9

Browse files
author
James Waters
committed
Add deepobject support for test unmarshalers
1 parent b62303b commit daeb3b9

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

deepobject.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package runtime
22

33
import (
4+
"encoding"
45
"encoding/json"
56
"errors"
67
"fmt"
@@ -199,6 +200,18 @@ func assignPathValues(dst interface{}, pathValues fieldOrValue) error {
199200
iv := reflect.Indirect(v)
200201
it := iv.Type()
201202

203+
switch dst := v.Interface().(type) {
204+
case Binder:
205+
return dst.Bind(pathValues.value)
206+
case encoding.TextUnmarshaler:
207+
err := dst.UnmarshalText([]byte(pathValues.value))
208+
if err != nil {
209+
return fmt.Errorf("error unmarshalling text '%s': %w", pathValues.value, err)
210+
}
211+
212+
return nil
213+
}
214+
202215
switch it.Kind() {
203216
case reflect.Map:
204217
dstMap := reflect.MakeMap(iv.Type())

deepobject_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77
"time"
88

9+
"github.com/google/uuid"
910
"github.com/oapi-codegen/runtime/types"
1011
"github.com/stretchr/testify/assert"
1112
"github.com/stretchr/testify/require"
@@ -39,6 +40,8 @@ type AllFields struct {
3940
Oda *types.Date `json:"oda,omitempty"`
4041
Ti time.Time `json:"ti"`
4142
Oti *time.Time `json:"oti,omitempty"`
43+
U types.UUID `json:"u"`
44+
Ou *types.UUID `json:"ou,omitempty"`
4245
}
4346

4447
func TestDeepObject(t *testing.T) {
@@ -57,6 +60,7 @@ func TestDeepObject(t *testing.T) {
5760
bi := MockBinder{Time: time.Date(2020, 2, 1, 0, 0, 0, 0, time.UTC)}
5861
da := types.Date{Time: time.Date(2020, 2, 2, 0, 0, 0, 0, time.UTC)}
5962
ti := time.Now().UTC()
63+
u := uuid.New()
6064

6165
srcObj := AllFields{
6266
// Primitive types
@@ -83,6 +87,8 @@ func TestDeepObject(t *testing.T) {
8387
Oda: &da,
8488
Ti: ti,
8589
Oti: &ti,
90+
U: u,
91+
Ou: &u,
8692
}
8793

8894
marshaled, err := MarshalDeepObject(srcObj, "p")

0 commit comments

Comments
 (0)