Skip to content

Commit

Permalink
stanislavpetrov/MYST-1137-ArgInt64Slice (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
StanislavStefanovPetrov authored Feb 14, 2023
1 parent 481d77f commit 9d03e72
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
39 changes: 39 additions & 0 deletions job.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,45 @@ func (j *Job) ArgInt64(key string) int64 {
return 0
}

// ArgInt64Slice returns j.Args[key] typed to an []int64. If the key is missing or of the wrong type, it sets an argument error
// on the job. This function is meant to be used in the body of a job handling function while extracting arguments,
// followed by a single call to j.ArgError().
func (j *Job) ArgInt64Slice(key string) []int64 {
var values []int64
row, ok := j.Args[key]
if ok {
listSlice, ok := row.([]interface{})
if ok {
for _, v := range listSlice {
rVal := reflect.ValueOf(v)
if isIntKind(rVal) {
values = append(values, rVal.Int())
continue
} else if isUintKind(rVal) {
vUint := rVal.Uint()
if vUint <= math.MaxInt64 {
values = append(values, int64(vUint))
continue
}
} else if isFloatKind(rVal) {
vFloat64 := rVal.Float()
vInt64 := int64(vFloat64)
if vFloat64 == math.Trunc(vFloat64) && vInt64 <= 9007199254740892 && vInt64 >= -9007199254740892 {
values = append(values, vInt64)
continue
}
}
j.argError = typecastError("int64", key, v)
}
} else {
j.argError = typecastError("[]int64", key, row)
}
} else {
j.argError = missingKeyError("[]int64", key)
}
return values
}

// ArgFloat64 returns j.Args[key] typed to a float64. If the key is missing or of the wrong type, it sets an argument error
// on the job. This function is meant to be used in the body of a job handling function while extracting arguments,
// followed by a single call to j.ArgError().
Expand Down
89 changes: 89 additions & 0 deletions job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,95 @@ func TestJobArgumentExtractionBadInt(t *testing.T) {
}
}

func TestJobArgumentExtractionBadSliceInt(t *testing.T) {
// Test slice params
var testBadValues = []struct {
key string
val []interface{}
good bool
count int
}{
{"a", []interface{}{"boo"}, false, 0},
{"b", []interface{}{true}, false, 0},
{"c", []interface{}{1.1}, false, 0},
{"d", []interface{}{19007199254740892.0}, false, 0},
{"e", []interface{}{-19007199254740892.0}, false, 0},
{"f", []interface{}{uint64(math.MaxInt64) + 1}, false, 0},
{"g", []interface{}{1, uint64(math.MaxInt64) + 1}, false, 1},
{"h", []interface{}{1, uint64(math.MaxInt64) + 1, 2.0}, false, 2},

{"z", []interface{}{0}, true, 1},
{"y", []interface{}{9007199254740892}, true, 1},
{"x", []interface{}{9007199254740892.0}, true, 1},
{"w", []interface{}{573839921}, true, 1},
{"v", []interface{}{-573839921}, true, 1},
{"u", []interface{}{uint64(math.MaxInt64)}, true, 1},
{"t", []interface{}{1, 2, 3}, true, 3},
{"s", []interface{}{}, true, 0},
}

j := Job{}

for _, tc := range testBadValues {
j.setArg(tc.key, tc.val)
}

for _, tc := range testBadValues {
r := j.ArgInt64Slice(tc.key)
err := j.ArgError()
if tc.good {
if err != nil {
t.Errorf("Failed test case: %v; err = %v\n", tc, err)
}
} else {
if err == nil {
t.Errorf("Failed test case: %v; but err was nil\n", tc)
}
}
if len(r) != tc.count {
t.Errorf("Failed test case: %v; but r was %v\n", tc, r)
}
j.argError = nil
}

// Test no slice
var testNoSlice = []struct {
key string
val interface{}
}{
{"j", "bool"},
{"k", true},
{"l", 1},
}

for _, tc := range testNoSlice {
j.setArg(tc.key, tc.val)
}

for _, tc := range testNoSlice {
r := j.ArgInt64Slice(tc.key)
err := j.ArgError()
if err == nil {
t.Errorf("Failed test case: %v; err = %v\n", tc, err)
}
if len(r) != 0 {
t.Errorf("Failed test case: %v; but r was %v\n", tc, r)
}
j.argError = nil
}

// Test missing key
r := j.ArgInt64Slice("m")
err := j.ArgError()
if err == nil {
t.Errorf("Failed test case: %v; err = %v\n", "missing params", err)
}
if len(r) != 0 {
t.Errorf("Failed test case: %v; but r was %v\n", "missing params", r)
}
j.argError = nil
}

func TestJobArgumentExtractionBadFloat(t *testing.T) {
var testCases = []struct {
key string
Expand Down

0 comments on commit 9d03e72

Please sign in to comment.