Default field mimic the expected type

This commit is contained in:
nemunaire 2024-01-17 16:38:10 +01:00
parent 7d51a88424
commit 909ef75b16
2 changed files with 19 additions and 2 deletions

View File

@ -23,8 +23,10 @@ package api
import (
"fmt"
"log"
"net/http"
"reflect"
"strconv"
"strings"
"github.com/gin-gonic/gin"
@ -133,7 +135,22 @@ func getSpecs(svcType reflect.Type) viewServiceSpec {
case "placeholder":
f.Placeholder = kv[1]
case "default":
f.Default = kv[1]
var err error
if strings.HasPrefix(f.Type, "uint") {
f.Default, err = strconv.ParseUint(kv[1], 10, 64)
} else if strings.HasPrefix(f.Type, "int") {
f.Default, err = strconv.ParseInt(kv[1], 10, 64)
} else if strings.HasPrefix(f.Type, "float") {
f.Default, err = strconv.ParseFloat(kv[1], 64)
} else if strings.HasPrefix(f.Type, "bool") {
f.Default, err = strconv.ParseBool(kv[1])
} else {
f.Default = kv[1]
}
if err != nil {
log.Printf("Format error for default field %s of type %s definition: %s", svcType.Field(i).Name, svcType.Name(), err.Error())
}
case "description":
f.Description = kv[1]
case "choices":

View File

@ -41,7 +41,7 @@ type Field struct {
Placeholder string `json:"placeholder,omitempty"`
// Default is the preselected value or the default value in case the field is not filled by the user.
Default string `json:"default,omitempty"`
Default interface{} `json:"default,omitempty"`
// Choices holds the differents choices shown to the user in <select> tag.
Choices []string `json:"choices,omitempty"`