-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
93 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |