2023-07-22 19:12:53 +00:00
|
|
|
package forms
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-02-13 20:43:52 +00:00
|
|
|
"owl-blogs/domain/model"
|
2023-07-22 19:12:53 +00:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2024-02-13 20:43:52 +00:00
|
|
|
type DefaultForm struct{}
|
|
|
|
|
|
|
|
func (meta *DefaultForm) Form(binSvc model.BinaryStorageInterface) string {
|
|
|
|
form := NewForm(meta, nil)
|
|
|
|
htmlForm, _ := form.HtmlForm()
|
|
|
|
return htmlForm
|
|
|
|
}
|
|
|
|
|
|
|
|
func (meta *DefaultForm) ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) (model.EntryMetaData, error) {
|
|
|
|
form := NewForm(meta, binSvc)
|
|
|
|
return form.Parse(data)
|
2023-07-22 19:12:53 +00:00
|
|
|
}
|
|
|
|
|
2024-02-13 20:43:52 +00:00
|
|
|
type Form[T interface{}] struct {
|
|
|
|
data T
|
|
|
|
binSvc model.BinaryStorageInterface
|
2023-07-22 19:12:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type FormFieldParams struct {
|
|
|
|
InputType string
|
|
|
|
Widget string
|
|
|
|
}
|
|
|
|
|
|
|
|
type FormField struct {
|
|
|
|
Name string
|
2023-08-05 19:24:26 +00:00
|
|
|
Value reflect.Value
|
2023-07-22 19:12:53 +00:00
|
|
|
Params FormFieldParams
|
|
|
|
}
|
|
|
|
|
2024-02-13 20:43:52 +00:00
|
|
|
func NewForm[T interface{}](data T, binaryService model.BinaryStorageInterface) *Form[T] {
|
|
|
|
return &Form[T]{
|
2023-07-22 19:12:53 +00:00
|
|
|
data: data,
|
|
|
|
binSvc: binaryService,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FormFieldParams) ApplyTag(tagKey string, tagValue string) error {
|
|
|
|
switch tagKey {
|
|
|
|
case "inputType":
|
|
|
|
s.InputType = tagValue
|
|
|
|
case "widget":
|
|
|
|
s.Widget = tagValue
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown tag key: %v", tagKey)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-05 19:24:26 +00:00
|
|
|
func (s *FormField) ToWidget() Widget {
|
|
|
|
switch s.Params.Widget {
|
|
|
|
case "textarea":
|
|
|
|
return &TextareaWidget{*s}
|
|
|
|
case "textlist":
|
|
|
|
return &TextListWidget{*s}
|
2023-08-10 19:56:06 +00:00
|
|
|
case "password":
|
|
|
|
return &PasswordWidget{*s}
|
2024-02-13 20:43:52 +00:00
|
|
|
case "text":
|
2023-08-05 19:24:26 +00:00
|
|
|
return &TextWidget{*s}
|
2024-02-13 20:43:52 +00:00
|
|
|
default:
|
|
|
|
return &OmitWidget{*s}
|
2023-08-05 19:24:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-22 19:12:53 +00:00
|
|
|
func (s *FormField) Html() string {
|
|
|
|
html := ""
|
|
|
|
html += fmt.Sprintf("<label for=\"%v\">%v</label>\n", s.Name, s.Name)
|
2023-08-05 19:24:26 +00:00
|
|
|
if s.Params.InputType == "file" {
|
2023-07-22 19:12:53 +00:00
|
|
|
html += fmt.Sprintf("<input type=\"%v\" name=\"%v\" id=\"%v\" value=\"%v\" />\n", s.Params.InputType, s.Name, s.Name, s.Value)
|
2023-08-05 19:24:26 +00:00
|
|
|
} else {
|
|
|
|
html += s.ToWidget().Html()
|
|
|
|
html += "\n"
|
2023-07-22 19:12:53 +00:00
|
|
|
}
|
|
|
|
return html
|
|
|
|
}
|
|
|
|
|
2023-08-05 19:24:26 +00:00
|
|
|
func FieldToFormField(field reflect.StructField, value reflect.Value) (FormField, error) {
|
2023-07-22 19:12:53 +00:00
|
|
|
formField := FormField{
|
|
|
|
Name: field.Name,
|
|
|
|
Value: value,
|
|
|
|
Params: FormFieldParams{},
|
|
|
|
}
|
|
|
|
tag := field.Tag.Get("owl")
|
|
|
|
for _, param := range strings.Split(tag, " ") {
|
|
|
|
parts := strings.Split(param, "=")
|
|
|
|
if len(parts) != 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err := formField.Params.ApplyTag(parts[0], parts[1])
|
|
|
|
if err != nil {
|
|
|
|
return FormField{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return formField, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func StructToFormFields(data interface{}) ([]FormField, error) {
|
|
|
|
dataValue := reflect.Indirect(reflect.ValueOf(data))
|
|
|
|
dataType := reflect.TypeOf(data).Elem()
|
|
|
|
numFields := dataType.NumField()
|
|
|
|
fields := []FormField{}
|
|
|
|
for i := 0; i < numFields; i++ {
|
2023-08-05 19:24:26 +00:00
|
|
|
field, err := FieldToFormField(
|
|
|
|
dataType.Field(i),
|
|
|
|
dataValue.FieldByIndex([]int{i}),
|
|
|
|
)
|
2023-07-22 19:12:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fields = append(fields, field)
|
|
|
|
}
|
|
|
|
return fields, nil
|
|
|
|
}
|
|
|
|
|
2024-02-13 20:43:52 +00:00
|
|
|
func (s *Form[T]) HtmlForm() (string, error) {
|
2023-07-22 19:12:53 +00:00
|
|
|
fields, err := StructToFormFields(s.data)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2023-08-05 08:14:05 +00:00
|
|
|
html := ""
|
2023-07-22 19:12:53 +00:00
|
|
|
for _, field := range fields {
|
|
|
|
html += field.Html()
|
|
|
|
}
|
|
|
|
|
|
|
|
return html, nil
|
|
|
|
}
|
|
|
|
|
2024-02-13 20:43:52 +00:00
|
|
|
func (s *Form[T]) Parse(ctx model.HttpFormData) (T, error) {
|
|
|
|
var empty T
|
|
|
|
|
2023-07-22 19:12:53 +00:00
|
|
|
if ctx == nil {
|
2024-02-13 20:43:52 +00:00
|
|
|
return empty, fmt.Errorf("nil context")
|
2023-07-22 19:12:53 +00:00
|
|
|
}
|
|
|
|
dataVal := reflect.ValueOf(s.data)
|
|
|
|
if dataVal.Kind() != reflect.Ptr {
|
2024-02-13 20:43:52 +00:00
|
|
|
return empty, fmt.Errorf("meta data is not a pointer")
|
2023-07-22 19:12:53 +00:00
|
|
|
}
|
|
|
|
fields, err := StructToFormFields(s.data)
|
|
|
|
if err != nil {
|
2024-02-13 20:43:52 +00:00
|
|
|
return empty, err
|
2023-07-22 19:12:53 +00:00
|
|
|
}
|
|
|
|
for _, field := range fields {
|
|
|
|
fieldName := field.Name
|
|
|
|
|
|
|
|
if field.Params.InputType == "file" {
|
|
|
|
file, err := ctx.FormFile(fieldName)
|
|
|
|
if err != nil {
|
2023-07-25 19:31:03 +00:00
|
|
|
// If field already has a value, we can ignore the error
|
2023-08-05 19:24:26 +00:00
|
|
|
if field.Value != reflect.Zero(field.Value.Type()) {
|
2023-07-25 19:31:03 +00:00
|
|
|
metaField := dataVal.Elem().FieldByName(fieldName)
|
|
|
|
if metaField.IsValid() {
|
2023-08-05 19:24:26 +00:00
|
|
|
metaField.SetString(field.Value.String())
|
2023-07-25 19:31:03 +00:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2024-02-13 20:43:52 +00:00
|
|
|
return empty, err
|
2023-07-22 19:12:53 +00:00
|
|
|
}
|
|
|
|
fileData, err := file.Open()
|
|
|
|
if err != nil {
|
2024-02-13 20:43:52 +00:00
|
|
|
return empty, err
|
2023-07-22 19:12:53 +00:00
|
|
|
}
|
|
|
|
defer fileData.Close()
|
|
|
|
fileBytes := make([]byte, file.Size)
|
|
|
|
_, err = fileData.Read(fileBytes)
|
|
|
|
if err != nil {
|
2024-02-13 20:43:52 +00:00
|
|
|
return empty, err
|
2023-07-22 19:12:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
binaryFile, err := s.binSvc.Create(file.Filename, fileBytes)
|
|
|
|
if err != nil {
|
2024-02-13 20:43:52 +00:00
|
|
|
return empty, err
|
2023-07-22 19:12:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
metaField := dataVal.Elem().FieldByName(fieldName)
|
|
|
|
if metaField.IsValid() {
|
|
|
|
metaField.SetString(binaryFile.Id)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
formValue := ctx.FormValue(fieldName)
|
|
|
|
metaField := dataVal.Elem().FieldByName(fieldName)
|
|
|
|
if metaField.IsValid() {
|
2023-08-05 19:24:26 +00:00
|
|
|
field.ToWidget().ParseValue(formValue, metaField)
|
2023-07-22 19:12:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.data, nil
|
|
|
|
}
|