-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmarshaler.go
More file actions
59 lines (49 loc) · 1.09 KB
/
marshaler.go
File metadata and controls
59 lines (49 loc) · 1.09 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
package main
import (
"flag"
"fmt"
"go/parser"
"go/token"
"os"
)
type PIIMarshaler interface {
Generate([]string, map[string][]field) string
GetLibFunc(string) string
}
func generate(path string, loglib PIIMarshaler) {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, path, nil, parser.Mode(0))
if err != nil {
panic(err)
}
structs, structFields := getFields(f, loglib)
if len(structs) == 0 {
panic(fmt.Errorf("cannot generate no suitable struct exists target=%s", path))
}
data := loglib.Generate(structs, structFields)
if data == "" {
panic(fmt.Errorf("cannot generate file no suitable struct exists target=%s", path))
}
file, err := os.OpenFile(path, os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
panic(err)
}
defer file.Close()
_, err = file.Write([]byte(data))
if err != nil {
panic(err)
}
}
func main() {
path := flag.String("f", "", "target file path")
lib := flag.String("lib", "", "zap/zerolog?")
flag.Parse()
var loglib PIIMarshaler
switch *lib {
case "zerolog":
loglib = &ZeroLog{}
default:
loglib = &UberZap{}
}
generate(*path, loglib)
}