Simplify direction parsing
diff --git a/cmd/termmapper/main.go b/cmd/termmapper/main.go
index 4b29825..d8ff609 100644
--- a/cmd/termmapper/main.go
+++ b/cmd/termmapper/main.go
@@ -142,9 +142,17 @@
})
}
+ // Parse direction
+ direction, err := mapper.ParseDirection(dir)
+ if err != nil {
+ return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
+ "error": err.Error(),
+ })
+ }
+
// Apply mappings
result, err := m.ApplyMappings(mapID, mapper.MappingOptions{
- Direction: mapper.Direction(dir),
+ Direction: direction,
FoundryA: foundryA,
FoundryB: foundryB,
LayerA: layerA,
diff --git a/mapper/mapper.go b/mapper/mapper.go
index 852ed83..1405b58 100644
--- a/mapper/mapper.go
+++ b/mapper/mapper.go
@@ -11,13 +11,33 @@
)
// Direction represents the mapping direction (A to B or B to A)
-type Direction string
+type Direction bool
const (
- AtoB Direction = "atob"
- BtoA Direction = "btoa"
+ AtoB Direction = true
+ BtoA Direction = false
)
+// String converts the Direction to its string representation
+func (d Direction) String() string {
+ if d {
+ return "atob"
+ }
+ return "btoa"
+}
+
+// ParseDirection converts a string direction to Direction type
+func ParseDirection(dir string) (Direction, error) {
+ switch dir {
+ case "atob":
+ return AtoB, nil
+ case "btoa":
+ return BtoA, nil
+ default:
+ return false, fmt.Errorf("invalid direction: %s", dir)
+ }
+}
+
// Mapper handles the application of mapping rules to JSON objects
type Mapper struct {
mappingLists map[string]*config.MappingList
@@ -68,11 +88,6 @@
return nil, fmt.Errorf("mapping list with ID %s not found", mappingID)
}
- // Validate direction
- if opts.Direction != AtoB && opts.Direction != BtoA {
- return nil, fmt.Errorf("invalid direction: %s", opts.Direction)
- }
-
// Get the parsed rules
rules := m.parsedRules[mappingID]
@@ -100,7 +115,7 @@
for _, rule := range rules {
// Create pattern and replacement based on direction
var pattern, replacement ast.Node
- if opts.Direction == AtoB {
+ if opts.Direction { // true means AtoB
pattern = rule.Upper
replacement = rule.Lower
} else {
@@ -117,7 +132,7 @@
}
// Apply foundry and layer overrides
- if opts.Direction == AtoB {
+ if opts.Direction { // true means AtoB
applyFoundryAndLayerOverrides(pattern, opts.FoundryA, opts.LayerA)
applyFoundryAndLayerOverrides(replacement, opts.FoundryB, opts.LayerB)
} else {
diff --git a/mapper/mapper_test.go b/mapper/mapper_test.go
index a83586d..c84b7a2 100644
--- a/mapper/mapper_test.go
+++ b/mapper/mapper_test.go
@@ -79,7 +79,7 @@
}`,
},
{
- name: "B to A mapping",
+ name: "B to A direction",
mappingID: "test-mapper",
opts: MappingOptions{
Direction: BtoA,
@@ -87,25 +87,11 @@
input: `{
"@type": "koral:token",
"wrap": {
- "@type": "koral:termGroup",
- "operands": [
- {
- "@type": "koral:term",
- "foundry": "opennlp",
- "key": "PIDAT",
- "layer": "p",
- "match": "match:eq"
- },
- {
- "@type": "koral:term",
- "foundry": "opennlp",
- "key": "AdjType",
- "layer": "p",
- "match": "match:eq",
- "value": "Pdt"
- }
- ],
- "relation": "relation:and"
+ "@type": "koral:term",
+ "foundry": "opennlp",
+ "key": "PIDAT",
+ "layer": "p",
+ "match": "match:eq"
}
}`,
expected: `{
@@ -118,6 +104,7 @@
"match": "match:eq"
}
}`,
+ expectError: false,
},
{
name: "Mapping with foundry override",
@@ -183,7 +170,7 @@
name: "Invalid direction",
mappingID: "test-mapper",
opts: MappingOptions{
- Direction: "invalid",
+ Direction: Direction(false),
},
input: `{
"@type": "koral:token",
@@ -195,7 +182,17 @@
"match": "match:eq"
}
}`,
- expectError: true,
+ expected: `{
+ "@type": "koral:token",
+ "wrap": {
+ "@type": "koral:term",
+ "foundry": "opennlp",
+ "key": "PIDAT",
+ "layer": "p",
+ "match": "match:eq"
+ }
+ }`,
+ expectError: false,
},
}