-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsers.go
72 lines (58 loc) · 1.77 KB
/
parsers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"fmt"
"strings"
)
// skeleton function
func parseSentence(sentenceType string, line string, sentenceMap map[string]map[string]interface{}) (map[string]string, bool) {
if sentenceType == "$INXDR" {
return parseINXDR(line, sentenceMap)
} else {
return parseCommon(line, sentenceMap)
}
}
func parseCommon(line string, sentenceMap map[string]map[string]interface{}) (map[string]string, bool) {
data := make(map[string]string)
splitLine := strings.Split(line, "*")
if len(splitLine) != 2 {
return data, false
}
fields := strings.Split(splitLine[0], ",")
sentenceType := fields[0]
if mapping, exists := sentenceMap[sentenceType]; exists {
for pos, name := range mapping {
posInt := 0
fmt.Sscan(pos, &posInt) // Convert the position from string to integer
if posInt > 0 && posInt < len(fields) {
data[name.(string)] = fields[posInt]
}
}
return data, true
}
return data, false
}
// Parse $INXDR sentences
func parseINXDR(line string, sentenceMap map[string]map[string]interface{}) (map[string]string, bool) {
data := make(map[string]string)
splitLine := strings.Split(line, "*")
if len(splitLine) != 2 {
return data, false
}
fields := strings.Split(splitLine[0], ",")
sentenceType := fields[0] // Should be "$INXDR"
// Find the last field name, such as "FOILMIN"
lastName := fields[len(fields)-1]
// Check if this field is in the sentenceMap
if mapping, exists := sentenceMap[sentenceType][lastName]; exists {
mappingTyped := mapping.(map[string]interface{})
for pos, name := range mappingTyped {
posInt := 0
fmt.Sscan(pos, &posInt) // Convert the position from string to integer
if posInt > 0 && posInt < len(fields) {
data[name.(string)] = fields[posInt]
}
}
return data, true
}
return data, false
}