Add openapi3.Unmarshal

Fixes #7442
Fixes #7443
This commit is contained in:
Bjørn Erik Pedersen
2020-06-30 16:11:05 +02:00
parent 58c0f5e617
commit 12a65e76df
12 changed files with 268 additions and 26 deletions

View File

@@ -14,6 +14,7 @@
package types
import (
"encoding/json"
"html/template"
"github.com/spf13/cast"
@@ -59,10 +60,20 @@ func TypeToString(v interface{}) (string, bool) {
// ToString converts v to a string.
func ToString(v interface{}) string {
s, _ := ToStringE(v)
return s
}
// ToStringE converts v to a string.
func ToStringE(v interface{}) (string, error) {
if s, ok := TypeToString(v); ok {
return s
return s, nil
}
return cast.ToString(v)
switch s := v.(type) {
case json.RawMessage:
return string(s), nil
default:
return cast.ToStringE(v)
}
}

View File

@@ -14,6 +14,7 @@
package types
import (
"encoding/json"
"testing"
qt "github.com/frankban/quicktest"
@@ -27,3 +28,11 @@ func TestToStringSlicePreserveString(t *testing.T) {
c.Assert(ToStringSlicePreserveString(nil), qt.IsNil)
}
func TestToString(t *testing.T) {
c := qt.New(t)
c.Assert(ToString([]byte("Hugo")), qt.Equals, "Hugo")
c.Assert(ToString(json.RawMessage("Hugo")), qt.Equals, "Hugo")
}