Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

Commit

Permalink
restored OptFieldTypeInt single arg version
Browse files Browse the repository at this point in the history
  • Loading branch information
yuce committed Jun 12, 2019
1 parent 874d72a commit 2ac5f48
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
9 changes: 6 additions & 3 deletions orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -755,16 +755,19 @@ func OptFieldTypeSet(cacheType CacheType, cacheSize int) FieldOption {

// OptFieldTypeInt adds an integer field.
// No arguments: min = min_int, max = max_int
// 1 argument: min = limit[0], max = max_int
// 2 or more arguments: min = limit[0], max = limit[1]
func OptFieldTypeInt(limits ...int64) FieldOption {
min := int64(math.MinInt64)
max := int64(math.MaxInt64)

if len(limits) != 0 && len(limits) != 2 {
panic("error: OptFieldTypInt accepts or 2 arguments")
if len(limits) > 2 {
panic("error: OptFieldTypInt accepts at most 2 arguments")
}
if len(limits) == 2 {
if len(limits) > 0 {
min = limits[0]
}
if len(limits) > 1 {
max = limits[1]
}

Expand Down
8 changes: 8 additions & 0 deletions orm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,14 @@ func TestIntFieldOptions(t *testing.T) {
}
compareFieldOptions(t, field.Options(), FieldTypeInt, TimeQuantumNone, CacheTypeDefault, 0, -10, 100)

field = sampleIndex.Field("int-field2", OptFieldTypeInt(-10))
jsonString = field.options.String()
targetString = fmt.Sprintf(`{"options":{"type":"int","min":-10,"max":%d}}`, math.MaxInt64)
if sortedString(targetString) != sortedString(jsonString) {
t.Fatalf("`%s` != `%s`", targetString, jsonString)
}

compareFieldOptions(t, field.Options(), FieldTypeInt, TimeQuantumNone, CacheTypeDefault, 0, -10, math.MaxInt64)
field = sampleIndex.Field("int-field3", OptFieldTypeInt())
jsonString = field.options.String()
targetString = fmt.Sprintf(`{"options":{"type":"int","min":%d,"max":%d}}`, math.MinInt64, math.MaxInt64)
Expand Down

0 comments on commit 2ac5f48

Please sign in to comment.