55 lines
1.1 KiB
Go
55 lines
1.1 KiB
Go
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
|
|
}
|