package text_component import ( "blocky/internal/block" "blocky/internal/block/validation" "encoding/json" "fmt" ) type TextComponent struct { identifier string name string validators []validation.Validator TextValidators []TextValidator } func (t TextComponent) Type() block.ComponentType { return block.TextComponentType } func (t TextComponent) Name() string { return t.name } func (t TextComponent) Identifier() string { return t.identifier } func (t TextComponent) Validators() []validation.Validator { return t.validators } func NewTextComponent(identifier, name string, validators []validation.ComponentOption, textValidators []TextComponentOption) TextComponent { cfg := &validation.ComponentConfig{} for _, opt := range validators { opt(cfg) } textCfg := &TextComponentConfig{} for _, opt := range textValidators { opt(textCfg) } return TextComponent{identifier: identifier, name: name, validators: cfg.Validators, TextValidators: textCfg.Validators} } func (t TextComponent) NewInstance() block.ComponentInstance { return &TextComponentInstance{} } type TextComponentInstance struct { value string } func (t *TextComponentInstance) FromJSON(component block.Component, data json.RawMessage) error { var s string err := json.Unmarshal(data, &s) if err != nil { return 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.(TextComponent); ok { for _, v := range tc.TextValidators { if err := v(s); err != nil { return fmt.Errorf("%s: %w", component.Identifier(), err) } } } return nil } type TextValidator func(value any) error type TextComponentConfig struct { Validators []TextValidator } type TextComponentOption func(*TextComponentConfig)