Skip to content

Commit

Permalink
♻️ Cleaning UP
Browse files Browse the repository at this point in the history
  • Loading branch information
UltiRequiem committed Sep 22, 2021
1 parent 1305c98 commit 894fd52
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.git
.travis.yml
Makefile
README.md
Dockerfile
22 changes: 18 additions & 4 deletions internal/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,19 @@ func FibonacciHandler(c echo.Context) error {

number, err := strconv.Atoi(numberParam)

fmt.Println(err)

if err != nil {
return logManageableError(fmt.Errorf(`Got '%s', but a number was expected.`, numberParam), http.StatusUnprocessableEntity, c)
}

fiboNum, err := Fibonacci(number)

if err != nil {
logManageableError(fmt.Sprintf(`Got "%s", but a number was expected.`, numberParam), http.StatusUnprocessableEntity, c)
return logManageableError(err, http.StatusUnprocessableEntity, c)
}

return c.String(http.StatusOK, strconv.Itoa(Fibonacci(number)))
return c.JSON(http.StatusOK, &FibonacciNumber{fiboNum})
}

func FibonacciSequenceHandler(c echo.Context) error {
Expand All @@ -28,8 +36,14 @@ func FibonacciSequenceHandler(c echo.Context) error {
number, err := strconv.Atoi(numberParam)

if err != nil {
logManageableError(fmt.Sprintf(`Got "%s", but a number was expected.`, numberParam), http.StatusUnprocessableEntity, c)
return logManageableError(fmt.Errorf(`Got '%s', but a number was expected.`, numberParam), http.StatusUnprocessableEntity, c)
}

fiboNums, err := FibonacciSequence(number)

if err != nil {
return logManageableError(err, http.StatusUnprocessableEntity, c)
}

return c.JSON(http.StatusOK, FibonacciSequence(number))
return c.JSON(http.StatusOK, &FibonacciNumberSequence{fiboNums})
}
14 changes: 14 additions & 0 deletions internal/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package internal

type FibonacciNumber struct {
Number int
}

type FibonacciNumberSequence struct {
Numbers []int
}

type EchoError struct {
Message string
Code int
}
8 changes: 5 additions & 3 deletions internal/utils.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package internal

import (
"github.com/labstack/echo/v4"
"fmt"
"log"

"github.com/labstack/echo/v4"
)

func logManageableError(message string, code int, c echo.Context) error {
func logManageableError(message error, code int, c echo.Context) error {
log.Println(message)
return c.String(code, message)
return c.JSON(code, &EchoError{fmt.Sprintf(`%v`, message), code})
}
57 changes: 41 additions & 16 deletions pkg/root.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,54 @@
package fibonnaci
package fibonacci

func Fibonacci(n int) int {
f := make([]int, n+1, n+2)
import "fmt"

if n < 2 {
f = f[0:2]
var cache = make(map[int]int)

func Fibonacci(n int) (int, error) {
if n < 0 {
return 0, fmt.Errorf("Got '%d' but expected a number bigger than 0.", n)
}

f[0] = 0
f[1] = 1
// If already in cache, don't operate again
v, exits := cache[n]

for i := 2; i <= n; i++ {
f[i] = f[i-1] + f[i-2]
if exits {
return v, nil
}

return f[n]
// Equal
if n < 2 {
cache[n] = n
return n, nil
}

// TODO: Use a loop instead of recursion here
// I'dont check the error because I alredy know that is bigger than 0 :D
vOne, _ := Fibonacci(n - 2)

vTwo, _ := Fibonacci(n - 1)

v = vOne + vTwo

// Save to cache
cache[n] = v

return v, nil
}

func FibonacciSequence(n int) []int {
var sequence []int
func FibonacciSequence(length int) ([]int, error) {
sequence := make([]int, length)

for i := 0; i <= n; i++ {
sequence = append(sequence, Fibonacci(i))
}
for i := 0; i < length; i++ {
fibo, err := Fibonacci(i)

return sequence
if err != nil {
return make([]int, 0), err

}

sequence[i] = fibo
}

return sequence, nil
}

0 comments on commit 894fd52

Please sign in to comment.