github.com/kubeshop/testkube@v1.17.23/pkg/tcl/cloudtcl/data/testworkflow/utils.go (about) 1 // Copyright 2024 Testkube. 2 // 3 // Licensed as a Testkube Pro file under the Testkube Community 4 // License (the "License"); you may not use this file except in compliance with 5 // the License. You may obtain a copy of the License at 6 // 7 // https://github.com/kubeshop/testkube/blob/main/licenses/TCL.txt 8 9 package testworkflow 10 11 import ( 12 "context" 13 "encoding/json" 14 15 "github.com/kubeshop/testkube/pkg/cloud/data/executor" 16 "github.com/kubeshop/testkube/pkg/tcl/repositorytcl/testworkflow" 17 ) 18 19 func passWithErr[T any, U any](e executor.Executor, ctx context.Context, req interface{}, fn func(u T) (U, error)) (v U, err error) { 20 response, err := e.Execute(ctx, command(req), req) 21 if err != nil { 22 return v, err 23 } 24 var commandResponse T 25 if err = json.Unmarshal(response, &commandResponse); err != nil { 26 return v, err 27 } 28 return fn(commandResponse) 29 } 30 31 func pass[T any, U any](e executor.Executor, ctx context.Context, req interface{}, fn func(u T) U) (v U, err error) { 32 return passWithErr(e, ctx, req, func(u T) (U, error) { 33 return fn(u), nil 34 }) 35 } 36 37 func passNoContentProcess[T any](e executor.Executor, ctx context.Context, req interface{}, fn func(u T) error) (err error) { 38 _, err = passWithErr(e, ctx, req, func(u T) (interface{}, error) { 39 return nil, fn(u) 40 }) 41 return err 42 } 43 44 func passNoContent(e executor.Executor, ctx context.Context, req interface{}) (err error) { 45 return passNoContentProcess(e, ctx, req, func(u interface{}) error { 46 return nil 47 }) 48 } 49 50 func mapFilters(s []testworkflow.Filter) []*testworkflow.FilterImpl { 51 v := make([]*testworkflow.FilterImpl, len(s)) 52 for i := range s { 53 if vv, ok := s[i].(testworkflow.FilterImpl); ok { 54 v[i] = &vv 55 } else { 56 v[i] = s[i].(*testworkflow.FilterImpl) 57 } 58 } 59 return v 60 }