From ae94075a42368dda092ae4cc7bfb85c8005a70cd Mon Sep 17 00:00:00 2001 From: 3timeslazy Date: Mon, 21 Oct 2019 20:11:47 +0300 Subject: [PATCH] add NewPairValueType --- pkg/runtime/core/type.go | 36 ++++++++++++++++++++++++++++- pkg/runtime/core/value.go | 7 ------ pkg/stdlib/datetime/add_subtract.go | 10 +++----- pkg/stdlib/datetime/compare.go | 6 ++--- pkg/stdlib/datetime/diff.go | 6 ++--- pkg/stdlib/io/fs/write.go | 4 ++-- 6 files changed, 46 insertions(+), 23 deletions(-) diff --git a/pkg/runtime/core/type.go b/pkg/runtime/core/type.go index ea325934..0f1191cf 100644 --- a/pkg/runtime/core/type.go +++ b/pkg/runtime/core/type.go @@ -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, + } +} diff --git a/pkg/runtime/core/value.go b/pkg/runtime/core/value.go index 3c4853c6..8933f233 100644 --- a/pkg/runtime/core/value.go +++ b/pkg/runtime/core/value.go @@ -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 - } ) diff --git a/pkg/stdlib/datetime/add_subtract.go b/pkg/stdlib/datetime/add_subtract.go index 92abda1c..d211cf70 100644 --- a/pkg/stdlib/datetime/add_subtract.go +++ b/pkg/stdlib/datetime/add_subtract.go @@ -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 diff --git a/pkg/stdlib/datetime/compare.go b/pkg/stdlib/datetime/compare.go index 28fd4431..b3449fa7 100644 --- a/pkg/stdlib/datetime/compare.go +++ b/pkg/stdlib/datetime/compare.go @@ -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 diff --git a/pkg/stdlib/datetime/diff.go b/pkg/stdlib/datetime/diff.go index ee41a0cb..02152e1f 100644 --- a/pkg/stdlib/datetime/diff.go +++ b/pkg/stdlib/datetime/diff.go @@ -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 diff --git a/pkg/stdlib/io/fs/write.go b/pkg/stdlib/io/fs/write.go index 9fbde2c0..e8366045 100644 --- a/pkg/stdlib/io/fs/write.go +++ b/pkg/stdlib/io/fs/write.go @@ -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...)