//nolint:dupl package rulesengine import ( "context" "errors" "fmt" ) // AddCommands updates the dataset with given names. returns a list of // values where the values remain unchanged in the dataset. func (reng RulesEngine) AddCommands(ctx context.Context, commands []PostCommandPayload) (AddNameResult, error) { //validation if len(commands) == 0 { return AddNameResult{}, fmt.Errorf("empty command list") } if len(commands) > maxCommands { return AddNameResult{}, fmt.Errorf("total number of commands %d exceeds max %d", len(commands), maxCommands) } var retErr error for i, command := range commands { if err := command.Validate(); err != nil { retErr = errors.Join(retErr, fmt.Errorf("invalid command at %d: %w", i, err)) } } if retErr != nil { return AddNameResult{}, retErr } comms := []string{} for _, comm := range commands { comms = append(comms, comm.Name) } return reng.ds.AddCommands(ctx, comms) } func (reng RulesEngine) DeleteCommand(ctx context.Context, name string) (DeleteResult, error) { // validation if name == "" { return DeleteResult{}, fmt.Errorf("empty command name") } return reng.ds.DeleteCommand(ctx, name) } func (reng RulesEngine) ReadCommands(ctx context.Context) ([]Command, error) { return reng.ds.ReadAllCommands(ctx) } // Takes in a list of command names and returns all commands in db that match. // If the names list is empty, then all commands are returned. func (reng RulesEngine) ReadCommandsWithFilter(ctx context.Context, names []string) ([]Command, error) { return reng.ds.ReadCommandsWithFilter(ctx, names) } func (reng RulesEngine) ReadCommand(ctx context.Context, name string) (Command, error) { // validation if name == "" { return Command{}, fmt.Errorf("empty command name") } return reng.ds.ReadCommand(ctx, name) }