aboutsummaryrefslogtreecommitdiff
path: root/model
diff options
context:
space:
mode:
author2026-02-22 20:15:50 +0300
committer2026-02-22 20:15:50 +0300
commitdb8bb97dfa2dacef002a1f349ea970d76fee4fc9 (patch)
tree7de11be3a01a6ef83a218dc98d90586dd1afb09a /model
parentДобавил утилитарные функции для моделей (diff)
downloadconf-0.0.4.tar.gz
conf-0.0.4.tar.bz2
conf-0.0.4.tar.xz
conf-0.0.4.zip
Refactoringv0.0.4
Diffstat (limited to 'model')
-rw-r--r--model/body.go28
-rw-r--r--model/command.go9
-rw-r--r--model/directive.go9
-rw-r--r--model/doc.go41
-rw-r--r--model/setting.go (renamed from model/assignment.go)2
-rw-r--r--model/visitor.go6
6 files changed, 44 insertions, 51 deletions
diff --git a/model/body.go b/model/body.go
new file mode 100644
index 0000000..b7b4a0c
--- /dev/null
+++ b/model/body.go
@@ -0,0 +1,28 @@
+package model
+
+import (
+ "errors"
+ "fmt"
+)
+
+type Body []any
+
+var ErrInvalidType = errors.New("invalid type")
+
+func (d Body) Execute(v Visitor) error {
+ for _, it := range d {
+ switch it := it.(type) {
+ case *Setting:
+ if err := v.VisitSetting(it.Key, it.Value); err != nil {
+ return err
+ }
+ case *Directive:
+ if err := v.VisitDirective(it.Name, it.Arguments, it.Body); err != nil {
+ return err
+ }
+ default:
+ return fmt.Errorf("%w: %t", ErrInvalidType, it)
+ }
+ }
+ return nil
+}
diff --git a/model/command.go b/model/command.go
deleted file mode 100644
index 237e94f..0000000
--- a/model/command.go
+++ /dev/null
@@ -1,9 +0,0 @@
-package model
-
-type Command struct {
- Name string
- Arguments Values
- Body *Doc
-}
-
-type Commands []*Command
diff --git a/model/directive.go b/model/directive.go
new file mode 100644
index 0000000..3852e72
--- /dev/null
+++ b/model/directive.go
@@ -0,0 +1,9 @@
+package model
+
+type Directive struct {
+ Name string
+ Arguments Values
+ Body Body
+}
+
+type Directives []*Directive
diff --git a/model/doc.go b/model/doc.go
deleted file mode 100644
index 9c13a4c..0000000
--- a/model/doc.go
+++ /dev/null
@@ -1,41 +0,0 @@
-package model
-
-type Doc struct {
- items []any
- vars map[string]Values
- commands map[string]Commands
-}
-
-func New(cap int) *Doc {
- return &Doc{
- items: make([]any, 0, cap),
- vars: make(map[string]Values, cap),
- commands: make(map[string]Commands, cap),
- }
-}
-
-func (d *Doc) AppendAssignment(e *Assignment) {
- d.items = append(d.items, e)
- d.vars[e.Key] = append(d.vars[e.Key], e.Value...)
-}
-
-func (d *Doc) AppendCommand(c *Command) {
- d.items = append(d.items, c)
- d.commands[c.Name] = append(d.commands[c.Name], c)
-}
-
-func (d *Doc) Vars() map[string]Values {
- return d.vars
-}
-
-func (d *Doc) Get(key string) Values {
- return d.vars[key]
-}
-
-func (d *Doc) Commands(name string) Commands {
- return d.commands[name]
-}
-
-func (d *Doc) Items() []any {
- return d.items
-}
diff --git a/model/assignment.go b/model/setting.go
index 746e2b3..363a8a9 100644
--- a/model/assignment.go
+++ b/model/setting.go
@@ -1,6 +1,6 @@
package model
-type Assignment struct {
+type Setting struct {
Key string
Value Values
}
diff --git a/model/visitor.go b/model/visitor.go
new file mode 100644
index 0000000..3f290d3
--- /dev/null
+++ b/model/visitor.go
@@ -0,0 +1,6 @@
+package model
+
+type Visitor interface {
+ VisitDirective(ident string, args Values, body Body) error
+ VisitSetting(key string, values Values) error
+}