From 4af91faa7ed8f67c50f5f279d6cec4eaf414bc63 Mon Sep 17 00:00:00 2001 From: Travis Date: Fri, 8 Nov 2019 15:56:15 -0600 Subject: [PATCH 1/3] fix cacheSize when cacheType is none (and cacheSize is 0) There was an edge case where setting cacheType to none wouldn't zero out its cacheSize. This fixes that edge case. --- field.go | 10 ++++------ field_internal_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/field.go b/field.go index 38b364b67..1f71812b3 100644 --- a/field.go +++ b/field.go @@ -596,12 +596,10 @@ func (f *Field) applyOptions(opt FieldOptions) error { if opt.CacheType != "" { f.options.CacheType = opt.CacheType } - if opt.CacheSize != 0 { - if opt.CacheType == CacheTypeNone { - f.options.CacheSize = 0 - } else { - f.options.CacheSize = opt.CacheSize - } + if opt.CacheType == CacheTypeNone { + f.options.CacheSize = 0 + } else if opt.CacheSize != 0 { + f.options.CacheSize = opt.CacheSize } f.options.Min = 0 f.options.Max = 0 diff --git a/field_internal_test.go b/field_internal_test.go index 7cadb2dc8..b382813e4 100644 --- a/field_internal_test.go +++ b/field_internal_test.go @@ -438,3 +438,35 @@ func TestBSIGroup_BaseDefaultValue(t *testing.T) { } } } + +func TestField_ApplyOptions(t *testing.T) { + for i, tt := range []struct { + opts FieldOptions + expOpts FieldOptions + }{ + { + FieldOptions{ + Type: FieldTypeSet, + CacheType: CacheTypeNone, + CacheSize: 0, + }, + FieldOptions{ + Type: FieldTypeSet, + CacheType: CacheTypeNone, + CacheSize: 0, + }, + }, + } { + + fld := &Field{} + fld.options = applyDefaultOptions(FieldOptions{}) + + fld.applyOptions(tt.opts) + + if fld.options.CacheType != tt.expOpts.CacheType { + t.Fatalf("test %d, unexpected FieldOptions.CacheType value. expected: %s, but got: %s", i, tt.expOpts.CacheType, fld.options.CacheType) + } else if fld.options.CacheSize != tt.expOpts.CacheSize { + t.Fatalf("test %d, unexpected FieldOptions.CacheSize value. expected: %d, but got: %d", i, tt.expOpts.CacheSize, fld.options.CacheSize) + } + } +} From 4272693641a4f77ac5560baaedd0c5da62fe9de4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Podg=C3=B3rski?= Date: Thu, 9 Apr 2020 15:04:11 +0200 Subject: [PATCH 2/3] Update field_internal_test.go --- field_internal_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/field_internal_test.go b/field_internal_test.go index cb8d5b3f3..2eda0133e 100644 --- a/field_internal_test.go +++ b/field_internal_test.go @@ -461,7 +461,9 @@ func TestField_ApplyOptions(t *testing.T) { fld := &Field{} fld.options = applyDefaultOptions(FieldOptions{}) - fld.applyOptions(tt.opts) + if err := fld.applyOptions(tt.opts); err 1= nil { + t.Fatal(err) + } if fld.options.CacheType != tt.expOpts.CacheType { t.Fatalf("test %d, unexpected FieldOptions.CacheType value. expected: %s, but got: %s", i, tt.expOpts.CacheType, fld.options.CacheType) From 9cdaff542f0ce49b499febe2bf88e5b0495b6f6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Podg=C3=B3rski?= Date: Thu, 9 Apr 2020 15:04:34 +0200 Subject: [PATCH 3/3] Update field_internal_test.go --- field_internal_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/field_internal_test.go b/field_internal_test.go index 2eda0133e..1f7f8303e 100644 --- a/field_internal_test.go +++ b/field_internal_test.go @@ -461,7 +461,7 @@ func TestField_ApplyOptions(t *testing.T) { fld := &Field{} fld.options = applyDefaultOptions(FieldOptions{}) - if err := fld.applyOptions(tt.opts); err 1= nil { + if err := fld.applyOptions(tt.opts); err != nil { t.Fatal(err) }