Skip to content

Commit ee54296

Browse files
committed
Fix options rewrite
1 parent 5426c30 commit ee54296

File tree

3 files changed

+28
-24
lines changed

3 files changed

+28
-24
lines changed

presets_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ func (s *PresetsTestSuite) TestParsePreset() {
1919
require.Nil(s.T(), err)
2020

2121
assert.Equal(s.T(), urlOptions{
22-
"resize": []string{"fit", "100", "200"},
23-
"sharpen": []string{"2"},
22+
urlOption{Name: "resize", Args: []string{"fit", "100", "200"}},
23+
urlOption{Name: "sharpen", Args: []string{"2"}},
2424
}, p["test"])
2525
}
2626

@@ -85,8 +85,8 @@ func (s *PresetsTestSuite) TestParsePresetComment() {
8585
func (s *PresetsTestSuite) TestCheckPresets() {
8686
p := presets{
8787
"test": urlOptions{
88-
"resize": []string{"fit", "100", "200"},
89-
"sharpen": []string{"2"},
88+
urlOption{Name: "resize", Args: []string{"fit", "100", "200"}},
89+
urlOption{Name: "sharpen", Args: []string{"2"}},
9090
},
9191
}
9292

@@ -98,8 +98,8 @@ func (s *PresetsTestSuite) TestCheckPresets() {
9898
func (s *PresetsTestSuite) TestCheckPresetsInvalid() {
9999
p := presets{
100100
"test": urlOptions{
101-
"resize": []string{"fit", "-1", "-2"},
102-
"sharpen": []string{"2"},
101+
urlOption{Name: "resize", Args: []string{"fit", "-1", "-2"}},
102+
urlOption{Name: "sharpen", Args: []string{"2"}},
103103
},
104104
}
105105

processing_options.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ import (
1212
"strings"
1313
)
1414

15-
type urlOptions map[string][]string
15+
type urlOption struct {
16+
Name string
17+
Args []string
18+
}
19+
type urlOptions []urlOption
1620

1721
type processingHeaders struct {
1822
Accept string
@@ -713,8 +717,8 @@ func applyProcessingOption(po *processingOptions, name string, args []string) er
713717
}
714718

715719
func applyProcessingOptions(po *processingOptions, options urlOptions) error {
716-
for name, args := range options {
717-
if err := applyProcessingOption(po, name, args); err != nil {
720+
for _, opt := range options {
721+
if err := applyProcessingOption(po, opt.Name, opt.Args); err != nil {
718722
return err
719723
}
720724
}
@@ -723,7 +727,7 @@ func applyProcessingOptions(po *processingOptions, options urlOptions) error {
723727
}
724728

725729
func parseURLOptions(opts []string) (urlOptions, []string) {
726-
parsed := make(urlOptions)
730+
parsed := make(urlOptions, 0, len(opts))
727731
urlStart := len(opts) + 1
728732

729733
for i, opt := range opts {
@@ -734,7 +738,7 @@ func parseURLOptions(opts []string) (urlOptions, []string) {
734738
break
735739
}
736740

737-
parsed[args[0]] = args[1:]
741+
parsed = append(parsed, urlOption{Name: args[0], Args: args[1:]})
738742
}
739743

740744
var rest []string

processing_options_test.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -335,12 +335,12 @@ func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedWatermark() {
335335

336336
func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedPreset() {
337337
conf.Presets["test1"] = urlOptions{
338-
"resizing_type": []string{"fill"},
338+
urlOption{Name: "resizing_type", Args: []string{"fill"}},
339339
}
340340

341341
conf.Presets["test2"] = urlOptions{
342-
"blur": []string{"0.2"},
343-
"quality": []string{"50"},
342+
urlOption{Name: "blur", Args: []string{"0.2"}},
343+
urlOption{Name: "quality", Args: []string{"50"}},
344344
}
345345

346346
req := s.getRequest("http://example.com/unsafe/preset:test1:test2/plain/http://images.dev/lorem/ipsum.jpg")
@@ -356,9 +356,9 @@ func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedPreset() {
356356

357357
func (s *ProcessingOptionsTestSuite) TestParsePathPresetDefault() {
358358
conf.Presets["default"] = urlOptions{
359-
"resizing_type": []string{"fill"},
360-
"blur": []string{"0.2"},
361-
"quality": []string{"50"},
359+
urlOption{Name: "resizing_type", Args: []string{"fill"}},
360+
urlOption{Name: "blur", Args: []string{"0.2"}},
361+
urlOption{Name: "quality", Args: []string{"50"}},
362362
}
363363

364364
req := s.getRequest("http://example.com/unsafe/quality:70/plain/http://images.dev/lorem/ipsum.jpg")
@@ -374,12 +374,12 @@ func (s *ProcessingOptionsTestSuite) TestParsePathPresetDefault() {
374374

375375
func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedPresetLoopDetection() {
376376
conf.Presets["test1"] = urlOptions{
377-
"resizing_type": []string{"fill"},
377+
urlOption{Name: "resizing_type", Args: []string{"fill"}},
378378
}
379379

380380
conf.Presets["test2"] = urlOptions{
381-
"blur": []string{"0.2"},
382-
"quality": []string{"50"},
381+
urlOption{Name: "blur", Args: []string{"0.2"}},
382+
urlOption{Name: "quality", Args: []string{"50"}},
383383
}
384384

385385
req := s.getRequest("http://example.com/unsafe/preset:test1:test2:test1/plain/http://images.dev/lorem/ipsum.jpg")
@@ -553,10 +553,10 @@ func (s *ProcessingOptionsTestSuite) TestParsePathSignedInvalid() {
553553
func (s *ProcessingOptionsTestSuite) TestParsePathOnlyPresets() {
554554
conf.OnlyPresets = true
555555
conf.Presets["test1"] = urlOptions{
556-
"blur": []string{"0.2"},
556+
urlOption{Name: "blur", Args: []string{"0.2"}},
557557
}
558558
conf.Presets["test2"] = urlOptions{
559-
"quality": []string{"50"},
559+
urlOption{Name: "quality", Args: []string{"50"}},
560560
}
561561

562562
req := s.getRequest("http://example.com/unsafe/test1:test2/plain/http://images.dev/lorem/ipsum.jpg")
@@ -573,10 +573,10 @@ func (s *ProcessingOptionsTestSuite) TestParsePathOnlyPresets() {
573573
func (s *ProcessingOptionsTestSuite) TestParseBase64URLOnlyPresets() {
574574
conf.OnlyPresets = true
575575
conf.Presets["test1"] = urlOptions{
576-
"blur": []string{"0.2"},
576+
urlOption{Name: "blur", Args: []string{"0.2"}},
577577
}
578578
conf.Presets["test2"] = urlOptions{
579-
"quality": []string{"50"},
579+
urlOption{Name: "quality", Args: []string{"50"}},
580580
}
581581

582582
imageURL := "http://images.dev/lorem/ipsum.jpg?param=value"

0 commit comments

Comments
 (0)