From 80b700973da3077e237639a4f5ddf8013d3158a8 Mon Sep 17 00:00:00 2001 From: mdrakos Date: Thu, 23 Nov 2023 14:57:43 -0800 Subject: [PATCH 1/3] Add support for fail build expression format --- .../runbits/buildscript/buildscript_test.go | 3 -- .../buildexpression/buildexpression.go | 32 +++++++++++++++++++ .../buildexpression/buildexpression_test.go | 7 ++++ .../testdata/buildexpression-fail.json | 5 +++ 4 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 pkg/platform/runtime/buildexpression/testdata/buildexpression-fail.json diff --git a/internal/runbits/buildscript/buildscript_test.go b/internal/runbits/buildscript/buildscript_test.go index ceb19353b2..bcf9f3809d 100644 --- a/internal/runbits/buildscript/buildscript_test.go +++ b/internal/runbits/buildscript/buildscript_test.go @@ -48,11 +48,8 @@ in: assert.Equal(t, `let: runtime = solve( platforms = [ -<<<<<<< local "77777", -======= "12345", ->>>>>>> remote "67890" ], requirements = [ diff --git a/pkg/platform/runtime/buildexpression/buildexpression.go b/pkg/platform/runtime/buildexpression/buildexpression.go index 64a2e88886..e03bc59ab9 100644 --- a/pkg/platform/runtime/buildexpression/buildexpression.go +++ b/pkg/platform/runtime/buildexpression/buildexpression.go @@ -84,6 +84,10 @@ type In struct { Name *string } +type Fail struct { + Message string +} + // New creates a BuildExpression from a JSON byte array. // The JSON must be a valid BuildExpression in the following format: // @@ -143,6 +147,12 @@ func New(data []byte) (*BuildExpression, error) { } expr.Let = let + } else if key == "fail" { + fail, err := newFail(path, v) + if err != nil { + return nil, errs.Wrap(err, "Could not parse 'fail' key") + } + return nil, locale.NewError("err_build_expression_fail", "BuildExpression", fail.Message) } else if isAp(path, v) { ap, err := newAp(path, v) if err != nil { @@ -453,6 +463,28 @@ func newIn(path []string, inValue interface{}) (*In, error) { return in, nil } +func newFail(path []string, m map[string]interface{}) (*Fail, error) { + path = append(path, "fail") + defer func() { + _, _, err := sliceutils.Pop(path) + if err != nil { + multilog.Error("Could not pop context: %v", err) + } + }() + + message, ok := m["message"] + if !ok { + return nil, errs.New("Build expression's 'fail' object has no 'message' key") + } + + messageStr, ok := message.(string) + if !ok { + return nil, errs.New("'message' key's value is not a string") + } + + return &Fail{Message: messageStr}, nil +} + // validateRequirements ensures that the requirements in the BuildExpression contain // both the name and namespace fields. These fileds are used for requirement operations. func (e *BuildExpression) validateRequirements() error { diff --git a/pkg/platform/runtime/buildexpression/buildexpression_test.go b/pkg/platform/runtime/buildexpression/buildexpression_test.go index 08d965e1ef..84d9229920 100644 --- a/pkg/platform/runtime/buildexpression/buildexpression_test.go +++ b/pkg/platform/runtime/buildexpression/buildexpression_test.go @@ -70,6 +70,13 @@ func TestNew(t *testing.T) { }, wantErr: false, }, + { + name: "fail", + args: args{ + filename: "buildexpression-fail.json", + }, + wantErr: true, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/pkg/platform/runtime/buildexpression/testdata/buildexpression-fail.json b/pkg/platform/runtime/buildexpression/testdata/buildexpression-fail.json new file mode 100644 index 0000000000..1d300f3ed6 --- /dev/null +++ b/pkg/platform/runtime/buildexpression/testdata/buildexpression-fail.json @@ -0,0 +1,5 @@ +{ + "fail": { + "message": "invalid order" + } +} \ No newline at end of file From 28c4a63c0d23c1e7ce968bebd2aa2853a61b5a08 Mon Sep 17 00:00:00 2001 From: mdrakos Date: Thu, 23 Nov 2023 15:04:47 -0800 Subject: [PATCH 2/3] Remove placeholder message --- pkg/platform/runtime/buildexpression/buildexpression.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/platform/runtime/buildexpression/buildexpression.go b/pkg/platform/runtime/buildexpression/buildexpression.go index e03bc59ab9..c6c16e5720 100644 --- a/pkg/platform/runtime/buildexpression/buildexpression.go +++ b/pkg/platform/runtime/buildexpression/buildexpression.go @@ -152,7 +152,7 @@ func New(data []byte) (*BuildExpression, error) { if err != nil { return nil, errs.Wrap(err, "Could not parse 'fail' key") } - return nil, locale.NewError("err_build_expression_fail", "BuildExpression", fail.Message) + return nil, locale.NewError("err_build_expression_fail", "", fail.Message) } else if isAp(path, v) { ap, err := newAp(path, v) if err != nil { From 8433373e6976b9dd01a9784d3ff06e2d4fa9d102 Mon Sep 17 00:00:00 2001 From: mdrakos Date: Thu, 23 Nov 2023 15:31:50 -0800 Subject: [PATCH 3/3] Revert buildscript test changes --- internal/runbits/buildscript/buildscript_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/internal/runbits/buildscript/buildscript_test.go b/internal/runbits/buildscript/buildscript_test.go index bcf9f3809d..ceb19353b2 100644 --- a/internal/runbits/buildscript/buildscript_test.go +++ b/internal/runbits/buildscript/buildscript_test.go @@ -48,8 +48,11 @@ in: assert.Equal(t, `let: runtime = solve( platforms = [ +<<<<<<< local "77777", +======= "12345", +>>>>>>> remote "67890" ], requirements = [