Default field mimic the expected type
This commit is contained in:
parent
7d51a88424
commit
909ef75b16
|
@ -23,8 +23,10 @@ package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
@ -133,7 +135,22 @@ func getSpecs(svcType reflect.Type) viewServiceSpec {
|
||||||
case "placeholder":
|
case "placeholder":
|
||||||
f.Placeholder = kv[1]
|
f.Placeholder = kv[1]
|
||||||
case "default":
|
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":
|
case "description":
|
||||||
f.Description = kv[1]
|
f.Description = kv[1]
|
||||||
case "choices":
|
case "choices":
|
||||||
|
|
|
@ -41,7 +41,7 @@ type Field struct {
|
||||||
Placeholder string `json:"placeholder,omitempty"`
|
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 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 holds the differents choices shown to the user in <select> tag.
|
||||||
Choices []string `json:"choices,omitempty"`
|
Choices []string `json:"choices,omitempty"`
|
||||||
|
|
Loading…
Reference in New Issue