mirror of
https://github.com/metabarcoding/obitools4.git
synced 2025-06-29 16:20:46 +00:00
add functionalities to the expression
predicate evaluations
This commit is contained in:
@ -7,6 +7,7 @@ import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"os"
|
||||
"reflect"
|
||||
"sync"
|
||||
)
|
||||
|
||||
@ -203,3 +204,28 @@ func JsonMarshal(i interface{}) ([]byte, error) {
|
||||
err := encoder.Encode(i)
|
||||
return bytes.TrimRight(buffer.Bytes(), "\n"), err
|
||||
}
|
||||
|
||||
func IsAMap(value interface{}) bool {
|
||||
return reflect.TypeOf(value).Kind() == reflect.Map
|
||||
}
|
||||
|
||||
func IsAnArray(value interface{}) bool {
|
||||
return reflect.TypeOf(value).Kind() == reflect.Array
|
||||
}
|
||||
|
||||
func IsASlice(value interface{}) bool {
|
||||
return reflect.TypeOf(value).Kind() == reflect.Slice
|
||||
}
|
||||
|
||||
func HasLength(value interface{}) bool {
|
||||
return IsAMap(value) || IsAnArray(value) || IsASlice(value)
|
||||
}
|
||||
func Length(value interface{}) int {
|
||||
l := 1
|
||||
if HasLength(value) {
|
||||
vc := reflect.ValueOf(value)
|
||||
l = vc.Len()
|
||||
}
|
||||
|
||||
return l
|
||||
}
|
@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
|
||||
"git.metabarcoding.org/lecasofts/go/obitools/pkg/goutils"
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/PaesslerAG/gval"
|
||||
@ -20,7 +21,11 @@ func (predicate1 SequencePredicate) And(predicate2 SequencePredicate) SequencePr
|
||||
return predicate1
|
||||
default:
|
||||
return func(sequence *BioSequence) bool {
|
||||
return predicate1(sequence) && predicate2(sequence)
|
||||
if !predicate1(sequence) {
|
||||
return false
|
||||
}
|
||||
|
||||
return predicate2(sequence)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -33,7 +38,10 @@ func (predicate1 SequencePredicate) Or(predicate2 SequencePredicate) SequencePre
|
||||
return predicate1
|
||||
default:
|
||||
return func(sequence *BioSequence) bool {
|
||||
return predicate1(sequence) || predicate2(sequence)
|
||||
if predicate1(sequence) {
|
||||
return true
|
||||
}
|
||||
return predicate2(sequence)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -195,8 +203,17 @@ func IsIdIn(ids ...string) SequencePredicate {
|
||||
|
||||
func ExpressionPredicat(expression string) SequencePredicate {
|
||||
|
||||
exp, err := gval.Full().NewEvaluable(expression)
|
||||
|
||||
lang := gval.NewLanguage(
|
||||
gval.Full(),
|
||||
gval.Function("len", func(args ...interface{}) (interface{}, error) {
|
||||
length := goutils.Length(args[0])
|
||||
return (float64)(length), nil
|
||||
}),
|
||||
gval.Function("ismap", func(args ...interface{}) (interface{}, error) {
|
||||
ismap := goutils.IsAMap(args[0])
|
||||
return ismap, nil
|
||||
}))
|
||||
exp, err := lang.NewEvaluable(expression)
|
||||
if err != nil {
|
||||
log.Fatalf("Error in the expression : %s", expression)
|
||||
}
|
||||
@ -206,7 +223,7 @@ func ExpressionPredicat(expression string) SequencePredicate {
|
||||
map[string]interface{}{
|
||||
"annot": sequence.Annotations(),
|
||||
"count": sequence.Count(),
|
||||
"length": sequence.Length(),
|
||||
"seqlength": sequence.Length(),
|
||||
"sequence": sequence,
|
||||
},
|
||||
)
|
||||
|
Reference in New Issue
Block a user