Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FileQuestion / ImageConstraintの実装改築 #86

Merged
merged 14 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions svc/pkg/domain/model/question/checkbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
return NewCheckBoxQuestion(q.ID, q.Text, options, optionsOrder, q.FormID), nil
}

func (q CheckBoxQuestion) Export() StandardQuestion {
func (q CheckBoxQuestion) Export() (*StandardQuestion, error) {

Check warning on line 96 in svc/pkg/domain/model/question/checkbox.go

View check run for this annotation

Codecov / codecov/patch

svc/pkg/domain/model/question/checkbox.go#L96

Added line #L96 was not covered by tests
customs := make(map[string]interface{})
options := make(map[string]string, len(q.Options))
for _, option := range q.Options {
Expand All @@ -105,7 +105,7 @@
}
customs[CheckBoxOptionsField] = options
customs[CheckBoxOptionsOrderField] = optionsOrder
return NewStandardQuestion(TypeCheckBox, q.ID, q.FormID, q.Text, customs)
return NewStandardQuestion(TypeCheckBox, q.ID, q.FormID, q.Text, customs), nil

Check warning on line 108 in svc/pkg/domain/model/question/checkbox.go

View check run for this annotation

Codecov / codecov/patch

svc/pkg/domain/model/question/checkbox.go#L108

Added line #L108 was not covered by tests
}

func (o CheckboxOptionsOrder) GetOrderedIDs() []CheckBoxOptionID {
Expand Down
99 changes: 42 additions & 57 deletions svc/pkg/domain/model/question/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,100 +9,85 @@
type (
FileQuestion struct {
Basic
FileType FileType
Constraint FileConstraint
FileTypes FileTypes
ImageFileConstraint
}
FileTypes struct {
AcceptAny bool
AcceptImage bool
AcceptPDF bool
}
FileType int
)

const (
Image FileType = 1
PDF FileType = 2
Any FileType = 3
FileQuestionFileTypeField = "fileType"
FileConstraintsCustomsField = "fileConstraint"
FileQuestionFileTypeField = "fileTypes"
FileImageConstraintField = "img_c"
)

func (t FileType) String() string {
switch t {
case Image:
return "image"
case PDF:
return "pdf"
case Any:
return "any"
default:
return "unknown"
}
}

func NewFileQuestion(
id id.QuestionID, text string, fileType FileType, constraint FileConstraint, formID id.FormID,
id id.QuestionID, text string, fileTypes FileTypes,
imgConstraint ImageFileConstraint,
formID id.FormID,
) *FileQuestion {
return &FileQuestion{
Basic: NewBasic(id, text, TypeFile, formID),
FileType: fileType,
Constraint: constraint,
}
}

func NewFileType(v int) (FileType, error) {
switch FileType(v) {
case Image, PDF, Any:
return FileType(v), nil
Basic: NewBasic(id, text, TypeFile, formID),
FileTypes: fileTypes,
ImageFileConstraint: imgConstraint,
}
return 0, errors.New("invalid file type")
}

func ImportFileQuestion(q StandardQuestion) (*FileQuestion, error) {
// check if customs has "fileType" as int, return error if not
// expect custom field has fileTypes as []bool
// [AcceptAny, AcceptImage, AcceptPDF]
fileTypeDataI, has := q.Customs[FileQuestionFileTypeField]
if !has {
return nil, errors.New(
fmt.Sprintf("\"%s\" is required for FileQuestion", FileQuestionFileTypeField))
}
fileTypeData, ok := fileTypeDataI.(int64)
fileTypeData, ok := fileTypeDataI.([]bool)
if !ok {
return nil, errors.New(
fmt.Sprintf("\"%s\" must be int for FileQuestion", FileQuestionFileTypeField))
}
fileType, err := NewFileType(int(fileTypeData))
if err != nil {
return nil, err

// extend length if not enough
if len(fileTypeData) < 3 {
for i := len(fileTypeData); i < 3; i++ {
fileTypeData = append(fileTypeData, false)

Check warning on line 56 in svc/pkg/domain/model/question/file.go

View check run for this annotation

Codecov / codecov/patch

svc/pkg/domain/model/question/file.go#L55-L56

Added lines #L55 - L56 were not covered by tests
}
}

if fileType == Any {
return NewFileQuestion(q.ID, q.Text, fileType, nil, q.FormID), nil
fileTypes := FileTypes{
AcceptAny: fileTypeData[0],
AcceptImage: fileTypeData[1],
AcceptPDF: fileTypeData[2],
}

constraintsCustomsData, has := q.Customs[FileConstraintsCustomsField]
// if FileConstraintsCustomsField is not present, return FileQuestion without constraint
imgConstraintCustomR, has := q.Customs[FileImageConstraintField]
//if FileConstraintsCustomsField is not present, return FileQuestion without constraint
if !has {
return NewFileQuestion(q.ID, q.Text, fileType, nil, q.FormID), nil
return NewFileQuestion(q.ID, q.Text, fileTypes, ImageFileConstraint{}, q.FormID), nil

Check warning on line 69 in svc/pkg/domain/model/question/file.go

View check run for this annotation

Codecov / codecov/patch

svc/pkg/domain/model/question/file.go#L69

Added line #L69 was not covered by tests
}

constraintsCustoms, ok := constraintsCustomsData.(map[string]interface{})
// if FileConstraintsCustomsField Found, but it is not map[string]interface{}, return error
imgConstraintCustom, ok := imgConstraintCustomR.(map[string]interface{})
//if FileConstraintsCustomsField Found, but it is not slice, return error
if !ok {
return nil, errors.New(
fmt.Sprintf("\"%s\" must be map[string]interface{} for FileQuestion", FileConstraintsCustomsField))
fmt.Sprintf("\"%s\" must be map[string]interface{} for FileQuestion", FileImageConstraintField))

Check warning on line 76 in svc/pkg/domain/model/question/file.go

View check run for this annotation

Codecov / codecov/patch

svc/pkg/domain/model/question/file.go#L76

Added line #L76 was not covered by tests
}

constraint := NewStandardFileConstraint(fileType, constraintsCustoms)
question := NewFileQuestion(q.ID, q.Text, fileType, ImportFileConstraint(constraint), q.FormID)
imgConstraint, err := ImportImageFileConstraint(imgConstraintCustom)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to import ImageFileConstraint: %w", err)

Check warning on line 80 in svc/pkg/domain/model/question/file.go

View check run for this annotation

Codecov / codecov/patch

svc/pkg/domain/model/question/file.go#L80

Added line #L80 was not covered by tests
}
question := NewFileQuestion(q.ID, q.Text, fileTypes, *imgConstraint, q.FormID)
return question, nil
}

func (q FileQuestion) Export() StandardQuestion {
func (q FileQuestion) Export() (*StandardQuestion, error) {
customs := make(map[string]interface{})

customs[FileQuestionFileTypeField] = q.FileType

if q.Constraint != nil {
customs[FileConstraintsCustomsField] = q.Constraint.Export().Customs
}
return NewStandardQuestion(TypeFile, q.ID, q.FormID, q.Text, customs)
qt := []bool{q.FileTypes.AcceptAny, q.FileTypes.AcceptImage, q.FileTypes.AcceptPDF}
customs[FileQuestionFileTypeField] = qt
customs[FileImageConstraintField] = q.ImageFileConstraint.Export()
return NewStandardQuestion(TypeFile, q.ID, q.FormID, q.Text, customs), nil
}
23 changes: 7 additions & 16 deletions svc/pkg/domain/model/question/file_constraint.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package question

type (
FileType int
StandardFileConstraint struct {
Type FileType
Customs map[string]interface{}
}
FileConstraint interface {
GetFileType() FileType
GetExtensions() []Extension
Export() StandardFileConstraint
Export() (*StandardFileConstraint, error)
ValidateFiles(file []File) error
}
File struct {
Expand All @@ -18,18 +19,8 @@ type (
Extension string
)

func NewStandardFileConstraint(fileType FileType, customs map[string]interface{}) StandardFileConstraint {
return StandardFileConstraint{
Type: fileType,
Customs: customs,
}
}

func ImportFileConstraint(standard StandardFileConstraint) FileConstraint {
switch standard.Type {
case Image:
return ImportImageFileConstraint(standard)
default:
return nil
}
}
const (
Image FileType = 1
PDF FileType = 2
FileTypeCustomField = "type"
)
Loading
Loading