-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
80 lines (74 loc) · 1.92 KB
/
parser.go
File metadata and controls
80 lines (74 loc) · 1.92 KB
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
73
74
75
76
77
78
79
80
package main
import (
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"gopkg.in/yaml.v3"
"github.com/sqweek/dialog"
)
type Data struct {
A string `json:"A" xml:"A" yaml:"A"`
B string `json:"B" xml:"B" yaml:"B"`
}
func parseFileContent(content []byte, fileType string) (Data, error) {
var data Data
switch fileType {
case "json":
err := json.Unmarshal(content, &data)
return data, err
case "xml":
err := xml.Unmarshal(content, &data)
return data, err
case "yaml", "yml":
err := yaml.Unmarshal(content, &data)
return data, err
default:
return data, errors.New("unsupported file type")
}
}
func main() {
filePath, err := dialog.File().Filter("JSON files", "json").Filter("XML files", "xml").Filter("YAML files", "yaml", "yml").Title("Select File").Load()
if err != nil {
fmt.Println("No file selected or error:", err)
return
}
ext := strings.ToLower(filepath.Ext(filePath))
var fileType string
switch ext {
case ".json":
fileType = "json"
case ".xml":
fileType = "xml"
case ".yaml", ".yml":
fileType = "yaml"
default:
fmt.Println("Unsupported file type")
return
}
content, err := ioutil.ReadFile(filePath)
if err != nil {
fmt.Println("Error reading file:", err)
return
}
data, err := parseFileContent(content, fileType)
if err != nil {
fmt.Println("Invalid file or format:", err)
return
}
if data.A == "" || data.B == "" {
fmt.Println("Fields A and B are required.")
return
}
if data.A == data.B {
fmt.Println("Fields A and B are the same.")
} else if data.B > data.A {
fmt.Println("Field B is greater than A.")
} else {
fmt.Println("Fields A and B are different.")
}
}