Add support keeping rewrites
diff --git a/ast/ast.go b/ast/ast.go
index 685483c..07a6df1 100644
--- a/ast/ast.go
+++ b/ast/ast.go
@@ -19,6 +19,7 @@
TokenNode NodeType = "token"
TermGroupNode NodeType = "termGroup"
TermNode NodeType = "term"
+ RewriteNode NodeType = "rewrite"
AndRelation RelationType = "and"
OrRelation RelationType = "or"
MatchEqual MatchType = "eq"
@@ -30,9 +31,23 @@
Type() NodeType
}
+// Rewrite represents a koral:rewrite
+type Rewrite struct {
+ Editor string `json:"editor,omitempty"`
+ Operation string `json:"operation,omitempty"`
+ Scope string `json:"scope,omitempty"`
+ Src string `json:"src,omitempty"`
+ Comment string `json:"_comment,omitempty"`
+}
+
+func (r *Rewrite) Type() NodeType {
+ return RewriteNode
+}
+
// Token represents a koral:token
type Token struct {
- Wrap Node `json:"wrap"`
+ Wrap Node `json:"wrap"`
+ Rewrites []Rewrite `json:"rewrites,omitempty"`
}
func (t *Token) Type() NodeType {
@@ -43,6 +58,7 @@
type TermGroup struct {
Operands []Node `json:"operands"`
Relation RelationType `json:"relation"`
+ Rewrites []Rewrite `json:"rewrites,omitempty"`
}
func (tg *TermGroup) Type() NodeType {
@@ -51,11 +67,12 @@
// Term represents a koral:term
type Term struct {
- Foundry string `json:"foundry"`
- Key string `json:"key"`
- Layer string `json:"layer"`
- Match MatchType `json:"match"`
- Value string `json:"value,omitempty"`
+ Foundry string `json:"foundry"`
+ Key string `json:"key"`
+ Layer string `json:"layer"`
+ Match MatchType `json:"match"`
+ Value string `json:"value,omitempty"`
+ Rewrites []Rewrite `json:"rewrites,omitempty"`
}
func (t *Term) Type() NodeType {
diff --git a/ast/ast_test.go b/ast/ast_test.go
index 1311a46..0daaf7c 100644
--- a/ast/ast_test.go
+++ b/ast/ast_test.go
@@ -36,6 +36,16 @@
},
expected: TermNode,
},
+ {
+ name: "Rewrite node returns correct type",
+ node: &Rewrite{
+ Editor: "Kustvakt",
+ Operation: "operation:injection",
+ Scope: "foundry",
+ Src: "Kustvakt",
+ },
+ expected: RewriteNode,
+ },
}
for _, tt := range tests {
@@ -61,14 +71,26 @@
Value: "Pdt",
}
+ rewrites := []Rewrite{
+ {
+ Editor: "Kustvakt",
+ Operation: "operation:injection",
+ Scope: "foundry",
+ Src: "Kustvakt",
+ Comment: "Default foundry has been added.",
+ },
+ }
+
group := &TermGroup{
Operands: []Node{term1, term2},
Relation: AndRelation,
+ Rewrites: rewrites,
}
assert.Len(t, group.Operands, 2)
assert.Equal(t, AndRelation, group.Relation)
assert.Equal(t, TermGroupNode, group.Type())
+ assert.Equal(t, rewrites, group.Rewrites)
// Test operands are correctly set
assert.Equal(t, term1, group.Operands[0])
@@ -83,10 +105,24 @@
Match: MatchEqual,
}
- token := &Token{Wrap: term}
+ rewrites := []Rewrite{
+ {
+ Editor: "Kustvakt",
+ Operation: "operation:injection",
+ Scope: "foundry",
+ Src: "Kustvakt",
+ Comment: "Default foundry has been added.",
+ },
+ }
+
+ token := &Token{
+ Wrap: term,
+ Rewrites: rewrites,
+ }
assert.Equal(t, TokenNode, token.Type())
assert.Equal(t, term, token.Wrap)
+ assert.Equal(t, rewrites, token.Rewrites)
}
func TestTermConstruction(t *testing.T) {
@@ -99,6 +135,7 @@
match MatchType
hasValue bool
value string
+ rewrites []Rewrite
}{
{
name: "Term without value",
@@ -144,6 +181,38 @@
match: MatchNotEqual,
hasValue: false,
},
+ {
+ name: "Term with rewrites",
+ term: &Term{
+ Foundry: "opennlp",
+ Key: "DET",
+ Layer: "p",
+ Match: MatchEqual,
+ Rewrites: []Rewrite{
+ {
+ Editor: "Kustvakt",
+ Operation: "operation:injection",
+ Scope: "foundry",
+ Src: "Kustvakt",
+ Comment: "Default foundry has been added.",
+ },
+ },
+ },
+ foundry: "opennlp",
+ key: "DET",
+ layer: "p",
+ match: MatchEqual,
+ hasValue: false,
+ rewrites: []Rewrite{
+ {
+ Editor: "Kustvakt",
+ Operation: "operation:injection",
+ Scope: "foundry",
+ Src: "Kustvakt",
+ Comment: "Default foundry has been added.",
+ },
+ },
+ },
}
for _, tt := range tests {
@@ -158,6 +227,11 @@
} else {
assert.Empty(t, tt.term.Value)
}
+ if tt.rewrites != nil {
+ assert.Equal(t, tt.rewrites, tt.term.Rewrites)
+ } else {
+ assert.Empty(t, tt.term.Rewrites)
+ }
})
}
}
@@ -251,6 +325,23 @@
}
}
+func TestRewriteConstruction(t *testing.T) {
+ rewrite := &Rewrite{
+ Editor: "Kustvakt",
+ Operation: "operation:injection",
+ Scope: "foundry",
+ Src: "Kustvakt",
+ Comment: "Default foundry has been added.",
+ }
+
+ assert.Equal(t, RewriteNode, rewrite.Type())
+ assert.Equal(t, "Kustvakt", rewrite.Editor)
+ assert.Equal(t, "operation:injection", rewrite.Operation)
+ assert.Equal(t, "foundry", rewrite.Scope)
+ assert.Equal(t, "Kustvakt", rewrite.Src)
+ assert.Equal(t, "Default foundry has been added.", rewrite.Comment)
+}
+
func TestComplexNestedStructures(t *testing.T) {
// Create a complex nested structure
innerGroup1 := &TermGroup{