Skip to content

Commit

Permalink
Merge pull request #2 from jukeizu/validation
Browse files Browse the repository at this point in the history
Extra validation for selection parse input & github actions
  • Loading branch information
shawntoffel committed Jan 22, 2022
2 parents cc6b2d0 + 668f84d commit 842ace6
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 76 deletions.
64 changes: 0 additions & 64 deletions .circleci/config.yml

This file was deleted.

31 changes: 31 additions & 0 deletions .github/workflows/docker.yaml
@@ -0,0 +1,31 @@
name: Docker Image CI

on:
push:
branches: '*'
tags: '*'
pull_request:
branches: [ master ]

jobs:

build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Docker Build
run: make docker-build

- name: Login to Docker Hub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
if: startsWith(github.ref, 'refs/tags/')

- name: Docker Push
run: |
make docker-deploy
if: startsWith(github.ref, 'refs/tags/')
23 changes: 23 additions & 0 deletions .github/workflows/go.yaml
@@ -0,0 +1,23 @@
name: Go

on:
push:
branches: '*'
tags: '*'
pull_request:
branches: [ master ]

jobs:

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.17

- name: Build
run: make
2 changes: 1 addition & 1 deletion Dockerfile
@@ -1,4 +1,4 @@
FROM golang:1.12 as build
FROM golang:1.17 as build
WORKDIR /go/src/github.com/jukeizu/selection
COPY Makefile go.mod go.sum ./
RUN make deps
Expand Down
2 changes: 1 addition & 1 deletion cmd/selection/main.go
Expand Up @@ -116,7 +116,7 @@ func main() {
}

func interrupt(cancel <-chan struct{}) error {
c := make(chan os.Signal)
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)

select {
Expand Down
26 changes: 16 additions & 10 deletions selection/service.go
Expand Up @@ -11,16 +11,18 @@ import (
)

type DefaultService struct {
logger zerolog.Logger
repository Repository
sorter Sorter
batcher Batcher
regex *regexp.Regexp
logger zerolog.Logger
repository Repository
sorter Sorter
batcher Batcher
parseRegex *regexp.Regexp
validationRegex *regexp.Regexp
}

func NewDefaultService(logger zerolog.Logger, repository Repository, sorter Sorter, batcher Batcher) Service {
regex := regexp.MustCompile("[0-9]+")
return &DefaultService{logger, repository, sorter, batcher, regex}
parseRegex := regexp.MustCompile(`\b\d+\b`)
validationRegex := regexp.MustCompile(`^[\d\s]+$`)
return &DefaultService{logger, repository, sorter, batcher, parseRegex, validationRegex}
}

func (s DefaultService) Create(req CreateSelectionRequest) (SelectionReply, error) {
Expand Down Expand Up @@ -65,24 +67,28 @@ func (s DefaultService) Create(req CreateSelectionRequest) (SelectionReply, erro
}

func (s DefaultService) Parse(req ParseSelectionRequest) ([]RankedOption, error) {
if !s.validationRegex.MatchString(req.Content) {
return nil, NewValidationError("Input must only container number values.")
}

selection, err := s.repository.Selection(req.AppId, req.InstanceId, req.UserId, req.ServerId)
if err != nil {
return nil, err
}

choices := s.regex.FindAllString(req.Content, -1)
choices := s.parseRegex.FindAllString(req.Content, -1)

rankedOptions := []RankedOption{}

for i, choice := range choices {
c, err := strconv.Atoi(choice)
if err != nil {
return nil, NewValidationError("%s is not a valid integer. %s", choice, err)
return nil, NewValidationError("Input `%s` is not a valid selection.", choice)
}

option, ok := selection.Options[c]
if !ok {
return nil, NewValidationError("could not find option for id: %d", c)
return nil, NewValidationError("Input `%d` is not a valid selection.", c)
}

rankedOption := RankedOption{
Expand Down

0 comments on commit 842ace6

Please sign in to comment.