-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_schema.go
57 lines (52 loc) · 1.15 KB
/
update_schema.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
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func UpdateSchema(schemaPath string) error {
schemaPathText := fmt.Sprintf("Updating the schema in: %s", schemaPath)
fmt.Println(schemaPathText)
file, err := os.Open(schemaPath)
if err != nil {
return err
}
outFile, err := os.OpenFile(schemaPath, os.O_RDWR, 0777)
if err != nil {
return err
}
defer file.Close()
defer outFile.Close()
scanner := bufio.NewScanner(file)
w := bufio.NewWriter(outFile)
updated := false
for scanner.Scan() {
line := scanner.Text()
if strings.HasSuffix(line, "``") {
updated = true
name := (strings.Fields(line)[0])
name2 := name
if name == "ID" || name == "UID" {
name = strings.ToLower(name)
name2 = name
if name == "id" {
name2 = "_" + name + ",omitempty"
name = "-"
}
} else {
name = fmt.Sprintf("%s%s", strings.ToLower(string(name[0])), name[1:])
name2 = name
}
replacement := fmt.Sprintf("`json:\"%s,omitempty\" bson:\"%s\"`", name, name2)
line = strings.ReplaceAll(line, "``", replacement)
}
fmt.Fprintln(w, line)
}
if updated {
if err := w.Flush(); err != nil {
return err
}
}
return nil
}