From 311393f397c5cde611ea6d24a52683e0669dc79e Mon Sep 17 00:00:00 2001 From: Shion Ichikawa Date: Sun, 26 May 2024 23:36:54 +0900 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20(model/question)=20move=20?= =?UTF-8?q?ImageSpecs=20to=20new=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../model/question/file_constraint_image.go | 111 --------------- .../question/file_constraint_image_spec.go | 130 ++++++++++++++++++ 2 files changed, 130 insertions(+), 111 deletions(-) create mode 100644 svc/pkg/domain/model/question/file_constraint_image_spec.go diff --git a/svc/pkg/domain/model/question/file_constraint_image.go b/svc/pkg/domain/model/question/file_constraint_image.go index b734238..c9b7c60 100644 --- a/svc/pkg/domain/model/question/file_constraint_image.go +++ b/svc/pkg/domain/model/question/file_constraint_image.go @@ -9,17 +9,6 @@ import ( ) type ( - DimensionSpec struct { - Min, Max *int - Eq *int - } - - // RatioSpec represents the ratio of width / height - RatioSpec struct { - Min, Max *float32 - Eq *float32 - } - ImageFileConstraint struct { Ratio RatioSpec MinNumber *int @@ -272,76 +261,6 @@ func ImportImageFileConstraint(c map[string]interface{}) (*ImageFileConstraint, return NewImageFileConstraint(minNumber, maxNumber, width, height, ratio) } -func loadDimensionSpec(c map[string]interface{}, key string) (DimensionSpec, error) { - target := DimensionSpec{} - t, has := c[key] - if !has { - return DimensionSpec{}, nil - } - m, ok := t.(map[string]interface{}) - if !ok { - return DimensionSpec{}, errors.New("invalid dimension spec") - } - if eqR, has := m[FileImageConstraintDimensionEq]; has && eqR != nil { - eqV, ok := eqR.(int) - if !ok { - return DimensionSpec{}, errors.New("invalid eq dimension") - } - target.Eq = &eqV - } else { - if minR, has := m[FileImageConstraintDimensionMin]; has && minR != nil { - minV, ok := minR.(int) - if !ok { - return DimensionSpec{}, errors.New("invalid min dimension") - } - target.Min = &minV - } - if maxR, has := m[FileImageConstraintDimensionMax]; has && maxR != nil { - maxV, ok := maxR.(int) - if !ok { - return DimensionSpec{}, errors.New("invalid max dimension") - } - target.Max = &maxV - } - } - return target, nil -} - -func loadRatioSpec(c map[string]interface{}, key string) (RatioSpec, error) { - target := RatioSpec{} - t, has := c[key] - if !has { - return RatioSpec{}, nil - } - m, ok := t.(map[string]interface{}) - if !ok { - return RatioSpec{}, errors.New("invalid ratio spec") - } - if eqR, has := m[FileImageConstraintRatioEq]; has && eqR != nil { - eqV, ok := eqR.(float32) - if !ok { - return RatioSpec{}, errors.New("invalid eq ratio") - } - target.Eq = &eqV - } else { - if minR, has := m[FileImageConstraintRatioMin]; has && minR != nil { - minV, ok := minR.(float32) - if !ok { - return RatioSpec{}, errors.New("invalid min ratio") - } - target.Min = &minV - } - if maxR, has := m[FileImageConstraintRatioMax]; has && maxR != nil { - maxV, ok := maxR.(float32) - if !ok { - return RatioSpec{}, errors.New("invalid max ratio") - } - target.Max = &maxV - } - } - return target, nil -} - func loadInt(t map[string]interface{}, key string) (*int, error) { v, has := t[key] if !has { @@ -368,33 +287,3 @@ func (c ImageFileConstraint) Export() map[string]interface{} { result[FileImageConstraintHeight] = heightC return result } - -func (s DimensionSpec) Export() map[string]interface{} { - result := map[string]interface{}{} - if s.Eq != nil { - result[FileImageConstraintDimensionEq] = *s.Eq - } else { - if s.Min != nil { - result[FileImageConstraintDimensionMin] = *s.Min - } - if s.Max != nil { - result[FileImageConstraintDimensionMax] = *s.Max - } - } - return result -} - -func (s RatioSpec) Export() map[string]interface{} { - result := map[string]interface{}{} - if s.Eq != nil { - result[FileImageConstraintRatioEq] = *s.Eq - } else { - if s.Min != nil { - result[FileImageConstraintRatioMin] = *s.Min - } - if s.Max != nil { - result[FileImageConstraintRatioMax] = *s.Max - } - } - return result -} diff --git a/svc/pkg/domain/model/question/file_constraint_image_spec.go b/svc/pkg/domain/model/question/file_constraint_image_spec.go new file mode 100644 index 0000000..9b09741 --- /dev/null +++ b/svc/pkg/domain/model/question/file_constraint_image_spec.go @@ -0,0 +1,130 @@ +package question + +import "errors" + +type ( + DimensionSpec struct { + Min, Max *int + Eq *int + } + + // RatioSpec represents the ratio of width / height + RatioSpec struct { + Min, Max *float32 + Eq *float32 + } +) + +func NewDimensionSpec(min, max, eq *int) DimensionSpec { + if eq != nil { + return DimensionSpec{Eq: eq} + } + return DimensionSpec{Min: min, Max: max, Eq: nil} +} + +func NewRatioSpec(min, max, eq *float32) RatioSpec { + if eq != nil { + return RatioSpec{Eq: eq} + } + return RatioSpec{Min: min, Max: max, Eq: nil} +} + +func (s DimensionSpec) Export() map[string]interface{} { + result := map[string]interface{}{} + if s.Eq != nil { + result[FileImageConstraintDimensionEq] = *s.Eq + } else { + if s.Min != nil { + result[FileImageConstraintDimensionMin] = *s.Min + } + if s.Max != nil { + result[FileImageConstraintDimensionMax] = *s.Max + } + } + return result +} + +func (s RatioSpec) Export() map[string]interface{} { + result := map[string]interface{}{} + if s.Eq != nil { + result[FileImageConstraintRatioEq] = *s.Eq + } else { + if s.Min != nil { + result[FileImageConstraintRatioMin] = *s.Min + } + if s.Max != nil { + result[FileImageConstraintRatioMax] = *s.Max + } + } + return result +} + +func loadDimensionSpec(c map[string]interface{}, key string) (DimensionSpec, error) { + target := DimensionSpec{} + t, has := c[key] + if !has { + return DimensionSpec{}, nil + } + m, ok := t.(map[string]interface{}) + if !ok { + return DimensionSpec{}, errors.New("invalid dimension spec") + } + if eqR, has := m[FileImageConstraintDimensionEq]; has && eqR != nil { + eqV, ok := eqR.(int) + if !ok { + return DimensionSpec{}, errors.New("invalid eq dimension") + } + target.Eq = &eqV + } else { + if minR, has := m[FileImageConstraintDimensionMin]; has && minR != nil { + minV, ok := minR.(int) + if !ok { + return DimensionSpec{}, errors.New("invalid min dimension") + } + target.Min = &minV + } + if maxR, has := m[FileImageConstraintDimensionMax]; has && maxR != nil { + maxV, ok := maxR.(int) + if !ok { + return DimensionSpec{}, errors.New("invalid max dimension") + } + target.Max = &maxV + } + } + return target, nil +} + +func loadRatioSpec(c map[string]interface{}, key string) (RatioSpec, error) { + target := RatioSpec{} + t, has := c[key] + if !has { + return RatioSpec{}, nil + } + m, ok := t.(map[string]interface{}) + if !ok { + return RatioSpec{}, errors.New("invalid ratio spec") + } + if eqR, has := m[FileImageConstraintRatioEq]; has && eqR != nil { + eqV, ok := eqR.(float32) + if !ok { + return RatioSpec{}, errors.New("invalid eq ratio") + } + target.Eq = &eqV + } else { + if minR, has := m[FileImageConstraintRatioMin]; has && minR != nil { + minV, ok := minR.(float32) + if !ok { + return RatioSpec{}, errors.New("invalid min ratio") + } + target.Min = &minV + } + if maxR, has := m[FileImageConstraintRatioMax]; has && maxR != nil { + maxV, ok := maxR.(float32) + if !ok { + return RatioSpec{}, errors.New("invalid max ratio") + } + target.Max = &maxV + } + } + return target, nil +}