add Int64Component
This commit is contained in:
@@ -2,7 +2,6 @@ package block
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Block struct {
|
type Block struct {
|
||||||
@@ -23,17 +22,18 @@ func (b *BlockInstance) FromJSON(block *Block, data []byte) error {
|
|||||||
|
|
||||||
for _, c := range block.Components {
|
for _, c := range block.Components {
|
||||||
jsonValue, ok := raw[c.Identifier()]
|
jsonValue, ok := raw[c.Identifier()]
|
||||||
|
// jsonValue, ok := raw[c.Identifier()]
|
||||||
if !ok {
|
if !ok {
|
||||||
continue
|
// continue
|
||||||
|
jsonValue = nil
|
||||||
}
|
}
|
||||||
instance := c.NewInstance()
|
instance := c.NewInstance()
|
||||||
|
|
||||||
if err := instance.FromJSON(c, jsonValue); err != nil {
|
if err := instance.FromJSON(c, jsonValue); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
components = append(components, instance)
|
|
||||||
|
|
||||||
fmt.Println(c.Identifier(), instance)
|
components = append(components, instance)
|
||||||
}
|
}
|
||||||
|
|
||||||
b.Components = components
|
b.Components = components
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ type ComponentType int8
|
|||||||
const (
|
const (
|
||||||
_ ComponentType = iota
|
_ ComponentType = iota
|
||||||
TextComponentType
|
TextComponentType
|
||||||
NumberComponentType
|
Int64ComponentType
|
||||||
Date
|
Date
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -24,4 +24,5 @@ type Component interface {
|
|||||||
|
|
||||||
type ComponentInstance interface {
|
type ComponentInstance interface {
|
||||||
FromJSON(component Component, data json.RawMessage) error
|
FromJSON(component Component, data json.RawMessage) error
|
||||||
|
Value() interface{}
|
||||||
}
|
}
|
||||||
|
|||||||
89
internal/block/int64_component/int64_component.go
Normal file
89
internal/block/int64_component/int64_component.go
Normal 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)
|
||||||
21
internal/block/int64_component/validators/max.go
Normal file
21
internal/block/int64_component/validators/max.go
Normal 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
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,54 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
||||||
@@ -52,13 +52,20 @@ type TextComponentInstance struct {
|
|||||||
value string
|
value string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *TextComponentInstance) Value() interface{} {
|
||||||
|
return t.value
|
||||||
|
}
|
||||||
|
|
||||||
func (t *TextComponentInstance) FromJSON(component block.Component, data json.RawMessage) error {
|
func (t *TextComponentInstance) FromJSON(component block.Component, data json.RawMessage) error {
|
||||||
var s string
|
var s string
|
||||||
|
|
||||||
|
if data != nil {
|
||||||
err := json.Unmarshal(data, &s)
|
err := json.Unmarshal(data, &s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
t.value = s
|
t.value = s
|
||||||
|
}
|
||||||
|
|
||||||
for _, v := range component.Validators() {
|
for _, v := range component.Validators() {
|
||||||
if err := v(s); err != nil {
|
if err := v(s); err != nil {
|
||||||
@@ -81,5 +88,4 @@ type TextValidator func(value any) error
|
|||||||
type TextComponentConfig struct {
|
type TextComponentConfig struct {
|
||||||
Validators []TextValidator
|
Validators []TextValidator
|
||||||
}
|
}
|
||||||
|
|
||||||
type TextComponentOption func(*TextComponentConfig)
|
type TextComponentOption func(*TextComponentConfig)
|
||||||
|
|||||||
25
main.go
25
main.go
@@ -2,6 +2,8 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"blocky/internal/block"
|
"blocky/internal/block"
|
||||||
|
"blocky/internal/block/int64_component"
|
||||||
|
int64_component_validators "blocky/internal/block/int64_component/validators"
|
||||||
"blocky/internal/block/text_component"
|
"blocky/internal/block/text_component"
|
||||||
text_component_validators "blocky/internal/block/text_component/validators"
|
text_component_validators "blocky/internal/block/text_component/validators"
|
||||||
"blocky/internal/block/validation"
|
"blocky/internal/block/validation"
|
||||||
@@ -9,31 +11,32 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
b := &block.Block{}
|
||||||
textComponent := text_component.NewTextComponent(
|
textComponent := text_component.NewTextComponent(
|
||||||
"title",
|
"title",
|
||||||
"Title",
|
"Title",
|
||||||
// validation.Required(),
|
|
||||||
// validation.MaxLength(4),
|
|
||||||
// nil,
|
|
||||||
[]validation.ComponentOption{
|
[]validation.ComponentOption{
|
||||||
validation.Required(),
|
validation.Required(),
|
||||||
validation.MaxLength(4),
|
|
||||||
},
|
},
|
||||||
[]text_component.TextComponentOption{
|
[]text_component.TextComponentOption{
|
||||||
text_component_validators.Regex("^\\d+$"),
|
text_component_validators.Regex("^\\d+$"),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
b := &block.Block{}
|
|
||||||
b.Components = append(b.Components, textComponent)
|
b.Components = append(b.Components, textComponent)
|
||||||
|
|
||||||
fmt.Println(textComponent.Name(), textComponent.Type())
|
fmt.Println(textComponent.Name(), textComponent.Type())
|
||||||
|
|
||||||
numberComponent := block.NewNumberComponent("age", "Age")
|
int64Component := int64_component.NewInt64Component(
|
||||||
b.Components = append(b.Components, numberComponent)
|
"year",
|
||||||
|
"Year",
|
||||||
|
[]validation.ComponentOption{},
|
||||||
|
[]int64_component.Int64ComponentOption{
|
||||||
|
int64_component_validators.Max(4),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
b.Components = append(b.Components, int64Component)
|
||||||
|
|
||||||
data := []byte(`{"title": "252", "age": 25 }`)
|
data := []byte(`{"title": "252", "year": 2 }`)
|
||||||
// data := []byte(`{"title": "this is the title", "age": 25 }`)
|
// data := []byte(`{"year": 2}`)
|
||||||
instance := &block.BlockInstance{}
|
instance := &block.BlockInstance{}
|
||||||
err := instance.FromJSON(b, data)
|
err := instance.FromJSON(b, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user