Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions pkg/platform/runtime/buildexpression/buildexpression.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
//
Expand Down Expand Up @@ -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", "", fail.Message)
} else if isAp(path, v) {
ap, err := newAp(path, v)
if err != nil {
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions pkg/platform/runtime/buildexpression/buildexpression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"fail": {
"message": "invalid order"
}
}