diff --git a/api/cel-service.proto b/api/cel-service.proto index bcbe2a7..76da84b 100644 --- a/api/cel-service.proto +++ b/api/cel-service.proto @@ -1,7 +1,6 @@ syntax = "proto3"; package protofiles; -import "google/protobuf/any.proto"; import "google/protobuf/struct.proto"; option go_package = "./pkg/protofiles"; @@ -9,21 +8,7 @@ option go_package = "./pkg/protofiles"; message CelRequest { google.protobuf.Struct Context = 1; string Expression = 2; -} - -message ContextValue { - enum Type { - int23 = 0; - int64 = 1; - double = 2; - float = 3; - bool = 4; - string = 5; - bytes = 6; - map = 7; - } - Type type = 1; - google.protobuf.Any value = 2; + string identifier = 3; } message CelResponse { diff --git a/internal/celproc/celproc.go b/internal/celproc/celproc.go index 9457eb4..4e21974 100644 --- a/internal/celproc/celproc.go +++ b/internal/celproc/celproc.go @@ -15,6 +15,15 @@ import ( exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1" ) +var lrucache LRUList + +func init() { + lrucache = LRUList{ + MaxCount: int(10000), + } + lrucache.Init() +} + func GRPCProcCel(celRequest *protofiles.CelRequest) (*protofiles.CelResponse, error) { context := convertJson2Map(celRequest.Context.AsMap()) celModel := model.CelModel{ @@ -61,45 +70,64 @@ func convertJson2Map(src map[string]interface{}) (dst map[string]interface{}) { func ProcCel(celModel model.CelModel) (model.CelResult, error) { context := convertJson2Map(celModel.Context) - var declList = make([]*exprpb.Decl, len(context)) - x := 0 - for k := range context { - declList[x] = decls.NewVar(k, decls.Dyn) - x++ - } - env, err := cel.NewEnv( - cel.Declarations( - declList..., - ), - ) - if err != nil { - log.Logger.Errorf("env declaration error: %s", err) - } - ast, issues := env.Compile(celModel.Expression) - if issues != nil && issues.Err() != nil { - log.Logger.Errorf("type-check error: %v", issues.Err()) - return model.CelResult{ - Error: fmt.Sprintf("%v", issues.Err()), - Message: issues.Err().Error(), - }, issues.Err() + ok := false + var prg cel.Program + id := celModel.Identifier + if id != "" { + var entry LRUEntry + entry, ok = lrucache.Get(id) + if ok { + prg = entry.Program + } } - prg, err := env.Program(ast) - if err != nil { - log.Logger.Errorf("program construction error: %v", err) - return model.CelResult{ - Error: fmt.Sprintf("%v", err), - Message: fmt.Sprintf("program construction error: %s", err.Error()), - }, err + if !ok { + var declList = make([]*exprpb.Decl, len(context)) + x := 0 + for k := range context { + declList[x] = decls.NewVar(k, decls.Dyn) + x++ + } + env, err := cel.NewEnv( + cel.Declarations( + declList..., + ), + ) + if err != nil { + log.Logger.Errorf("env declaration error: %s", err) + } + ast, issues := env.Compile(celModel.Expression) + if issues != nil && issues.Err() != nil { + log.Logger.Errorf("type-check error: %v", issues.Err()) + return model.CelResult{ + Error: fmt.Sprintf("%v", issues.Err()), + Message: issues.Err().Error(), + }, issues.Err() + } + prg, err = env.Program(ast) + if err != nil { + log.Logger.Errorf("program construction error: %v", err) + return model.CelResult{ + Error: fmt.Sprintf("%v", err), + Message: fmt.Sprintf("program construction error: %s", err.Error()), + }, err + } + if id != "" { + lrucache.Add(LRUEntry{ + ID: id, + Program: prg, + }) + go lrucache.HandleContrains() + } } out, details, err := prg.Eval(context) - fmt.Printf("result: %v\ndetails: %v\nerror: %v\n", out, details, err) + //fmt.Printf("result: %v\ndetails: %v\nerror: %v\n", out, details, err) if err != nil { log.Logger.Errorf("program evaluation error: %v", err) return model.CelResult{ Error: fmt.Sprintf("%v", err), - Message: fmt.Sprintf("program evaluation error: %s", err.Error()), + Message: fmt.Sprintf("program evaluation error: %s\r\ndetails: %s", err.Error(), details), }, err } switch v := out.(type) { diff --git a/internal/celproc/celproc_test.go b/internal/celproc/celproc_test.go index 33672d8..f6724b4 100644 --- a/internal/celproc/celproc_test.go +++ b/internal/celproc/celproc_test.go @@ -5,6 +5,7 @@ import ( "encoding/json" "io/ioutil" "testing" + "time" "google.golang.org/protobuf/types/known/structpb" "gopkg.in/yaml.v3" @@ -44,6 +45,47 @@ func TestJson(t *testing.T) { } } +func BenchmarkJsonManyWithoutCache(t *testing.B) { + ast := assert.New(t) + + celModels := readJsonB("../../test/data/data1.json", t) + stt := time.Now() + for i := 0; i < 10000; i++ { + for _, cm := range celModels { + cm.Request.Context = convertJson2Map(cm.Request.Context) + cm.Request.Identifier = "" //fmt.Sprintf("%d_%d", i, x) + result, err := ProcCel(cm.Request) + ast.Nil(err) + ast.NotNil(result) + + ast.Equal(cm.Result, result.Result) + } + } + ste := time.Now() + + t.Logf("execution: %d", ste.Sub(stt).Milliseconds()) +} + +func BenchmarkJsonManyWithCache(t *testing.B) { + ast := assert.New(t) + + celModels := readJsonB("../../test/data/data1.json", t) + stt := time.Now() + for i := 0; i < 10000; i++ { + for _, cm := range celModels { + cm.Request.Context = convertJson2Map(cm.Request.Context) + result, err := ProcCel(cm.Request) + ast.Nil(err) + ast.NotNil(result) + + ast.Equal(cm.Result, result.Result) + } + } + ste := time.Now() + + t.Logf("execution: %d", ste.Sub(stt).Milliseconds()) +} + func TestGRPCJson(t *testing.T) { ast := assert.New(t) @@ -88,3 +130,16 @@ func readJson(filename string, t *testing.T) []model.TestCelModel { ast.Nil(err) return celModels } + +func readJsonB(filename string, t *testing.B) []model.TestCelModel { + ast := assert.New(t) + ya, err := ioutil.ReadFile(filename) + ast.Nil(err) + var celModels []model.TestCelModel + decoder := json.NewDecoder(bytes.NewReader(ya)) + decoder.UseNumber() + err = decoder.Decode(&celModels) + //err = json.Unmarshal(ya, &celModels) + ast.Nil(err) + return celModels +} diff --git a/internal/celproc/lrulist.go b/internal/celproc/lrulist.go new file mode 100644 index 0000000..616d40e --- /dev/null +++ b/internal/celproc/lrulist.go @@ -0,0 +1,161 @@ +package celproc + +import ( + "sort" + "sync" + "time" + + "github.com/google/cel-go/cel" +) + +type LRUEntry struct { + LastAccess time.Time `json:"lastAccess"` + ID string `json:"id"` + Program cel.Program `json:"description"` +} + +type LRUList struct { + MaxCount int + entries []LRUEntry + dmu sync.Mutex + cstLock sync.Mutex +} + +func (l *LRUList) Init() { + l.entries = make([]LRUEntry, 0) +} + +func (l *LRUList) Size() int { + return len(l.entries) +} + +func (l *LRUList) Add(e LRUEntry) bool { + l.dmu.Lock() + defer l.dmu.Unlock() + e.LastAccess = time.Now() + l.entries = l.insertSorted(l.entries, e) + return true +} + +func (l *LRUList) Update(e LRUEntry) { + id := e.ID + l.dmu.Lock() + defer l.dmu.Unlock() + i := sort.Search(len(l.entries), func(i int) bool { return l.entries[i].ID >= id }) + if i < len(l.entries) && l.entries[i].ID == id { + e.LastAccess = time.Now() + l.entries[i] = e + } +} + +func (l *LRUList) UpdateAccess(id string) { + l.dmu.Lock() + defer l.dmu.Unlock() + i := sort.Search(len(l.entries), func(i int) bool { return l.entries[i].ID >= id }) + if i < len(l.entries) && l.entries[i].ID == id { + l.entries[i].LastAccess = time.Now() + } +} + +func (l *LRUList) GetFullIDList() []string { + l.dmu.Lock() + defer l.dmu.Unlock() + ids := make([]string, len(l.entries)) + for x, e := range l.entries { + ids[x] = e.ID + } + return ids +} + +func (l *LRUList) HandleContrains() { + l.cstLock.Lock() + defer l.cstLock.Unlock() + for { + id := l.getSingleContrained() + if id != "" { + l.Delete(id) + } else { + break + } + } +} + +func (l *LRUList) getSingleContrained() string { + var id string + l.dmu.Lock() + defer l.dmu.Unlock() + if len(l.entries) > int(l.MaxCount) { + // remove oldest entry from cache + oldest := l.getOldest() + id = l.entries[oldest].ID + } + return id +} + +func (l *LRUList) Has(id string) bool { + l.dmu.Lock() + defer l.dmu.Unlock() + i := sort.Search(len(l.entries), func(i int) bool { return l.entries[i].ID >= id }) + if i < len(l.entries) && l.entries[i].ID == id { + return true + } + return false +} + +func (l *LRUList) Get(id string) (LRUEntry, bool) { + l.dmu.Lock() + defer l.dmu.Unlock() + i := sort.Search(len(l.entries), func(i int) bool { return l.entries[i].ID >= id }) + if i < len(l.entries) && l.entries[i].ID == id { + l.entries[i].LastAccess = time.Now() + return l.entries[i], true + } + return LRUEntry{}, false +} + +func (l *LRUList) Delete(id string) string { + l.dmu.Lock() + defer l.dmu.Unlock() + i := sort.Search(len(l.entries), func(i int) bool { return l.entries[i].ID >= id }) + if i < len(l.entries) && l.entries[i].ID == id { + ret := make([]LRUEntry, 0) + ret = append(ret, l.entries[:i]...) + l.entries = append(ret, l.entries[i+1:]...) + return id + } + return "" +} + +func (l *LRUList) getOldest() int { + oldest := 0 + for x, e := range l.entries { + if e.LastAccess.Before(l.entries[oldest].LastAccess) { + oldest = x + } + } + return oldest +} + +func (l *LRUList) insertSorted(data []LRUEntry, v LRUEntry) []LRUEntry { + i := sort.Search(len(data), func(i int) bool { return data[i].ID >= v.ID }) + return l.insertEntryAt(data, i, v) +} + +func (l *LRUList) insertEntryAt(data []LRUEntry, i int, v LRUEntry) []LRUEntry { + if i == len(data) { + // Insert at end is the easy case. + return append(data, v) + } + + // Make space for the inserted element by shifting + // values at the insertion index up one index. The call + // to append does not allocate memory when cap(data) is + // greater ​than len(data). + data = append(data[:i+1], data[i:]...) + + // Insert the new element. + data[i] = v + + // Return the updated slice. + return data +} diff --git a/pkg/model/celmodel.go b/pkg/model/celmodel.go index 6bd0459..7ed79cb 100644 --- a/pkg/model/celmodel.go +++ b/pkg/model/celmodel.go @@ -3,6 +3,7 @@ package model type CelModel struct { Context map[string]interface{} `yaml:"context" json:"context"` Expression string `yaml:"expression" json:"expression"` + Identifier string `yaml:"identifier" json:"identifier"` } type CelResult struct { diff --git a/pkg/protofiles/cel-service.pb.go b/pkg/protofiles/cel-service.pb.go index ee92c1e..3cb28a2 100644 --- a/pkg/protofiles/cel-service.pb.go +++ b/pkg/protofiles/cel-service.pb.go @@ -1,16 +1,15 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.27.1 -// protoc v3.13.0 +// protoc v3.19.2 // source: api/cel-service.proto package protofiles import ( - any "github.com/golang/protobuf/ptypes/any" - _struct "github.com/golang/protobuf/ptypes/struct" protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" + structpb "google.golang.org/protobuf/types/known/structpb" reflect "reflect" sync "sync" ) @@ -22,77 +21,14 @@ const ( _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) ) -type ContextValue_Type int32 - -const ( - ContextValue_int23 ContextValue_Type = 0 - ContextValue_int64 ContextValue_Type = 1 - ContextValue_double ContextValue_Type = 2 - ContextValue_float ContextValue_Type = 3 - ContextValue_bool ContextValue_Type = 4 - ContextValue_string ContextValue_Type = 5 - ContextValue_bytes ContextValue_Type = 6 - ContextValue_map ContextValue_Type = 7 -) - -// Enum value maps for ContextValue_Type. -var ( - ContextValue_Type_name = map[int32]string{ - 0: "int23", - 1: "int64", - 2: "double", - 3: "float", - 4: "bool", - 5: "string", - 6: "bytes", - 7: "map", - } - ContextValue_Type_value = map[string]int32{ - "int23": 0, - "int64": 1, - "double": 2, - "float": 3, - "bool": 4, - "string": 5, - "bytes": 6, - "map": 7, - } -) - -func (x ContextValue_Type) Enum() *ContextValue_Type { - p := new(ContextValue_Type) - *p = x - return p -} - -func (x ContextValue_Type) String() string { - return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) -} - -func (ContextValue_Type) Descriptor() protoreflect.EnumDescriptor { - return file_api_cel_service_proto_enumTypes[0].Descriptor() -} - -func (ContextValue_Type) Type() protoreflect.EnumType { - return &file_api_cel_service_proto_enumTypes[0] -} - -func (x ContextValue_Type) Number() protoreflect.EnumNumber { - return protoreflect.EnumNumber(x) -} - -// Deprecated: Use ContextValue_Type.Descriptor instead. -func (ContextValue_Type) EnumDescriptor() ([]byte, []int) { - return file_api_cel_service_proto_rawDescGZIP(), []int{1, 0} -} - type CelRequest struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Context *_struct.Struct `protobuf:"bytes,1,opt,name=Context,proto3" json:"Context,omitempty"` - Expression string `protobuf:"bytes,2,opt,name=Expression,proto3" json:"Expression,omitempty"` + Context *structpb.Struct `protobuf:"bytes,1,opt,name=Context,proto3" json:"Context,omitempty"` + Expression string `protobuf:"bytes,2,opt,name=Expression,proto3" json:"Expression,omitempty"` + Identifier string `protobuf:"bytes,3,opt,name=identifier,proto3" json:"identifier,omitempty"` } func (x *CelRequest) Reset() { @@ -127,7 +63,7 @@ func (*CelRequest) Descriptor() ([]byte, []int) { return file_api_cel_service_proto_rawDescGZIP(), []int{0} } -func (x *CelRequest) GetContext() *_struct.Struct { +func (x *CelRequest) GetContext() *structpb.Struct { if x != nil { return x.Context } @@ -141,59 +77,11 @@ func (x *CelRequest) GetExpression() string { return "" } -type ContextValue struct { - state protoimpl.MessageState - sizeCache protoimpl.SizeCache - unknownFields protoimpl.UnknownFields - - Type ContextValue_Type `protobuf:"varint,1,opt,name=type,proto3,enum=protofiles.ContextValue_Type" json:"type,omitempty"` - Value *any.Any `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty"` -} - -func (x *ContextValue) Reset() { - *x = ContextValue{} - if protoimpl.UnsafeEnabled { - mi := &file_api_cel_service_proto_msgTypes[1] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) - } -} - -func (x *ContextValue) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*ContextValue) ProtoMessage() {} - -func (x *ContextValue) ProtoReflect() protoreflect.Message { - mi := &file_api_cel_service_proto_msgTypes[1] - if protoimpl.UnsafeEnabled && x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use ContextValue.ProtoReflect.Descriptor instead. -func (*ContextValue) Descriptor() ([]byte, []int) { - return file_api_cel_service_proto_rawDescGZIP(), []int{1} -} - -func (x *ContextValue) GetType() ContextValue_Type { +func (x *CelRequest) GetIdentifier() string { if x != nil { - return x.Type + return x.Identifier } - return ContextValue_int23 -} - -func (x *ContextValue) GetValue() *any.Any { - if x != nil { - return x.Value - } - return nil + return "" } type CelResponse struct { @@ -209,7 +97,7 @@ type CelResponse struct { func (x *CelResponse) Reset() { *x = CelResponse{} if protoimpl.UnsafeEnabled { - mi := &file_api_cel_service_proto_msgTypes[2] + mi := &file_api_cel_service_proto_msgTypes[1] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -222,7 +110,7 @@ func (x *CelResponse) String() string { func (*CelResponse) ProtoMessage() {} func (x *CelResponse) ProtoReflect() protoreflect.Message { - mi := &file_api_cel_service_proto_msgTypes[2] + mi := &file_api_cel_service_proto_msgTypes[1] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -235,7 +123,7 @@ func (x *CelResponse) ProtoReflect() protoreflect.Message { // Deprecated: Use CelResponse.ProtoReflect.Descriptor instead. func (*CelResponse) Descriptor() ([]byte, []int) { - return file_api_cel_service_proto_rawDescGZIP(), []int{2} + return file_api_cel_service_proto_rawDescGZIP(), []int{1} } func (x *CelResponse) GetError() string { @@ -264,41 +152,29 @@ var File_api_cel_service_proto protoreflect.FileDescriptor var file_api_cel_service_proto_rawDesc = []byte{ 0x0a, 0x15, 0x61, 0x70, 0x69, 0x2f, 0x63, 0x65, 0x6c, 0x2d, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x0a, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x66, 0x69, - 0x6c, 0x65, 0x73, 0x1a, 0x19, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, - 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x61, 0x6e, 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x1c, - 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, - 0x73, 0x74, 0x72, 0x75, 0x63, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x5f, 0x0a, 0x0a, - 0x43, 0x65, 0x6c, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x31, 0x0a, 0x07, 0x43, 0x6f, - 0x6e, 0x74, 0x65, 0x78, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x17, 0x2e, 0x67, 0x6f, - 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x53, 0x74, - 0x72, 0x75, 0x63, 0x74, 0x52, 0x07, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x78, 0x74, 0x12, 0x1e, 0x0a, - 0x0a, 0x45, 0x78, 0x70, 0x72, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x18, 0x02, 0x20, 0x01, 0x28, - 0x09, 0x52, 0x0a, 0x45, 0x78, 0x70, 0x72, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x22, 0xcc, 0x01, - 0x0a, 0x0c, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x78, 0x74, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x31, - 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x1d, 0x2e, 0x70, - 0x72, 0x6f, 0x74, 0x6f, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x2e, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x78, - 0x74, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x2e, 0x54, 0x79, 0x70, 0x65, 0x52, 0x04, 0x74, 0x79, 0x70, - 0x65, 0x12, 0x2a, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, - 0x32, 0x14, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, - 0x75, 0x66, 0x2e, 0x41, 0x6e, 0x79, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x22, 0x5d, 0x0a, - 0x04, 0x54, 0x79, 0x70, 0x65, 0x12, 0x09, 0x0a, 0x05, 0x69, 0x6e, 0x74, 0x32, 0x33, 0x10, 0x00, - 0x12, 0x09, 0x0a, 0x05, 0x69, 0x6e, 0x74, 0x36, 0x34, 0x10, 0x01, 0x12, 0x0a, 0x0a, 0x06, 0x64, - 0x6f, 0x75, 0x62, 0x6c, 0x65, 0x10, 0x02, 0x12, 0x09, 0x0a, 0x05, 0x66, 0x6c, 0x6f, 0x61, 0x74, - 0x10, 0x03, 0x12, 0x08, 0x0a, 0x04, 0x62, 0x6f, 0x6f, 0x6c, 0x10, 0x04, 0x12, 0x0a, 0x0a, 0x06, - 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x10, 0x05, 0x12, 0x09, 0x0a, 0x05, 0x62, 0x79, 0x74, 0x65, - 0x73, 0x10, 0x06, 0x12, 0x07, 0x0a, 0x03, 0x6d, 0x61, 0x70, 0x10, 0x07, 0x22, 0x55, 0x0a, 0x0b, - 0x43, 0x65, 0x6c, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x45, - 0x72, 0x72, 0x6f, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x45, 0x72, 0x72, 0x6f, - 0x72, 0x12, 0x18, 0x0a, 0x07, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x02, 0x20, 0x01, - 0x28, 0x09, 0x52, 0x07, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x52, - 0x65, 0x73, 0x75, 0x6c, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x08, 0x52, 0x06, 0x52, 0x65, 0x73, - 0x75, 0x6c, 0x74, 0x32, 0x4a, 0x0a, 0x0b, 0x45, 0x76, 0x61, 0x6c, 0x53, 0x65, 0x72, 0x76, 0x69, - 0x63, 0x65, 0x12, 0x3b, 0x0a, 0x08, 0x45, 0x76, 0x61, 0x6c, 0x75, 0x61, 0x74, 0x65, 0x12, 0x16, - 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x2e, 0x43, 0x65, 0x6c, 0x52, - 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x17, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x66, 0x69, - 0x6c, 0x65, 0x73, 0x2e, 0x43, 0x65, 0x6c, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, - 0x12, 0x5a, 0x10, 0x2e, 0x2f, 0x70, 0x6b, 0x67, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x66, 0x69, - 0x6c, 0x65, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x6c, 0x65, 0x73, 0x1a, 0x1c, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x73, 0x74, 0x72, 0x75, 0x63, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x22, 0x7f, 0x0a, 0x0a, 0x43, 0x65, 0x6c, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, + 0x31, 0x0a, 0x07, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x78, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, + 0x32, 0x17, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, + 0x75, 0x66, 0x2e, 0x53, 0x74, 0x72, 0x75, 0x63, 0x74, 0x52, 0x07, 0x43, 0x6f, 0x6e, 0x74, 0x65, + 0x78, 0x74, 0x12, 0x1e, 0x0a, 0x0a, 0x45, 0x78, 0x70, 0x72, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, + 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0a, 0x45, 0x78, 0x70, 0x72, 0x65, 0x73, 0x73, 0x69, + 0x6f, 0x6e, 0x12, 0x1e, 0x0a, 0x0a, 0x69, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x66, 0x69, 0x65, 0x72, + 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0a, 0x69, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x66, 0x69, + 0x65, 0x72, 0x22, 0x55, 0x0a, 0x0b, 0x43, 0x65, 0x6c, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, + 0x65, 0x12, 0x14, 0x0a, 0x05, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, + 0x52, 0x05, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x12, 0x18, 0x0a, 0x07, 0x4d, 0x65, 0x73, 0x73, 0x61, + 0x67, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, + 0x65, 0x12, 0x16, 0x0a, 0x06, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, + 0x08, 0x52, 0x06, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x32, 0x4a, 0x0a, 0x0b, 0x45, 0x76, 0x61, + 0x6c, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x3b, 0x0a, 0x08, 0x45, 0x76, 0x61, 0x6c, + 0x75, 0x61, 0x74, 0x65, 0x12, 0x16, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x66, 0x69, 0x6c, 0x65, + 0x73, 0x2e, 0x43, 0x65, 0x6c, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x17, 0x2e, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x2e, 0x43, 0x65, 0x6c, 0x52, 0x65, 0x73, + 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x12, 0x5a, 0x10, 0x2e, 0x2f, 0x70, 0x6b, 0x67, 0x2f, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x33, } var ( @@ -313,27 +189,21 @@ func file_api_cel_service_proto_rawDescGZIP() []byte { return file_api_cel_service_proto_rawDescData } -var file_api_cel_service_proto_enumTypes = make([]protoimpl.EnumInfo, 1) -var file_api_cel_service_proto_msgTypes = make([]protoimpl.MessageInfo, 3) +var file_api_cel_service_proto_msgTypes = make([]protoimpl.MessageInfo, 2) var file_api_cel_service_proto_goTypes = []interface{}{ - (ContextValue_Type)(0), // 0: protofiles.ContextValue.Type - (*CelRequest)(nil), // 1: protofiles.CelRequest - (*ContextValue)(nil), // 2: protofiles.ContextValue - (*CelResponse)(nil), // 3: protofiles.CelResponse - (*_struct.Struct)(nil), // 4: google.protobuf.Struct - (*any.Any)(nil), // 5: google.protobuf.Any + (*CelRequest)(nil), // 0: protofiles.CelRequest + (*CelResponse)(nil), // 1: protofiles.CelResponse + (*structpb.Struct)(nil), // 2: google.protobuf.Struct } var file_api_cel_service_proto_depIdxs = []int32{ - 4, // 0: protofiles.CelRequest.Context:type_name -> google.protobuf.Struct - 0, // 1: protofiles.ContextValue.type:type_name -> protofiles.ContextValue.Type - 5, // 2: protofiles.ContextValue.value:type_name -> google.protobuf.Any - 1, // 3: protofiles.EvalService.Evaluate:input_type -> protofiles.CelRequest - 3, // 4: protofiles.EvalService.Evaluate:output_type -> protofiles.CelResponse - 4, // [4:5] is the sub-list for method output_type - 3, // [3:4] is the sub-list for method input_type - 3, // [3:3] is the sub-list for extension type_name - 3, // [3:3] is the sub-list for extension extendee - 0, // [0:3] is the sub-list for field type_name + 2, // 0: protofiles.CelRequest.Context:type_name -> google.protobuf.Struct + 0, // 1: protofiles.EvalService.Evaluate:input_type -> protofiles.CelRequest + 1, // 2: protofiles.EvalService.Evaluate:output_type -> protofiles.CelResponse + 2, // [2:3] is the sub-list for method output_type + 1, // [1:2] is the sub-list for method input_type + 1, // [1:1] is the sub-list for extension type_name + 1, // [1:1] is the sub-list for extension extendee + 0, // [0:1] is the sub-list for field type_name } func init() { file_api_cel_service_proto_init() } @@ -355,18 +225,6 @@ func file_api_cel_service_proto_init() { } } file_api_cel_service_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*ContextValue); i { - case 0: - return &v.state - case 1: - return &v.sizeCache - case 2: - return &v.unknownFields - default: - return nil - } - } - file_api_cel_service_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*CelResponse); i { case 0: return &v.state @@ -384,14 +242,13 @@ func file_api_cel_service_proto_init() { File: protoimpl.DescBuilder{ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_api_cel_service_proto_rawDesc, - NumEnums: 1, - NumMessages: 3, + NumEnums: 0, + NumMessages: 2, NumExtensions: 0, NumServices: 1, }, GoTypes: file_api_cel_service_proto_goTypes, DependencyIndexes: file_api_cel_service_proto_depIdxs, - EnumInfos: file_api_cel_service_proto_enumTypes, MessageInfos: file_api_cel_service_proto_msgTypes, }.Build() File_api_cel_service_proto = out.File diff --git a/pkg/protofiles/cel-service_grpc.pb.go b/pkg/protofiles/cel-service_grpc.pb.go index 8174280..c9680c9 100644 --- a/pkg/protofiles/cel-service_grpc.pb.go +++ b/pkg/protofiles/cel-service_grpc.pb.go @@ -1,4 +1,8 @@ // Code generated by protoc-gen-go-grpc. DO NOT EDIT. +// versions: +// - protoc-gen-go-grpc v1.2.0 +// - protoc v3.19.2 +// source: api/cel-service.proto package protofiles diff --git a/test/data/data1.json b/test/data/data1.json index 74c5fdb..8184c21 100644 --- a/test/data/data1.json +++ b/test/data/data1.json @@ -5,7 +5,8 @@ "value": 1 } }, - "expression": "data.value == 1" + "expression": "data.value == 1", + "identifier": "1" }, "result": true }, { @@ -23,7 +24,8 @@ "name": "klaas" } }, - "expression": "data.v1.value == 1 && data.float == 1.0 && data.bool == true" + "expression": "data.v1.value == 1 && data.float == 1.0 && data.bool == true", + "identifier": "2" }, "result": true }, { @@ -33,7 +35,8 @@ "name": "klaas" } }, - "expression": "has(user.name) && user.name == \"klaas\"" + "expression": "has(user.name) && user.name == \"klaas\"", + "identifier": "3" }, "result": true } diff --git a/test/data/data1.yaml b/test/data/data1.yaml index c434454..f31999e 100644 --- a/test/data/data1.yaml +++ b/test/data/data1.yaml @@ -3,6 +3,7 @@ data: value: 1 expression: "data.value == 1" + identifier: "1" result: true - request: context: @@ -14,4 +15,5 @@ user: name: klaas expression: "data.v1.value == 1 && data.float == 1.0 && user.name == \"klaas\"" + identifier: "2" result: true