Skip to content

Commit

Permalink
Merge pull request #390 from liggitt/mapstructure
Browse files Browse the repository at this point in the history
Drop unused mapstructure functionality
  • Loading branch information
k8s-ci-robot authored May 15, 2023
2 parents 3d976ce + a1ae96b commit 54b630e
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 195 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ require (
github.com/google/go-cmp v0.5.5
github.com/google/gofuzz v1.1.0
github.com/google/uuid v1.1.2
github.com/mitchellh/mapstructure v1.1.2
github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d
github.com/onsi/ginkgo/v2 v2.1.4
github.com/onsi/gomega v1.19.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand Down
81 changes: 0 additions & 81 deletions pkg/validation/strfmt/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@ package strfmt

import (
"encoding"
"fmt"
"reflect"
"strings"
"sync"
"time"

"github.com/mitchellh/mapstructure"
"k8s.io/kube-openapi/pkg/validation/errors"
)

Expand Down Expand Up @@ -50,7 +47,6 @@ type Registry interface {
ContainsName(string) bool
Validates(string, string) bool
Parse(string, string) (interface{}, error)
MapStructureHookFunc() mapstructure.DecodeHookFunc
}

type knownFormat struct {
Expand Down Expand Up @@ -92,83 +88,6 @@ func NewSeededFormats(seeds []knownFormat, normalizer NameNormalizer) Registry {
}
}

// MapStructureHookFunc is a decode hook function for mapstructure
func (f *defaultFormats) MapStructureHookFunc() mapstructure.DecodeHookFunc {
return func(from reflect.Type, to reflect.Type, data interface{}) (interface{}, error) {
if from.Kind() != reflect.String {
return data, nil
}
for _, v := range f.data {
tpe, _ := f.GetType(v.Name)
if to == tpe {
switch v.Name {
case "date":
d, err := time.Parse(RFC3339FullDate, data.(string))
if err != nil {
return nil, err
}
return Date(d), nil
case "datetime":
input := data.(string)
if len(input) == 0 {
return nil, fmt.Errorf("empty string is an invalid datetime format")
}
return ParseDateTime(input)
case "duration":
dur, err := ParseDuration(data.(string))
if err != nil {
return nil, err
}
return Duration(dur), nil
case "uri":
return URI(data.(string)), nil
case "email":
return Email(data.(string)), nil
case "uuid":
return UUID(data.(string)), nil
case "uuid3":
return UUID3(data.(string)), nil
case "uuid4":
return UUID4(data.(string)), nil
case "uuid5":
return UUID5(data.(string)), nil
case "hostname":
return Hostname(data.(string)), nil
case "ipv4":
return IPv4(data.(string)), nil
case "ipv6":
return IPv6(data.(string)), nil
case "cidr":
return CIDR(data.(string)), nil
case "mac":
return MAC(data.(string)), nil
case "isbn":
return ISBN(data.(string)), nil
case "isbn10":
return ISBN10(data.(string)), nil
case "isbn13":
return ISBN13(data.(string)), nil
case "creditcard":
return CreditCard(data.(string)), nil
case "ssn":
return SSN(data.(string)), nil
case "hexcolor":
return HexColor(data.(string)), nil
case "rgbcolor":
return RGBColor(data.(string)), nil
case "byte":
return Base64(data.(string)), nil
case "password":
return Password(data.(string)), nil
default:
return nil, errors.InvalidTypeName(v.Name)
}
}
}
return data, nil
}
}

// Add adds a new format, return true if this was a new item instead of a replacement
func (f *defaultFormats) Add(name string, strfmt Format, validator Validator) bool {
f.Lock()
Expand Down
111 changes: 0 additions & 111 deletions pkg/validation/strfmt/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ package strfmt
import (
"strings"
"testing"
"time"

"github.com/mitchellh/mapstructure"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -152,112 +150,3 @@ type testStruct struct {
B64 Base64 `json:"b64,omitempty"`
Pw Password `json:"pw,omitempty"`
}

func TestDecodeHook(t *testing.T) {
registry := NewFormats()
m := map[string]interface{}{
"d": "2014-12-15",
"dt": "2012-03-02T15:06:05.999999999Z",
"dur": "5s",
"uri": "http://www.dummy.com",
"eml": "[email protected]",
"uuid": "a8098c1a-f86e-11da-bd1a-00112444be1e",
"uuid3": "bcd02e22-68f0-3046-a512-327cca9def8f",
"uuid4": "025b0d74-00a2-4048-bf57-227c5111bb34",
"uuid5": "886313e1-3b8a-5372-9b90-0c9aee199e5d",
"hn": "somewhere.com",
"ipv4": "192.168.254.1",
"ipv6": "::1",
"cidr": "192.0.2.1/24",
"mac": "01:02:03:04:05:06",
"isbn": "0321751043",
"isbn10": "0321751043",
"isbn13": "978-0321751041",
"hexcolor": "#FFFFFF",
"rgbcolor": "rgb(255,255,255)",
"pw": "super secret stuff here",
"ssn": "111-11-1111",
"creditcard": "4111-1111-1111-1111",
"b64": "ZWxpemFiZXRocG9zZXk=",
}

date, _ := time.Parse(RFC3339FullDate, "2014-12-15")
dur, _ := ParseDuration("5s")
dt, _ := ParseDateTime("2012-03-02T15:06:05.999999999Z")

exp := &testStruct{
D: Date(date),
DT: dt,
Dur: Duration(dur),
URI: URI("http://www.dummy.com"),
Eml: Email("[email protected]"),
UUID: UUID("a8098c1a-f86e-11da-bd1a-00112444be1e"),
UUID3: UUID3("bcd02e22-68f0-3046-a512-327cca9def8f"),
UUID4: UUID4("025b0d74-00a2-4048-bf57-227c5111bb34"),
UUID5: UUID5("886313e1-3b8a-5372-9b90-0c9aee199e5d"),
Hn: Hostname("somewhere.com"),
Ipv4: IPv4("192.168.254.1"),
Ipv6: IPv6("::1"),
Cidr: CIDR("192.0.2.1/24"),
Mac: MAC("01:02:03:04:05:06"),
Isbn: ISBN("0321751043"),
Isbn10: ISBN10("0321751043"),
Isbn13: ISBN13("978-0321751041"),
Creditcard: CreditCard("4111-1111-1111-1111"),
Ssn: SSN("111-11-1111"),
Hexcolor: HexColor("#FFFFFF"),
Rgbcolor: RGBColor("rgb(255,255,255)"),
B64: Base64("ZWxpemFiZXRocG9zZXk="),
Pw: Password("super secret stuff here"),
}

test := new(testStruct)
cfg := &mapstructure.DecoderConfig{
DecodeHook: registry.MapStructureHookFunc(),
// weakly typed will pass if this passes
WeaklyTypedInput: false,
Result: test,
}
d, err := mapstructure.NewDecoder(cfg)
assert.Nil(t, err)
err = d.Decode(m)
assert.Nil(t, err)
assert.Equal(t, exp, test)
}

func TestDecodeDateTimeHook(t *testing.T) {
testCases := []struct {
Name string
Input string
}{
{
"empty datetime",
"",
},
{
"invalid non empty datetime",
"2019-01-01",
},
}
registry := NewFormats()
type layout struct {
DateTime *DateTime `json:"datetime,omitempty"`
}
for i := range testCases {
tc := testCases[i]
t.Run(tc.Name, func(t *testing.T) {
test := new(layout)
cfg := &mapstructure.DecoderConfig{
DecodeHook: registry.MapStructureHookFunc(),
WeaklyTypedInput: false,
Result: test,
}
d, err := mapstructure.NewDecoder(cfg)
assert.Nil(t, err)
input := make(map[string]interface{})
input["datetime"] = tc.Input
err = d.Decode(input)
assert.Error(t, err, "error expected got none")
})
}
}

0 comments on commit 54b630e

Please sign in to comment.