From c5140ba88d29952bd1e9cd2a362aef387ed3912e Mon Sep 17 00:00:00 2001 From: Travis Turner Date: Wed, 31 Jul 2019 09:19:04 -0500 Subject: [PATCH 1/3] default BSI base value to min, max, or 0 depending on the min/max range --- field.go | 9 +++++++++ field_internal_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/field.go b/field.go index 9b49d03b6..cad768350 100644 --- a/field.go +++ b/field.go @@ -143,6 +143,15 @@ func OptFieldTypeInt(min, max int64) FieldOption { fo.Type = FieldTypeInt fo.Min = min fo.Max = max + // Base is not exposed as a field option argument. + // It defaults to min, max, or 0 depending on the min/max range. + if min > 0 { + fo.Base = min + } else if max < 0 { + fo.Base = max + } else { + fo.Base = 0 + } return nil } } diff --git a/field_internal_test.go b/field_internal_test.go index 13851a493..1ce7c8237 100644 --- a/field_internal_test.go +++ b/field_internal_test.go @@ -412,3 +412,29 @@ func TestField_PersistAvailableShardsFootprint(t *testing.T) { } } + +// Ensure that FieldOptions.Base defaults to the correct value. +func TestBSIGroup_BaseDefaultValue(t *testing.T) { + for i, tt := range []struct { + min int64 + max int64 + expBase int64 + }{ + {100, 200, 100}, + {-100, 100, 0}, + {-200, -100, -100}, + } { + fn := OptFieldTypeInt(tt.min, tt.max) + + // Apply functional option. + fo := FieldOptions{} + err := fn(&fo) + if err != nil { + t.Fatalf("test %d, applying functional option: %s", i, err.Error()) + } + + if fo.Base != tt.expBase { + t.Fatalf("test %d, unexpected FieldOptions.Base value. expected: %d, but got: %d", i, tt.expBase, fo.Base) + } + } +} From 9e9103d98f74d42c2895665d424d1c3ea56849bf Mon Sep 17 00:00:00 2001 From: Travis Turner Date: Wed, 31 Jul 2019 11:38:36 -0500 Subject: [PATCH 2/3] apply default base logic to BSI v1 migration code --- field.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/field.go b/field.go index cad768350..d9ce05716 100644 --- a/field.go +++ b/field.go @@ -508,7 +508,13 @@ func (f *Field) loadMeta() error { // Initialize "base" to "min" when upgrading from v1 BSI format. if pb.BitDepth == 0 { - pb.Base = pb.Min + if pb.Min > 0 { + pb.Base = pb.Min + } else if pb.Max < 0 { + pb.Base = pb.Max + } else { + pb.Base = 0 + } pb.BitDepth = uint64(bitDepthInt64(pb.Max - pb.Min)) if pb.BitDepth == 0 { pb.BitDepth = 1 From 96e6c1189793a8981938b767cbb6f1b389985362 Mon Sep 17 00:00:00 2001 From: Travis Turner Date: Wed, 31 Jul 2019 12:43:05 -0500 Subject: [PATCH 3/3] add bsiBase() helper function to avoid duplication --- field.go | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/field.go b/field.go index d9ce05716..c803ea36c 100644 --- a/field.go +++ b/field.go @@ -143,15 +143,7 @@ func OptFieldTypeInt(min, max int64) FieldOption { fo.Type = FieldTypeInt fo.Min = min fo.Max = max - // Base is not exposed as a field option argument. - // It defaults to min, max, or 0 depending on the min/max range. - if min > 0 { - fo.Base = min - } else if max < 0 { - fo.Base = max - } else { - fo.Base = 0 - } + fo.Base = bsiBase(min, max) return nil } } @@ -508,13 +500,7 @@ func (f *Field) loadMeta() error { // Initialize "base" to "min" when upgrading from v1 BSI format. if pb.BitDepth == 0 { - if pb.Min > 0 { - pb.Base = pb.Min - } else if pb.Max < 0 { - pb.Base = pb.Max - } else { - pb.Base = 0 - } + pb.Base = bsiBase(pb.Min, pb.Max) pb.BitDepth = uint64(bitDepthInt64(pb.Max - pb.Min)) if pb.BitDepth == 0 { pb.BitDepth = 1 @@ -1516,6 +1502,18 @@ func isValidBSIGroupType(v string) bool { } } +// bsiBase is a helper function used to determine the default value +// for base. Because base is not exposed as a field option argument, +// it defaults to min, max, or 0 depending on the min/max range. +func bsiBase(min, max int64) int64 { + if min > 0 { + return min + } else if max < 0 { + return max + } + return 0 +} + // bsiGroup represents a group of range-encoded rows on a field. type bsiGroup struct { Name string `json:"name,omitempty"`