Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
bcspragu committed Feb 14, 2024
1 parent d7c8020 commit f47f8a6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 29 deletions.
40 changes: 14 additions & 26 deletions async/req.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,26 @@ import (
"github.com/RMI/pacta/task"
)

func ParsePortfolioReq() (*task.ParsePortfolioRequest, error) {
taskStr := os.Getenv("PARSE_PORTFOLIO_REQUEST")
if taskStr == "" {
return nil, errors.New("no PARSE_PORTFOLIO_REQUEST given")
}
var task task.ParsePortfolioRequest
if err := json.NewDecoder(strings.NewReader(taskStr)).Decode(&task); err != nil {
return nil, fmt.Errorf("failed to load ParsePortfolioRequest: %w", err)
}
return &task, nil
func LoadParsePortfolioRequestFromEnv() (*task.ParsePortfolioRequest, error) {
return loadFromEnv[task.ParsePortfolioRequest]("PARSE_PORTFOLIO_REQUEST", "ParsePortfolioRequest")
}

func CreateAuditReq() (*task.CreateAuditRequest, error) {
car := os.Getenv("CREATE_AUDIT_REQUEST")
if car == "" {
return nil, errors.New("no CREATE_AUDIT_REQUEST was given")
}
var task task.CreateAuditRequest
if err := json.NewDecoder(strings.NewReader(car)).Decode(&task); err != nil {
return nil, fmt.Errorf("failed to load CreateAuditRequest: %w", err)
}
return &task, nil
func LoadCreateAuditRequestFromEnv() (*task.CreateAuditRequest, error) {
return loadFromEnv[task.CreateAuditRequest]("CREATE_AUDIT_REQUEST", "CreateAuditRequest")
}

func LoadCreateReportRequestFromEnv() (*task.CreateReportRequest, error) {
return loadFromEnv[task.CreateReportRequest]("CREATE_REPORT_REQUEST", "CreateReportRequest")
}

func CreateReportReq() (*task.CreateReportRequest, error) {
crr := os.Getenv("CREATE_REPORT_REQUEST")
if crr == "" {
func loadFromEnv[T any](envVar string, entityName string) (*T, error) {
envStr := os.Getenv(envVar)
if envStr == "" {
return nil, errors.New("no CREATE_REPORT_REQUEST was given")
}
var task task.CreateReportRequest
if err := json.NewDecoder(strings.NewReader(crr)).Decode(&task); err != nil {
return nil, fmt.Errorf("failed to load CreateReportRequest: %w", err)
var task T
if err := json.NewDecoder(strings.NewReader(envStr)).Decode(&task); err != nil {
return nil, fmt.Errorf("failed to load %q: %w", entityName, err)
}
return &task, nil
}
2 changes: 1 addition & 1 deletion cmd/parser/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func run(args []string) error {
return errors.New("no TASK_ID given")
}

req, err := async.ParsePortfolioReq()
req, err := async.LoadParsePortfolioRequestFromEnv()
if err != nil {
return fmt.Errorf("failed to parse portfolio request: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/runner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ func run(args []string) error {
}

validTasks := map[task.Type]func(context.Context, task.ID) error{
task.CreateReport: toRunFn(async.CreateReportReq, func(ctx context.Context, id task.ID, req *task.CreateReportRequest) error {
task.CreateReport: toRunFn(async.LoadCreateReportRequestFromEnv, func(ctx context.Context, id task.ID, req *task.CreateReportRequest) error {
return h.CreateReport(ctx, id, req, *azReportContainer)
}),
task.CreateAudit: toRunFn(async.CreateAuditReq, h.CreateAudit),
task.CreateAudit: toRunFn(async.LoadCreateAuditRequestFromEnv, h.CreateAudit),
}

taskID := task.ID(os.Getenv("TASK_ID"))
Expand Down

0 comments on commit f47f8a6

Please sign in to comment.