-
Notifications
You must be signed in to change notification settings - Fork 85
/
main.go
108 lines (93 loc) · 2.91 KB
/
main.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
"gopkg.in/alanctgardner/gogen-avro.v3/container"
"gopkg.in/alanctgardner/gogen-avro.v3/generator"
"gopkg.in/alanctgardner/gogen-avro.v3/types"
)
func main() {
generateContainer := flag.Bool("container", false, "Whether to emit container file writer code")
packageName := flag.String("package", "avro", "Name of generated package")
flag.Parse()
if flag.NArg() < 2 {
fmt.Fprintf(os.Stderr, "Usage: gogen-avro [--container] [--package=<package name>] <target directory> <schema files>\n")
os.Exit(1)
}
targetDir := flag.Arg(0)
files := flag.Args()[1:]
var err error
pkg := generator.NewPackage(*packageName)
if *generateContainer {
err = addRecordDefinition([]byte(container.AVRO_BLOCK_SCHEMA), pkg, false)
if err != nil {
fmt.Fprintf(os.Stderr, "Error generating Avro container block schema - %v\n", err)
os.Exit(2)
}
err = addRecordDefinition([]byte(container.AVRO_HEADER_SCHEMA), pkg, false)
if err != nil {
fmt.Fprintf(os.Stderr, "Error generating Avro container header schema - %v\n", err)
os.Exit(2)
}
}
for _, fileName := range files {
schema, err := ioutil.ReadFile(fileName)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading file %q - %v\n", fileName, err)
os.Exit(2)
}
err = addRecordDefinition(schema, pkg, *generateContainer)
if err != nil {
fmt.Fprintf(os.Stderr, "Error decoding schema for file %q - %v\n", fileName, err)
os.Exit(3)
}
}
// Add header comment to all generated files.
for _, f := range pkg.Files() {
pkg.AddHeader(f, codegenComment(files))
}
err = pkg.WriteFiles(targetDir)
if err != nil {
fmt.Fprintf(os.Stderr, "Error writing source files to directory %q - %v\n", targetDir, err)
os.Exit(4)
}
}
func addRecordDefinition(schema []byte, pkg *generator.Package, generateContainer bool) error {
recordDefinition, err := types.RecordDefinitionForSchema(schema)
if err != nil {
return err
}
recordDefinition.AddStruct(pkg)
recordDefinition.AddSerializer(pkg)
recordDefinition.AddDeserializer(pkg)
if generateContainer {
containerWriter := container.NewAvroContainerWriter(schema, recordDefinition)
containerWriter.AddAvroContainerWriter(pkg)
}
return nil
}
// codegenComment generates a comment informing readers they are looking at
// generated code and lists the source avro files used to generate the code
//
// invariant: sources > 0
func codegenComment(sources []string) string {
const fileComment = `/*
* CODE GENERATED AUTOMATICALLY WITH github.com/alanctgardner/gogen-avro
* THIS FILE SHOULD NOT BE EDITED BY HAND
*
* %s
*/`
var sourceBlock []string
if len(sources) == 1 {
sourceBlock = append(sourceBlock, "SOURCE:")
} else {
sourceBlock = append(sourceBlock, "SOURCES:")
}
for _, source := range sources {
sourceBlock = append(sourceBlock, fmt.Sprintf(" * %s", source))
}
return fmt.Sprintf(fileComment, strings.Join(sourceBlock, "\n"))
}