@@ -2,31 +2,35 @@ package main
2
2
3
3
import (
4
4
"bytes"
5
+ "context"
5
6
"errors"
6
7
"flag"
7
8
"fmt"
8
9
"io"
9
10
"os"
10
11
"path/filepath"
12
+ "strconv"
11
13
"strings"
12
14
13
15
"github.com/hamba/avro/v2"
14
16
"github.com/hamba/avro/v2/gen"
17
+ "github.com/hamba/avro/v2/registry"
15
18
"golang.org/x/tools/imports"
16
19
)
17
20
18
21
type config struct {
19
22
TemplateFileName string
20
23
21
- Pkg string
22
- PkgDoc string
23
- Out string
24
- Tags string
25
- FullName bool
26
- Encoders bool
27
- FullSchema bool
28
- StrictTypes bool
29
- Initialisms string
24
+ Pkg string
25
+ PkgDoc string
26
+ Out string
27
+ Tags string
28
+ FullName bool
29
+ Encoders bool
30
+ FullSchema bool
31
+ StrictTypes bool
32
+ Initialisms string
33
+ SchemaRegistry string
30
34
}
31
35
32
36
func main () {
@@ -47,6 +51,7 @@ func realMain(args []string, stdout, stderr io.Writer) int {
47
51
flgs .BoolVar (& cfg .StrictTypes , "strict-types" , false , "Use strict type sizes (e.g. int32) during generation." )
48
52
flgs .StringVar (& cfg .Initialisms , "initialisms" , "" , "Custom initialisms <VAL>[,...] for struct and field names." )
49
53
flgs .StringVar (& cfg .TemplateFileName , "template-filename" , "" , "Override output template with one loaded from file." )
54
+ flgs .StringVar (& cfg .SchemaRegistry , "schemaregistry" , "" , "The URL to schema registry, e.g.: http://localhost:8081." )
50
55
flgs .Usage = func () {
51
56
_ , _ = fmt .Fprintln (stderr , "Usage: avrogen [options] schemas" )
52
57
_ , _ = fmt .Fprintln (stderr , "Options:" )
@@ -88,13 +93,38 @@ func realMain(args []string, stdout, stderr io.Writer) int {
88
93
gen .WithStrictTypes (cfg .StrictTypes ),
89
94
gen .WithFullSchema (cfg .FullSchema ),
90
95
}
96
+
91
97
g := gen .NewGenerator (cfg .Pkg , tags , opts ... )
92
- for _ , file := range flgs .Args () {
93
- schema , err := avro .ParseFiles (filepath .Clean (file ))
94
- if err != nil {
95
- _ , _ = fmt .Fprintf (stderr , "Error: %v\n " , err )
96
- return 2
98
+ for _ , entry := range flgs .Args () {
99
+ var schema avro.Schema
100
+
101
+ switch cfg .SchemaRegistry {
102
+ case "" :
103
+ schema , err = avro .ParseFiles (filepath .Clean (entry ))
104
+ if err != nil {
105
+ _ , _ = fmt .Fprintf (stderr , "Error: %v\n " , err )
106
+ return 2
107
+ }
108
+ default :
109
+ client , err := registry .NewClient (cfg .SchemaRegistry )
110
+ if err != nil {
111
+ _ , _ = fmt .Fprintf (stderr , "Error: %v\n " , err )
112
+ return 2
113
+ }
114
+
115
+ subject , version , err := parseSubjectVersion (entry )
116
+ if err != nil {
117
+ _ , _ = fmt .Fprintf (stderr , "Error: %v\n " , err )
118
+ return 2
119
+ }
120
+
121
+ schema , err = client .GetSchemaByVersion (context .Background (), subject , version )
122
+ if err != nil {
123
+ _ , _ = fmt .Fprintf (stderr , "Error: %v\n " , err )
124
+ return 2
125
+ }
97
126
}
127
+
98
128
g .Parse (schema )
99
129
}
100
130
@@ -205,3 +235,17 @@ func loadTemplate(templateFileName string) ([]byte, error) {
205
235
}
206
236
return os .ReadFile (filepath .Clean (templateFileName ))
207
237
}
238
+
239
+ func parseSubjectVersion (entry string ) (string , int , error ) {
240
+ parts := strings .Split (entry , ":" )
241
+ if len (parts ) != 2 {
242
+ return "" , - 1 , errors .New ("entry must be of format subject:version" )
243
+ }
244
+
245
+ version , err := strconv .Atoi (parts [1 ])
246
+ if err != nil {
247
+ return "" , - 1 , err
248
+ }
249
+
250
+ return parts [0 ], version , nil
251
+ }
0 commit comments