Skip to content

Commit

Permalink
feat(image): add Tagger
Browse files Browse the repository at this point in the history
  • Loading branch information
bounoable committed Sep 5, 2022
1 parent 173bea6 commit 20292d2
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 2 deletions.
31 changes: 29 additions & 2 deletions image/pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package image_test

import (
"context"
"fmt"
"testing"

"github.com/google/go-cmp/cmp"
Expand All @@ -14,6 +15,14 @@ func TestPipeline_Run(t *testing.T) {
pipe := image.Pipeline{
image.Resize(image.DimensionMap{"sm": {360}, "md": {640}, "lg": {960}}),
image.Compress(compression.JPEG(75)),
image.Tag(image.NewTags("foo", "bar")),
image.TagBy(func(p image.Processed) image.Tags {
suffix := "non-original"
if p.Original {
suffix = "original"
}
return image.NewTags(fmt.Sprintf("tagby:%s", suffix))
}),
}

ctx, cancel := context.WithCancel(context.Background())
Expand All @@ -34,8 +43,8 @@ func TestPipeline_Run(t *testing.T) {
t.Fatalf("first image should be the original\n%s", cmp.Diff(original, result.Images[0].Image))
}

if len(result.Images[0].Tags) != 1 {
t.Fatalf("original image should have exactly 1 tag; has %d", len(result.Images[0].Tags))
if len(result.Images[0].Tags) != 4 {
t.Fatalf("original image should have exactly 4 tags; has %d", len(result.Images[0].Tags))
}

if !result.Images[0].Original {
Expand Down Expand Up @@ -77,6 +86,24 @@ func TestPipeline_Run(t *testing.T) {
if !result.Images[3].Tags.Contains("compressor=jpeg,quality=75") {
t.Fatalf("last image should have tag %q", "compressor=jpeg,quality=75")
}

for _, img := range result.Images {
if !img.Tags.Contains("foo") {
t.Fatalf("all images should have tag %q", "foo")
}

if !img.Tags.Contains("bar") {
t.Fatalf("all images should have tag %q", "bar")
}

if img.Original && !img.Tags.Contains("tagby:original") {
t.Fatalf("original image should have tag %q", "tagby:original")
}

if !img.Original && !img.Tags.Contains("tagby:non-original") {
t.Fatalf("non-original image should have tag %q", "tagby:non-original")
}
}
}

func TestPipeline_Run_CompressOriginal(t *testing.T) {
Expand Down
26 changes: 26 additions & 0 deletions image/tagger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package image

// Tagger is a Processor that adds tags to images.
type Tagger struct {
fn func(Processed) Tags
}

// Tag returns a Tagger that adds the provided tags to images.
func Tag(tags Tags) *Tagger {
return TagBy(func(Processed) Tags {
return tags
})
}

// TagBy returns a Tagger that adds tags to images. For each image, the provided
// function is called to determine which tags to add to the image.
func TagBy(fn func(Processed) Tags) *Tagger {
return &Tagger{fn}
}

// Process implements [Processor]. It adds the configured tags to the image.
func (tagger *Tagger) Process(ctx ProcessorContext) ([]Processed, error) {
pimg := ctx.Image()
pimg.Tags = pimg.Tags.With(tagger.fn(pimg)...)
return []Processed{pimg}, nil
}
38 changes: 38 additions & 0 deletions image/tagger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package image_test

import (
"context"
"testing"

"github.com/modernice/media-tools/image"
"github.com/modernice/media-tools/image/internal"
)

func TestTagger_Process(t *testing.T) {
original := newExample()

tagger := image.Tag(image.NewTags("foo", "bar"))

ctx := image.NewProcessorContext(context.Background(), image.Processed{Image: original, Original: true})

tagged, err := tagger.Process(ctx)
if err != nil {
t.Fatalf("run processor: %v", err)
}

if len(tagged) != 1 {
t.Fatalf("expected 1 image; got %d", len(tagged))
}

if !internal.EqualImages(original, tagged[0].Image) {
t.Fatalf("tagger should not alter the image")
}

if !tagged[0].Tags.Contains("foo") {
t.Fatalf("expected image to have tag %q", "foo")
}

if !tagged[0].Tags.Contains("bar") {
t.Fatalf("expected image to have tag %q", "bar")
}
}

0 comments on commit 20292d2

Please sign in to comment.