-
Notifications
You must be signed in to change notification settings - Fork 1
/
form_control_ref.go
76 lines (67 loc) · 2.25 KB
/
form_control_ref.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package domain
import (
"encoding/json"
"errors"
"fmt"
logger "github.com/sirupsen/logrus"
)
const (
errInvalidFormControl = "form control struct validate failed for form control name [%s], with error : [%s]"
)
type FormControlRefList struct {
Count int32 `json:"count"`
FormControlRefList []*FormControlRef `json:"formControlList"`
}
type FormControlRef struct {
Id int64 `json:"id,omitempty"`
TargetRef string `json:"targetRef" validate:"required"`
SortOrder int32 `json:"sortOrder" validate:"required"`
Description string `json:"description" validate:"required"`
Name string `json:"name" validate:"required"`
Key string `json:"key" validate:"required"`
Value string `json:"value" validate:"required"`
FormControlId int64 `json:"formControlId" validate:"required"`
FormControl FormControl `json:"formControl,omitempty"`
Audit *Audit `json:"audit,omitempty"`
Ref string `json:"ref,omitempty"`
Version int64 `json:"version,omitempty"`
}
func NewFormControlRef() *FormControlRef {
return &FormControlRef{
Ref: NewRef(FormControlRefRefType),
}
}
func (o *FormControlRef) ValidateStringParams(paramName string, errString string) error {
if paramName == "" {
logger.Error(errors.New(errString))
return errors.New(errString)
}
return nil
}
func (o *FormControlRef) BuildFormControlRefForCreate(formControl FormControl, ref string, value string) error {
err := formControl.Validate()
if err != nil {
validateErr := errors.New(fmt.Sprintf(errInvalidFormControl, formControl.Name, err))
logger.Error(validateErr)
return validateErr
}
err = json.Unmarshal([]byte(formControl.Control), &formControl.ControlJSON)
if err != nil {
logger.Errorf("failed to unmarshall formControl.Control json with error : [%s]", err)
return err
}
o.TargetRef = ref
o.SortOrder = 1
o.Description = formControl.Description
o.Name = formControl.Name
o.Key = formControl.ControlJSON.Key
o.Value = value
o.FormControl = formControl
o.FormControlId = formControl.Id
err = validate.Struct(o)
if err != nil {
logger.Errorf("form control ref struct validate failed with error : [%s]", err)
return err
}
return nil
}