From ee54296249fb2993c4ff00890e33abc58a38752c Mon Sep 17 00:00:00 2001 From: DarthSim Date: Mon, 22 Jul 2019 17:27:18 +0600 Subject: [PATCH] Fix options rewrite --- presets_test.go | 12 ++++++------ processing_options.go | 14 +++++++++----- processing_options_test.go | 26 +++++++++++++------------- 3 files changed, 28 insertions(+), 24 deletions(-) diff --git a/presets_test.go b/presets_test.go index 673bbd42..058eab6b 100644 --- a/presets_test.go +++ b/presets_test.go @@ -19,8 +19,8 @@ func (s *PresetsTestSuite) TestParsePreset() { require.Nil(s.T(), err) assert.Equal(s.T(), urlOptions{ - "resize": []string{"fit", "100", "200"}, - "sharpen": []string{"2"}, + urlOption{Name: "resize", Args: []string{"fit", "100", "200"}}, + urlOption{Name: "sharpen", Args: []string{"2"}}, }, p["test"]) } @@ -85,8 +85,8 @@ func (s *PresetsTestSuite) TestParsePresetComment() { func (s *PresetsTestSuite) TestCheckPresets() { p := presets{ "test": urlOptions{ - "resize": []string{"fit", "100", "200"}, - "sharpen": []string{"2"}, + urlOption{Name: "resize", Args: []string{"fit", "100", "200"}}, + urlOption{Name: "sharpen", Args: []string{"2"}}, }, } @@ -98,8 +98,8 @@ func (s *PresetsTestSuite) TestCheckPresets() { func (s *PresetsTestSuite) TestCheckPresetsInvalid() { p := presets{ "test": urlOptions{ - "resize": []string{"fit", "-1", "-2"}, - "sharpen": []string{"2"}, + urlOption{Name: "resize", Args: []string{"fit", "-1", "-2"}}, + urlOption{Name: "sharpen", Args: []string{"2"}}, }, } diff --git a/processing_options.go b/processing_options.go index cf8da05f..621f0483 100644 --- a/processing_options.go +++ b/processing_options.go @@ -12,7 +12,11 @@ import ( "strings" ) -type urlOptions map[string][]string +type urlOption struct { + Name string + Args []string +} +type urlOptions []urlOption type processingHeaders struct { Accept string @@ -713,8 +717,8 @@ func applyProcessingOption(po *processingOptions, name string, args []string) er } func applyProcessingOptions(po *processingOptions, options urlOptions) error { - for name, args := range options { - if err := applyProcessingOption(po, name, args); err != nil { + for _, opt := range options { + if err := applyProcessingOption(po, opt.Name, opt.Args); err != nil { return err } } @@ -723,7 +727,7 @@ func applyProcessingOptions(po *processingOptions, options urlOptions) error { } func parseURLOptions(opts []string) (urlOptions, []string) { - parsed := make(urlOptions) + parsed := make(urlOptions, 0, len(opts)) urlStart := len(opts) + 1 for i, opt := range opts { @@ -734,7 +738,7 @@ func parseURLOptions(opts []string) (urlOptions, []string) { break } - parsed[args[0]] = args[1:] + parsed = append(parsed, urlOption{Name: args[0], Args: args[1:]}) } var rest []string diff --git a/processing_options_test.go b/processing_options_test.go index 7db787a5..12dda430 100644 --- a/processing_options_test.go +++ b/processing_options_test.go @@ -335,12 +335,12 @@ func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedWatermark() { func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedPreset() { conf.Presets["test1"] = urlOptions{ - "resizing_type": []string{"fill"}, + urlOption{Name: "resizing_type", Args: []string{"fill"}}, } conf.Presets["test2"] = urlOptions{ - "blur": []string{"0.2"}, - "quality": []string{"50"}, + urlOption{Name: "blur", Args: []string{"0.2"}}, + urlOption{Name: "quality", Args: []string{"50"}}, } 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() { func (s *ProcessingOptionsTestSuite) TestParsePathPresetDefault() { conf.Presets["default"] = urlOptions{ - "resizing_type": []string{"fill"}, - "blur": []string{"0.2"}, - "quality": []string{"50"}, + urlOption{Name: "resizing_type", Args: []string{"fill"}}, + urlOption{Name: "blur", Args: []string{"0.2"}}, + urlOption{Name: "quality", Args: []string{"50"}}, } req := s.getRequest("http://example.com/unsafe/quality:70/plain/http://images.dev/lorem/ipsum.jpg") @@ -374,12 +374,12 @@ func (s *ProcessingOptionsTestSuite) TestParsePathPresetDefault() { func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedPresetLoopDetection() { conf.Presets["test1"] = urlOptions{ - "resizing_type": []string{"fill"}, + urlOption{Name: "resizing_type", Args: []string{"fill"}}, } conf.Presets["test2"] = urlOptions{ - "blur": []string{"0.2"}, - "quality": []string{"50"}, + urlOption{Name: "blur", Args: []string{"0.2"}}, + urlOption{Name: "quality", Args: []string{"50"}}, } 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() { func (s *ProcessingOptionsTestSuite) TestParsePathOnlyPresets() { conf.OnlyPresets = true conf.Presets["test1"] = urlOptions{ - "blur": []string{"0.2"}, + urlOption{Name: "blur", Args: []string{"0.2"}}, } conf.Presets["test2"] = urlOptions{ - "quality": []string{"50"}, + urlOption{Name: "quality", Args: []string{"50"}}, } req := s.getRequest("http://example.com/unsafe/test1:test2/plain/http://images.dev/lorem/ipsum.jpg") @@ -573,10 +573,10 @@ func (s *ProcessingOptionsTestSuite) TestParsePathOnlyPresets() { func (s *ProcessingOptionsTestSuite) TestParseBase64URLOnlyPresets() { conf.OnlyPresets = true conf.Presets["test1"] = urlOptions{ - "blur": []string{"0.2"}, + urlOption{Name: "blur", Args: []string{"0.2"}}, } conf.Presets["test2"] = urlOptions{ - "quality": []string{"50"}, + urlOption{Name: "quality", Args: []string{"50"}}, } imageURL := "http://images.dev/lorem/ipsum.jpg?param=value"