Skip to content

Commit a02acac

Browse files
committed
feat: simplify condition shape
1 parent 292c94b commit a02acac

File tree

7 files changed

+87
-324
lines changed

7 files changed

+87
-324
lines changed

apps/api/api/openapi.go

Lines changed: 21 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ func patchExpression(doc *openapi3.T) {
7575
}
7676

7777
func patchConditions(doc *openapi3.T) {
78-
patchConditionBase(doc)
79-
patchConditionParams(doc)
8078
doc.Components.Schemas["ConditionType"] = &openapi3.SchemaRef{
8179
Value: &openapi3.Schema{
8280
Type: "string",
@@ -99,88 +97,47 @@ func patchConditions(doc *openapi3.T) {
9997
},
10098
},
10199
}
102-
doc.Components.Schemas["Condition"] = &openapi3.SchemaRef{
100+
doc.Components.Schemas["DRef"] = &openapi3.SchemaRef{
103101
Value: &openapi3.Schema{
104102
Type: "object",
105103
Properties: map[string]*openapi3.SchemaRef{
106-
"condition": {
107-
Value: &openapi3.Schema{
108-
Properties: map[string]*openapi3.SchemaRef{
109-
"base": {Ref: "#/components/schemas/ConditionBase"},
110-
"type": {Ref: "#/components/schemas/ConditionType"},
111-
"parameter": {Ref: "#/components/schemas/ConditionParameter"},
112-
},
113-
},
114-
},
115-
},
116-
},
117-
}
118-
}
119-
120-
func patchConditionParams(doc *openapi3.T) {
121-
doc.Components.Schemas["ConditionParameterString"] = &openapi3.SchemaRef{
122-
Value: &openapi3.Schema{
123-
Properties: map[string]*openapi3.SchemaRef{
124-
"stringValue": {Value: openapi3.NewStringSchema()},
125-
},
126-
},
127-
}
128-
doc.Components.Schemas["ConditionParameterBoolean"] = &openapi3.SchemaRef{
129-
Value: &openapi3.Schema{
130-
Properties: map[string]*openapi3.SchemaRef{
131-
"boolValue": {Value: openapi3.NewBoolSchema()},
132-
},
133-
},
134-
}
135-
doc.Components.Schemas["ConditionParameterInt"] = &openapi3.SchemaRef{
136-
Value: &openapi3.Schema{
137-
Properties: map[string]*openapi3.SchemaRef{
138-
"intValue": {Value: openapi3.NewIntegerSchema()},
104+
"src": {Value: openapi3.NewStringSchema()},
105+
"dst": {Value: openapi3.NewStringSchema()},
139106
},
140107
},
141108
}
142-
doc.Components.Schemas["ConditionParameterBytes"] = &openapi3.SchemaRef{
109+
doc.Components.Schemas["ConditionRef"] = &openapi3.SchemaRef{
143110
Value: &openapi3.Schema{
111+
Type: "object",
144112
Properties: map[string]*openapi3.SchemaRef{
145-
"byteValue": {Value: openapi3.NewBytesSchema()},
146-
},
147-
},
148-
}
149-
doc.Components.Schemas["ConditionParameter"] = &openapi3.SchemaRef{
150-
Value: &openapi3.Schema{
151-
OneOf: []*openapi3.SchemaRef{
152-
{Ref: "#/components/schemas/ConditionParameterString"},
153-
{Ref: "#/components/schemas/ConditionParameterBoolean"},
154-
{Ref: "#/components/schemas/ConditionParameterInt"},
155-
{Ref: "#/components/schemas/ConditionParameterBytes"},
113+
"ref": {Ref: "#/components/schemas/DRef"},
114+
"type": {Ref: "#/components/schemas/ConditionType"},
115+
"value": {Value: openapi3.NewBytesSchema()},
156116
},
157117
},
158118
}
159-
}
160-
161-
func patchConditionBase(doc *openapi3.T) {
162-
doc.Components.Schemas["ConditionBaseRef"] = &openapi3.SchemaRef{
119+
doc.Components.Schemas["ConditionKey"] = &openapi3.SchemaRef{
163120
Value: &openapi3.Schema{
164121
Type: "object",
165122
Properties: map[string]*openapi3.SchemaRef{
166-
"src": {Value: openapi3.NewStringSchema()},
167-
"dst": {Value: openapi3.NewStringSchema()},
123+
"key": {Value: openapi3.NewStringSchema()},
124+
"type": {Ref: "#/components/schemas/ConditionType"},
125+
"value": {Value: openapi3.NewBytesSchema()},
168126
},
169127
},
170128
}
171-
doc.Components.Schemas["ConditionBaseKey"] = &openapi3.SchemaRef{
129+
doc.Components.Schemas["Condition"] = &openapi3.SchemaRef{
172130
Value: &openapi3.Schema{
173131
Type: "object",
174132
Properties: map[string]*openapi3.SchemaRef{
175-
"key": {Value: openapi3.NewStringSchema()},
176-
},
177-
},
178-
}
179-
doc.Components.Schemas["ConditionBase"] = &openapi3.SchemaRef{
180-
Value: &openapi3.Schema{
181-
OneOf: []*openapi3.SchemaRef{
182-
{Ref: "#/components/schemas/ConditionBaseKey"},
183-
{Ref: "#/components/schemas/ConditionBaseRef"},
133+
"condition": {
134+
Value: &openapi3.Schema{
135+
OneOf: []*openapi3.SchemaRef{
136+
{Ref: "#/components/schemas/ConditionKey"},
137+
{Ref: "#/components/schemas/ConditionRef"},
138+
},
139+
},
140+
},
184141
},
185142
},
186143
}

apps/api/code/module.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,7 @@ func hasPrefix(args ...tengo.Object) (ret tengo.Object, err error) {
9797
case *tengo.String:
9898
prefix = o.Value
9999
default:
100-
return nil, tengo.ErrInvalidArgumentType{
101-
Name: "prefix",
102-
Expected: "string",
103-
Found: o.TypeName(),
104-
}
100+
prefix = o.String()
105101
}
106102

107103
stringifiedVal := fmt.Sprintf("%v", val)
@@ -125,11 +121,7 @@ func hasSuffix(args ...tengo.Object) (ret tengo.Object, err error) {
125121
case *tengo.String:
126122
suffix = o.Value
127123
default:
128-
return nil, tengo.ErrInvalidArgumentType{
129-
Name: "suffix",
130-
Expected: "string",
131-
Found: o.TypeName(),
132-
}
124+
suffix = o.String()
133125
}
134126

135127
stringifiedVal := fmt.Sprintf("%v", val)

apps/api/code/script.go

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@ import (
44
"fmt"
55
"github.com/d5/tengo/v2"
66
"github.com/d5/tengo/v2/stdlib"
7-
"github.com/goccy/go-json"
87
"go.dot.industries/brease/models"
98
"go.dot.industries/brease/pb"
10-
"reflect"
11-
"strconv"
129
)
1310

1411
const (
@@ -29,15 +26,9 @@ func moduleMaps(names ...string) *tengo.ModuleMap {
2926

3027
func conditionToScript(condition *pb.Condition) (code string) {
3128
jsonPath, ref := extractScriptBase(condition)
32-
parameterValue, isParamObj := parameterToScriptValue(condition)
3329
isReference := ref != nil
3430

35-
paramCode := ""
36-
if isParamObj {
37-
paramCode = fmt.Sprintf("%s", parameterValue)
38-
} else {
39-
paramCode = fmt.Sprintf("%v", parameterValue)
40-
}
31+
paramCode := string(condition.Value)
4132
fnWithParamLine := func(fnName string) string {
4233
if isReference {
4334
return fmt.Sprintf(`%s.%s(dref(jsonpath("%s", %s), "%s"), %s)`, tengoModuleName, fnName, ref.Src, objectVariable, ref.Dst, paramCode)
@@ -56,9 +47,9 @@ func conditionToScript(condition *pb.Condition) (code string) {
5647
}
5748
case models.ConditionEquals:
5849
if isReference {
59-
code = fmt.Sprintf(`dref(jsonpath("%s", %s), "%s") == %v`, ref.Src, objectVariable, ref.Dst, parameterValue)
50+
code = fmt.Sprintf(`dref(jsonpath("%s", %s), "%s") == %s`, ref.Src, objectVariable, ref.Dst, paramCode)
6051
} else {
61-
code = fmt.Sprintf(`jsonpath("%s", %s) == %v`, jsonPath, objectVariable, parameterValue)
52+
code = fmt.Sprintf(`jsonpath("%s", %s) == %s`, jsonPath, objectVariable, paramCode)
6253
}
6354
case models.ConditionDoesNotEqual:
6455
code = fmt.Sprintf(`!%s`, fnWithParamLine("equals"))
@@ -99,31 +90,3 @@ func extractScriptBase(condition *pb.Condition) (string, *pb.ConditionBaseRef) {
9990
}
10091
return jsonPath, ref
10192
}
102-
103-
func parameterToScriptValue(condition *pb.Condition) (parameterValue any, isObj bool) {
104-
switch condition.Parameter.(type) {
105-
case *pb.Condition_BoolValue:
106-
parameterValue = condition.GetBoolValue()
107-
case *pb.Condition_IntValue:
108-
parameterValue = condition.GetIntValue()
109-
case *pb.Condition_StringValue:
110-
// return arr/obj in tengo format
111-
var v interface{}
112-
val := condition.GetStringValue()
113-
if err := json.Unmarshal([]byte(val), &v); err == nil {
114-
rv := reflect.ValueOf(v)
115-
kind := rv.Kind()
116-
if kind == reflect.Array || kind == reflect.Slice || kind == reflect.Map {
117-
parameterValue = val
118-
isObj = true
119-
return
120-
}
121-
}
122-
parameterValue = strconv.Quote(condition.GetStringValue())
123-
case *pb.Condition_ByteValue:
124-
parameterValue = condition.GetByteValue()
125-
default:
126-
parameterValue = nil
127-
}
128-
return
129-
}

apps/api/expr.proto

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,7 @@ message Condition {
2828
// The type of this condition, based on the internally defined ConditionType
2929
string type = 3;
3030
// A condition parameter holds a target value (right hand side) for a comparison
31-
oneof parameter {
32-
string stringValue = 4;
33-
bool boolValue = 5;
34-
int32 intValue = 6;
35-
bytes byteValue = 7;
36-
};
37-
}
38-
39-
message Conditions {
40-
repeated Condition conditions = 1;
31+
bytes value = 4;
4132
}
4233

4334
message Expression {

apps/api/models/models.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type EvaluationResult struct {
3434
TargetID string `json:"targetID"`
3535
TargetType string `json:"actionTargetType"`
3636
Action string `json:"action"`
37-
Value string `json:"Value"`
37+
Value string `json:"value"`
3838
}
3939

4040
func (e EvaluationResult) MarshalLogObject(enc zapcore.ObjectEncoder) error {

apps/api/models/validate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
func ValidateExpression(expression map[string]interface{}) (*pb.Expression, error) {
1212
exprBytes, err := json.Marshal(expression)
1313
if err != nil {
14-
return nil, fmt.Errorf("expression is not base64 encoded: %v", err)
14+
return nil, fmt.Errorf("invalid expression: %w", err)
1515
}
1616
expr := &pb.Expression{}
1717
if unmarshalErr := protojson.Unmarshal(exprBytes, expr); err != nil {

0 commit comments

Comments
 (0)