Add SQL either function for simplified IF ELSE clauses
This commit is contained in:
parent
cc85b86ced
commit
3436634586
4 changed files with 118 additions and 0 deletions
|
@ -103,6 +103,7 @@ var funcs = map[string]map[int]interface{}{
|
|||
"binary": {1: nil},
|
||||
"difference": {-1: nil},
|
||||
"distinct": {1: nil},
|
||||
"either": {-1: nil},
|
||||
"get": {2: nil},
|
||||
"if": {3: nil},
|
||||
"intersect": {-1: nil},
|
||||
|
|
51
util/fncs/either.go
Normal file
51
util/fncs/either.go
Normal file
|
@ -0,0 +1,51 @@
|
|||
// Copyright © 2016 Abcum Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package fncs
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
func either(ctx context.Context, args ...interface{}) (interface{}, error) {
|
||||
for _, a := range args {
|
||||
switch v := a.(type) {
|
||||
case nil:
|
||||
continue
|
||||
default:
|
||||
return v, nil
|
||||
case string:
|
||||
if v != "" {
|
||||
return v, nil
|
||||
}
|
||||
case int64:
|
||||
if v != 0 {
|
||||
return v, nil
|
||||
}
|
||||
case float64:
|
||||
if v != 0 {
|
||||
return v, nil
|
||||
}
|
||||
case []interface{}:
|
||||
if len(v) != 0 {
|
||||
return v, nil
|
||||
}
|
||||
case map[string]interface{}:
|
||||
if len(v) != 0 {
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
}
|
64
util/fncs/either_test.go
Normal file
64
util/fncs/either_test.go
Normal file
|
@ -0,0 +1,64 @@
|
|||
// Copyright © 2016 Abcum Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package fncs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
)
|
||||
|
||||
func TestEither(t *testing.T) {
|
||||
|
||||
var res interface{}
|
||||
|
||||
Convey("either() works properly", t, func() {
|
||||
|
||||
res, _ = Run(context.Background(), "either", nil, true)
|
||||
So(res, ShouldResemble, true)
|
||||
|
||||
res, _ = Run(context.Background(), "either", int64(0), "two")
|
||||
So(res, ShouldResemble, "two")
|
||||
res, _ = Run(context.Background(), "either", int64(1), "two")
|
||||
So(res, ShouldResemble, int64(1))
|
||||
res, _ = Run(context.Background(), "either", int64(-1), "two")
|
||||
So(res, ShouldResemble, int64(-1))
|
||||
|
||||
res, _ = Run(context.Background(), "either", float64(0), "two")
|
||||
So(res, ShouldResemble, "two")
|
||||
res, _ = Run(context.Background(), "either", float64(1), "two")
|
||||
So(res, ShouldResemble, float64(1))
|
||||
res, _ = Run(context.Background(), "either", float64(-1), "two")
|
||||
So(res, ShouldResemble, float64(-1))
|
||||
|
||||
res, _ = Run(context.Background(), "either", "", "two")
|
||||
So(res, ShouldResemble, "two")
|
||||
res, _ = Run(context.Background(), "either", "one", "two")
|
||||
So(res, ShouldResemble, "one")
|
||||
|
||||
res, _ = Run(context.Background(), "either", []interface{}{}, []interface{}{"two"})
|
||||
So(res, ShouldResemble, []interface{}{"two"})
|
||||
res, _ = Run(context.Background(), "either", []interface{}{"one"}, []interface{}{"two"})
|
||||
So(res, ShouldResemble, []interface{}{"one"})
|
||||
|
||||
res, _ = Run(context.Background(), "either", map[string]interface{}{}, map[string]interface{}{"two": true})
|
||||
So(res, ShouldResemble, map[string]interface{}{"two": true})
|
||||
res, _ = Run(context.Background(), "either", map[string]interface{}{"one": true}, map[string]interface{}{"two": true})
|
||||
So(res, ShouldResemble, map[string]interface{}{"one": true})
|
||||
|
||||
})
|
||||
|
||||
}
|
|
@ -28,6 +28,8 @@ func Run(ctx context.Context, name string, args ...interface{}) (interface{}, er
|
|||
return difference(ctx, args...)
|
||||
case "distinct":
|
||||
return distinct(ctx, args...)
|
||||
case "either":
|
||||
return either(ctx, args...)
|
||||
case "get":
|
||||
return get(ctx, args...)
|
||||
case "if":
|
||||
|
|
Loading…
Reference in a new issue