Skip to content

Commit

Permalink
Merge pull request #16 from Talento90/feature-context
Browse files Browse the repository at this point in the history
Feature context
  • Loading branch information
Talento90 authored Oct 21, 2018
2 parents bcd2b77 + 6e03ef3 commit ccedd6b
Show file tree
Hide file tree
Showing 34 changed files with 153 additions and 54 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*.so
*.dylib
gin-bin
debug
imgartapi

# Test binary, build with `go test -c`
*.test
Expand Down
Binary file removed debug
Binary file not shown.
5 changes: 3 additions & 2 deletions effect/blur.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package effect

import (
"context"
"image"

"github.com/disintegration/imaging"
Expand Down Expand Up @@ -29,7 +30,7 @@ func NewBlur() imgart.Effect {
}
}

func (r *blur) Transform(img image.Image, params map[string]interface{}) (image.Image, error) {
func (r *blur) Transform(ctx context.Context, img image.Image, params map[string]interface{}) (image.Image, error) {
sigma, err := floatBinder("sigma", params)

if err != nil {
Expand All @@ -38,5 +39,5 @@ func (r *blur) Transform(img image.Image, params map[string]interface{}) (image.

img = imaging.Blur(img, sigma)

return img, nil
return img, ctx.Err()
}
3 changes: 2 additions & 1 deletion effect/blur_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package effect

import (
"context"
"image"
"testing"

Expand Down Expand Up @@ -31,7 +32,7 @@ func TestBlurTransform(t *testing.T) {
img := image.NewRGBA(image.Rect(0, 0, 100, 50))
blur := NewBlur()

_, err := blur.Transform(img, tc.params)
_, err := blur.Transform(context.Background(), img, tc.params)

if tc.err != "" {
if err == nil || !errors.Is(tc.err, err) {
Expand Down
5 changes: 3 additions & 2 deletions effect/brightness.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package effect

import (
"context"
"image"

"github.com/disintegration/imaging"
Expand Down Expand Up @@ -29,7 +30,7 @@ func NewBrightness() imgart.Effect {
}
}

func (r *brightness) Transform(img image.Image, params map[string]interface{}) (image.Image, error) {
func (r *brightness) Transform(ctx context.Context, img image.Image, params map[string]interface{}) (image.Image, error) {
percentage, err := floatBinder("percentage", params)

if err != nil {
Expand All @@ -38,5 +39,5 @@ func (r *brightness) Transform(img image.Image, params map[string]interface{}) (

img = imaging.AdjustBrightness(img, percentage)

return img, nil
return img, ctx.Err()
}
3 changes: 2 additions & 1 deletion effect/brightness_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package effect

import (
"context"
"image"
"testing"

Expand Down Expand Up @@ -31,7 +32,7 @@ func TestBrightnessTransform(t *testing.T) {
img := image.NewRGBA(image.Rect(0, 0, 100, 50))
brightness := NewBrightness()

_, err := brightness.Transform(img, tc.params)
_, err := brightness.Transform(context.Background(), img, tc.params)

if tc.err != "" {
if err == nil || !errors.Is(tc.err, err) {
Expand Down
5 changes: 3 additions & 2 deletions effect/contrast.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package effect

import (
"context"
"image"

"github.com/disintegration/imaging"
Expand Down Expand Up @@ -29,7 +30,7 @@ func NewContrast() imgart.Effect {
}
}

func (r *contrast) Transform(img image.Image, params map[string]interface{}) (image.Image, error) {
func (r *contrast) Transform(ctx context.Context, img image.Image, params map[string]interface{}) (image.Image, error) {
percentage, err := floatBinder("percentage", params)

if err != nil {
Expand All @@ -38,5 +39,5 @@ func (r *contrast) Transform(img image.Image, params map[string]interface{}) (im

img = imaging.AdjustContrast(img, percentage)

return img, nil
return img, ctx.Err()
}
3 changes: 2 additions & 1 deletion effect/contrast_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package effect

import (
"context"
"image"
"testing"

Expand All @@ -26,7 +27,7 @@ func TestContrastTransform(t *testing.T) {
img := image.NewRGBA(image.Rect(0, 0, 100, 50))
constrast := NewContrast()

_, err := constrast.Transform(img, tc.params)
_, err := constrast.Transform(context.Background(), img, tc.params)

if tc.err != "" {
if err == nil || !errors.Is(tc.err, err) {
Expand Down
5 changes: 3 additions & 2 deletions effect/crop.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package effect

import (
"context"
"image"

"github.com/disintegration/imaging"
Expand Down Expand Up @@ -29,7 +30,7 @@ func NewCrop() imgart.Effect {
}
}

func (o *crop) Transform(img image.Image, params map[string]interface{}) (image.Image, error) {
func (o *crop) Transform(ctx context.Context, img image.Image, params map[string]interface{}) (image.Image, error) {
rectangle, err := rectangleBinder("rectangle", params)

if err != nil {
Expand All @@ -38,5 +39,5 @@ func (o *crop) Transform(img image.Image, params map[string]interface{}) (image.

img = imaging.Crop(img, rectangle)

return img, nil
return img, ctx.Err()
}
3 changes: 2 additions & 1 deletion effect/crop_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package effect

import (
"context"
"image"
"testing"

Expand Down Expand Up @@ -30,7 +31,7 @@ func TestCropTransform(t *testing.T) {
img := image.NewRGBA(image.Rect(0, 0, 100, 50))
crop := NewCrop()

_, err := crop.Transform(img, tc.params)
_, err := crop.Transform(context.Background(), img, tc.params)

if tc.err != "" {
if err == nil || !errors.Is(tc.err, err) {
Expand Down
5 changes: 3 additions & 2 deletions effect/gamma.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package effect

import (
"context"
"image"

"github.com/disintegration/imaging"
Expand Down Expand Up @@ -29,7 +30,7 @@ func NewGamma() imgart.Effect {
}
}

func (r *gamma) Transform(img image.Image, params map[string]interface{}) (image.Image, error) {
func (r *gamma) Transform(ctx context.Context, img image.Image, params map[string]interface{}) (image.Image, error) {
gamma, err := floatBinder("gamma", params)

if err != nil {
Expand All @@ -38,5 +39,5 @@ func (r *gamma) Transform(img image.Image, params map[string]interface{}) (image

img = imaging.AdjustGamma(img, gamma)

return img, nil
return img, ctx.Err()
}
3 changes: 2 additions & 1 deletion effect/gamma_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package effect

import (
"context"
"image"
"testing"

Expand All @@ -27,7 +28,7 @@ func TestGammaTransform(t *testing.T) {
img := image.NewRGBA(image.Rect(0, 0, 100, 50))
gamma := NewGamma()

_, err := gamma.Transform(img, tc.params)
_, err := gamma.Transform(context.Background(), img, tc.params)

if tc.err != "" {
if err == nil || !errors.Is(tc.err, err) {
Expand Down
7 changes: 4 additions & 3 deletions effect/overlay.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package effect

import (
"context"
"image"

"github.com/disintegration/imaging"
Expand Down Expand Up @@ -44,7 +45,7 @@ func NewOverlay(imgRepository imgart.ImageRepository) imgart.Effect {
}
}

func (o *overlay) Transform(img image.Image, params map[string]interface{}) (image.Image, error) {
func (o *overlay) Transform(ctx context.Context, img image.Image, params map[string]interface{}) (image.Image, error) {
position, err := pointBinder("position", params)

if err != nil {
Expand All @@ -63,13 +64,13 @@ func (o *overlay) Transform(img image.Image, params map[string]interface{}) (ima
opacity = 100
}

overlayImg, _, err := o.imgRepository.Get(url.String())
overlayImg, _, err := o.imgRepository.Get(ctx, url.String())

if err != nil {
return nil, err
}

img = imaging.Overlay(img, overlayImg, position, opacity)

return img, nil
return img, ctx.Err()
}
3 changes: 2 additions & 1 deletion effect/overlay_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package effect

import (
"context"
"image"
"testing"

Expand Down Expand Up @@ -40,7 +41,7 @@ func TestOverlayTransform(t *testing.T) {
img := image.NewRGBA(image.Rect(0, 0, 100, 50))
overlay := NewOverlay(mock.NewImageRepository())

_, err := overlay.Transform(img, tc.params)
_, err := overlay.Transform(context.Background(), img, tc.params)

if tc.err != "" {
if err == nil || !errors.Is(tc.err, err) {
Expand Down
5 changes: 3 additions & 2 deletions effect/resize.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package effect

import (
"context"
"image"

"github.com/disintegration/imaging"
Expand Down Expand Up @@ -43,7 +44,7 @@ func NewResize() imgart.Effect {
}
}

func (r *resize) Transform(img image.Image, params map[string]interface{}) (image.Image, error) {
func (r *resize) Transform(ctx context.Context, img image.Image, params map[string]interface{}) (image.Image, error) {
width, err := integerBinder("width", params)

if err != nil {
Expand All @@ -64,5 +65,5 @@ func (r *resize) Transform(img image.Image, params map[string]interface{}) (imag

img = imaging.Resize(img, width, height, filter)

return img, nil
return img, ctx.Err()
}
3 changes: 2 additions & 1 deletion effect/resize_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package effect

import (
"context"
"image"
"testing"

Expand Down Expand Up @@ -39,7 +40,7 @@ func TestResizeTransform(t *testing.T) {
img := image.NewRGBA(image.Rect(0, 0, 100, 50))
resize := NewResize()

_, err := resize.Transform(img, tc.params)
_, err := resize.Transform(context.Background(), img, tc.params)

if tc.err != "" {
if err == nil || !errors.Is(tc.err, err) {
Expand Down
5 changes: 3 additions & 2 deletions effect/rotate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package effect

import (
"context"
"image"
"image/color"

Expand Down Expand Up @@ -38,7 +39,7 @@ func NewRotate() imgart.Effect {
}
}

func (r *rotate) Transform(img image.Image, params map[string]interface{}) (image.Image, error) {
func (r *rotate) Transform(ctx context.Context, img image.Image, params map[string]interface{}) (image.Image, error) {
angle, err := floatBinder("angle", params)

if err != nil {
Expand All @@ -53,5 +54,5 @@ func (r *rotate) Transform(img image.Image, params map[string]interface{}) (imag

img = imaging.Rotate(img, angle, bgColor)

return img, nil
return img, ctx.Err()
}
3 changes: 2 additions & 1 deletion effect/rotate_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package effect

import (
"context"
"image"
"testing"

Expand Down Expand Up @@ -34,7 +35,7 @@ func TestRotateTransform(t *testing.T) {
img := image.NewRGBA(image.Rect(0, 0, 100, 50))
rotate := NewRotate()

_, err := rotate.Transform(img, tc.params)
_, err := rotate.Transform(context.Background(), img, tc.params)

if tc.err != "" {
if err == nil || !errors.Is(tc.err, err) {
Expand Down
25 changes: 25 additions & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package errors

import (
"context"
)

// Error applicational
type Error struct {
Type Type
Expand Down Expand Up @@ -35,6 +39,10 @@ const (
Validation Type = "validation"
// AlreadyExists error
AlreadyExists Type = "already_exists"
// Timeout error
Timeout Type = "timeout"
// Cancelled error
Cancelled Type = "cancelled"
)

func (t Type) String() string {
Expand All @@ -49,6 +57,10 @@ func (t Type) String() string {
return "Validation error"
case AlreadyExists:
return "Item already exists"
case Cancelled:
return "Cancelled error"
case Timeout:
return "Timeout error"
}

return "Unknown error"
Expand Down Expand Up @@ -88,6 +100,19 @@ func EInternal(msg string, err error) error {
return New(Internal, msg, err)
}

// IsContextError creates an error of type Cancelled or Deadline
func IsContextError(err error) error {
if err == context.Canceled {
return New(Cancelled, "Operation cancelled", err)
}

if err.Error() == context.DeadlineExceeded.Error() {
return New(Timeout, "Operation timeout", err)
}

return nil
}

// Is method checks if an error is of a specific type
func Is(t Type, err error) bool {
e, ok := err.(*Error)
Expand Down
Loading

0 comments on commit ccedd6b

Please sign in to comment.