add Block + Component, add TextComponent with min, max and regex validators and early NumberComponent
This commit is contained in:
54
internal/block/number_component.go
Normal file
54
internal/block/number_component.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package block
|
||||
|
||||
import (
|
||||
"blocky/internal/block/validation"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
type NumberComponent struct {
|
||||
identifier string
|
||||
name string
|
||||
validators []validation.Validator
|
||||
}
|
||||
|
||||
func (t NumberComponent) Type() ComponentType {
|
||||
return NumberComponentType
|
||||
}
|
||||
|
||||
func (t NumberComponent) Name() string {
|
||||
return t.name
|
||||
}
|
||||
|
||||
func (t NumberComponent) Identifier() string {
|
||||
return t.identifier
|
||||
}
|
||||
|
||||
func (t NumberComponent) Validators() []validation.Validator {
|
||||
return t.validators
|
||||
}
|
||||
|
||||
func NewNumberComponent(identifier, name string, opts ...validation.ComponentOption) NumberComponent {
|
||||
cfg := &validation.ComponentConfig{}
|
||||
for _, opt := range opts {
|
||||
opt(cfg)
|
||||
}
|
||||
return NumberComponent{identifier: identifier, name: name, validators: cfg.Validators}
|
||||
}
|
||||
|
||||
func (t NumberComponent) NewInstance() ComponentInstance {
|
||||
return &NumberComponentInstance{}
|
||||
}
|
||||
|
||||
type NumberComponentInstance struct {
|
||||
value int64
|
||||
}
|
||||
|
||||
func (t *NumberComponentInstance) FromJSON(component Component, data json.RawMessage) error {
|
||||
var s int64
|
||||
err := json.Unmarshal(data, &s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
t.value = s
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user