mirror of
https://github.com/MontFerret/ferret.git
synced 2025-01-16 03:21:03 +02:00
add NewPairValueType
This commit is contained in:
parent
ebe9748dd5
commit
ae94075a42
@ -1,8 +1,9 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
"math/rand"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// Type represents runtime type with id for quick type check
|
||||
@ -81,3 +82,36 @@ func ValidateValueTypePairs(pairs ...PairValueType) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// PairValueType is a supporting
|
||||
// structure that used in validateValueTypePairs.
|
||||
type PairValueType struct {
|
||||
Value Value
|
||||
Types []Type
|
||||
}
|
||||
|
||||
// NewPairValueType it's a shortcut for creating a new PairValueType.
|
||||
//
|
||||
// The common pattern of using PairValueType is:
|
||||
// ```
|
||||
// pairs := []core.PairValueType{
|
||||
// core.PairValueType{args[0], []core.Type{types.String}}, // go vet warning
|
||||
// core.PairValueType{Value: args[1], Types: []core.Type{types.Binary}}, // too long
|
||||
// }
|
||||
// ```
|
||||
// With NewPairValueType there is no need to type `[]core.Type{...}` and code becomes
|
||||
// more readable and maintainable.
|
||||
//
|
||||
// That is how the code above looks like with NewPairValueType:
|
||||
// ```
|
||||
// pairs := []core.PairValueType{
|
||||
// core.NewPairValueType(args[0], types.String),
|
||||
// core.NewPairValueType(args[1], types.Binary),
|
||||
// }
|
||||
// ```
|
||||
func NewPairValueType(value Value, types ...Type) PairValueType {
|
||||
return PairValueType{
|
||||
Value: value,
|
||||
Types: types,
|
||||
}
|
||||
}
|
||||
|
@ -42,11 +42,4 @@ type (
|
||||
Setter interface {
|
||||
SetIn(ctx context.Context, path []Value, value Value) error
|
||||
}
|
||||
|
||||
// PairValueType is a supporting
|
||||
// structure that used in validateValueTypePairs.
|
||||
PairValueType struct {
|
||||
Value Value
|
||||
Types []Type
|
||||
}
|
||||
)
|
||||
|
@ -9,10 +9,6 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
sliceDateTime = []core.Type{types.DateTime}
|
||||
sliceIntType = []core.Type{types.Int}
|
||||
sliceStringType = []core.Type{types.String}
|
||||
|
||||
emptyDateTime values.DateTime
|
||||
emptyInt values.Int
|
||||
emptyString values.String
|
||||
@ -85,9 +81,9 @@ func getArgs(args []core.Value) (values.DateTime, values.Int, values.String, err
|
||||
}
|
||||
|
||||
err = core.ValidateValueTypePairs(
|
||||
core.PairValueType{Value: args[0], Types: sliceDateTime},
|
||||
core.PairValueType{Value: args[1], Types: sliceIntType},
|
||||
core.PairValueType{Value: args[2], Types: sliceStringType},
|
||||
core.NewPairValueType(args[0], types.DateTime),
|
||||
core.NewPairValueType(args[1], types.Int),
|
||||
core.NewPairValueType(args[2], types.String),
|
||||
)
|
||||
if err != nil {
|
||||
return emptyDateTime, emptyInt, emptyString, err
|
||||
|
@ -22,9 +22,9 @@ func DateCompare(_ context.Context, args ...core.Value) (core.Value, error) {
|
||||
}
|
||||
|
||||
err = core.ValidateValueTypePairs(
|
||||
core.PairValueType{Value: args[0], Types: sliceDateTime},
|
||||
core.PairValueType{Value: args[1], Types: sliceDateTime},
|
||||
core.PairValueType{Value: args[2], Types: sliceStringType},
|
||||
core.NewPairValueType(args[0], types.DateTime),
|
||||
core.NewPairValueType(args[1], types.DateTime),
|
||||
core.NewPairValueType(args[2], types.String),
|
||||
)
|
||||
if err != nil {
|
||||
return values.None, err
|
||||
|
@ -21,9 +21,9 @@ func DateDiff(_ context.Context, args ...core.Value) (core.Value, error) {
|
||||
}
|
||||
|
||||
err = core.ValidateValueTypePairs(
|
||||
core.PairValueType{Value: args[0], Types: sliceDateTime},
|
||||
core.PairValueType{Value: args[1], Types: sliceDateTime},
|
||||
core.PairValueType{Value: args[2], Types: sliceStringType},
|
||||
core.NewPairValueType(args[0], types.DateTime),
|
||||
core.NewPairValueType(args[1], types.DateTime),
|
||||
core.NewPairValueType(args[2], types.String),
|
||||
)
|
||||
if err != nil {
|
||||
return values.None, err
|
||||
|
@ -62,8 +62,8 @@ func validateRequiredWriteArgs(args []core.Value) error {
|
||||
}
|
||||
|
||||
pairs := []core.PairValueType{
|
||||
core.PairValueType{Value: args[0], Types: []core.Type{types.String}},
|
||||
core.PairValueType{Value: args[1], Types: []core.Type{types.Binary}},
|
||||
core.NewPairValueType(args[0], types.String),
|
||||
core.NewPairValueType(args[1], types.Binary),
|
||||
}
|
||||
|
||||
err = core.ValidateValueTypePairs(pairs...)
|
||||
|
Loading…
Reference in New Issue
Block a user