103 lines
2.3 KiB
Go
103 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type ActionModifyColumn struct {
|
|
ActionID uuid.UUID
|
|
OutputSchema *Schema
|
|
InputSchema *Schema
|
|
Config ModifyColumnConfig
|
|
status ActionStatus
|
|
bus *Bus
|
|
}
|
|
|
|
type ModifyColumnConfig struct {
|
|
ActionConfig
|
|
}
|
|
|
|
func NewModifyColumn(config ModifyColumnConfig, b *Bus) (ActionModifyColumn, error) {
|
|
as := ActionModifyColumn{}
|
|
err := as.init(config, b)
|
|
if err != nil {
|
|
return ActionModifyColumn{}, err
|
|
}
|
|
return as, nil
|
|
}
|
|
|
|
func (s *ActionModifyColumn) init(config ModifyColumnConfig, b *Bus) error {
|
|
if len(config.Inputs) <= 0 {
|
|
return errors.New(fmt.Sprintf("error no input schema defined. ActionID: %v", config.ActionID.String()))
|
|
}
|
|
s.InputSchema = config.Inputs[0].InputSchema
|
|
s.OutputSchema = config.OutputSchema
|
|
s.ActionID = config.ActionID
|
|
s.Config = config
|
|
s.status = ActionStatus{
|
|
State: NotStarted,
|
|
HandledRows: 0,
|
|
ExpectedRows: -1,
|
|
}
|
|
s.bus = b
|
|
return nil
|
|
}
|
|
|
|
func (s *ActionModifyColumn) Run(contentRow ContentRow) error {
|
|
|
|
// convert the schema to the new one
|
|
newContentRow := ContentRow{
|
|
Schema: s.Config.OutputSchema,
|
|
Values: make(map[uuid.UUID]string),
|
|
Raw: "",
|
|
Source: &s.ActionID,
|
|
}
|
|
|
|
for _, col := range newContentRow.Schema.Columns {
|
|
value, ok := contentRow.Values[col.ID]
|
|
if !ok {
|
|
value = col.DefaultValue
|
|
}
|
|
newContentRow.Values[col.ID] = value
|
|
if strings.EqualFold(newContentRow.Raw, "") {
|
|
newContentRow.Raw = value
|
|
} else {
|
|
newContentRow.Raw = newContentRow.Raw + "," + value
|
|
}
|
|
}
|
|
return s.publishRow(newContentRow)
|
|
}
|
|
|
|
func (s *ActionModifyColumn) publishRow(row ContentRow) error {
|
|
// TODO: decide on the method of transporting data to the next action
|
|
return s.bus.Run(s.ActionID, row)
|
|
}
|
|
|
|
func (s *ActionModifyColumn) publishErrorRow(row ContentRow) error {
|
|
// TODO: decide on the method of transporting data to the next action
|
|
return nil
|
|
}
|
|
|
|
func (s *ActionModifyColumn) Validate() error {
|
|
if len(s.Config.Inputs) <= 0 {
|
|
return errors.New(fmt.Sprintf("error no input schema defined. ActionID: %v", s.ActionID.String()))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *ActionModifyColumn) GetActionID() uuid.UUID {
|
|
return s.ActionID
|
|
}
|
|
|
|
func (s *ActionModifyColumn) Status() ActionStatus {
|
|
return s.status
|
|
}
|
|
|
|
func (s *ActionModifyColumn) GetActionConfig() ActionConfig {
|
|
return s.Config.ActionConfig
|
|
}
|