add Int64Component

This commit is contained in:
2026-02-12 20:34:32 +00:00
parent 6099538fa8
commit 08682c35c1
7 changed files with 141 additions and 75 deletions

View File

@@ -0,0 +1,89 @@
package int64_component
import (
"blocky/internal/block"
"blocky/internal/block/validation"
"encoding/json"
"fmt"
)
type Int64Component struct {
identifier string
name string
validators []validation.Validator
Int64Validators []Int64Validator
}
func (t Int64Component) Type() block.ComponentType {
return block.Int64ComponentType
}
func (t Int64Component) Name() string {
return t.name
}
func (t Int64Component) Identifier() string {
return t.identifier
}
func (t Int64Component) Validators() []validation.Validator {
return t.validators
}
func NewInt64Component(identifier, name string, validators []validation.ComponentOption, int64Validators []Int64ComponentOption) Int64Component {
cfg := &validation.ComponentConfig{}
for _, opt := range validators {
opt(cfg)
}
int64Cfg := &Int64ComponentConfig{}
for _, opt := range int64Validators {
opt(int64Cfg)
}
return Int64Component{identifier: identifier, name: name, validators: cfg.Validators, Int64Validators: int64Cfg.Validators}
}
func (t Int64Component) NewInstance() block.ComponentInstance {
return &Int64ComponentInstance{}
}
type Int64ComponentInstance struct {
value int64
}
func (t *Int64ComponentInstance) Value() interface{} {
return t.value
}
func (t *Int64ComponentInstance) FromJSON(component block.Component, data json.RawMessage) error {
var s int64
err := json.Unmarshal(data, &s)
if err != nil {
return fmt.Errorf("Error unmarshalling property %s: %w", component.Identifier(), err)
}
t.value = s
for _, v := range component.Validators() {
if err := v(s); err != nil {
return fmt.Errorf("%s: %w", component.Identifier(), err)
}
}
if tc, ok := component.(Int64Component); ok {
for _, v := range tc.Int64Validators {
if err := v(s); err != nil {
return fmt.Errorf("%s: %w", component.Identifier(), err)
}
}
}
return nil
}
type Int64Validator func(value any) error
type Int64ComponentConfig struct {
Validators []Int64Validator
}
type Int64ComponentOption func(*Int64ComponentConfig)

View File

@@ -0,0 +1,21 @@
package int64_component_validators
import (
"blocky/internal/block/int64_component"
"fmt"
)
func Max(max int64) int64_component.Int64ComponentOption {
return func(c *int64_component.Int64ComponentConfig) {
c.Validators = append(c.Validators, func(value any) error {
s, ok := value.(int64)
if !ok {
return fmt.Errorf("provided value bust be an int: %v", value)
}
if s > max {
return fmt.Errorf("int must be %d or less", max)
}
return nil
})
}
}