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"
|
"encoding/json"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"reflect"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -197,9 +198,34 @@ func AtomicCounter(initial ...int) func() int {
|
|||||||
// characters, json.Marshal should not be used. Playground of Go breaking a
|
// characters, json.Marshal should not be used. Playground of Go breaking a
|
||||||
// title: https://play.golang.org/p/o2hiX0c62oN
|
// title: https://play.golang.org/p/o2hiX0c62oN
|
||||||
func JsonMarshal(i interface{}) ([]byte, error) {
|
func JsonMarshal(i interface{}) ([]byte, error) {
|
||||||
buffer := &bytes.Buffer{}
|
buffer := &bytes.Buffer{}
|
||||||
encoder := json.NewEncoder(buffer)
|
encoder := json.NewEncoder(buffer)
|
||||||
encoder.SetEscapeHTML(false)
|
encoder.SetEscapeHTML(false)
|
||||||
err := encoder.Encode(i)
|
err := encoder.Encode(i)
|
||||||
return bytes.TrimRight(buffer.Bytes(), "\n"), err
|
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"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
|
"git.metabarcoding.org/lecasofts/go/obitools/pkg/goutils"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"github.com/PaesslerAG/gval"
|
"github.com/PaesslerAG/gval"
|
||||||
@ -20,7 +21,11 @@ func (predicate1 SequencePredicate) And(predicate2 SequencePredicate) SequencePr
|
|||||||
return predicate1
|
return predicate1
|
||||||
default:
|
default:
|
||||||
return func(sequence *BioSequence) bool {
|
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
|
return predicate1
|
||||||
default:
|
default:
|
||||||
return func(sequence *BioSequence) bool {
|
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 {
|
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 {
|
if err != nil {
|
||||||
log.Fatalf("Error in the expression : %s", expression)
|
log.Fatalf("Error in the expression : %s", expression)
|
||||||
}
|
}
|
||||||
@ -204,10 +221,10 @@ func ExpressionPredicat(expression string) SequencePredicate {
|
|||||||
f := func(sequence *BioSequence) bool {
|
f := func(sequence *BioSequence) bool {
|
||||||
value, err := exp.EvalBool(context.Background(),
|
value, err := exp.EvalBool(context.Background(),
|
||||||
map[string]interface{}{
|
map[string]interface{}{
|
||||||
"annot": sequence.Annotations(),
|
"annot": sequence.Annotations(),
|
||||||
"count": sequence.Count(),
|
"count": sequence.Count(),
|
||||||
"length": sequence.Length(),
|
"seqlength": sequence.Length(),
|
||||||
"sequence": sequence,
|
"sequence": sequence,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user