this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

lexgen: add more docs and comments

+35 -15
+2 -2
cmd/lexgen/main.go
··· 45 45 46 46 var cmdLegacy = &cli.Command{ 47 47 Name: "legacy", 48 - Usage: "generate code with legacy behaviors (for indigo only)", 48 + Usage: "generate code with legacy behaviors (for indigo repo only)", 49 49 ArgsUsage: `<file-or-dir>*`, 50 50 Flags: []cli.Flag{ 51 51 &cli.StringFlag{ ··· 187 187 } 188 188 189 189 buf := new(bytes.Buffer) 190 - gen := lexgen.FlatGenerator{ 190 + gen := lexgen.CodeGenerator{ 191 191 Config: cfg, 192 192 Lex: flat, 193 193 Cat: cat,
+16 -13
lex/lexgen/codegen.go
··· 11 11 "github.com/bluesky-social/indigo/atproto/syntax" 12 12 ) 13 13 14 + // Configuration for [CodeGenerator] output 14 15 type GenConfig struct { 15 16 RegisterLexiconTypeID bool 16 17 PackageMappings map[string]string ··· 36 37 } 37 38 } 38 39 39 - type FlatGenerator struct { 40 + // Core implementation of Go code generation for a single Lexicon schema file (multiple definitions), building on pre-parsed [FlatLexicon] 41 + type CodeGenerator struct { 40 42 Config *GenConfig 41 43 Lex *FlatLexicon 42 44 Cat lexicon.Catalog 43 45 Out io.Writer 44 46 } 45 47 46 - func (gen *FlatGenerator) WriteLexicon() error { 48 + // Outputs Go source code to the "Out" [io.Writer]. 49 + func (gen *CodeGenerator) WriteLexicon() error { 47 50 48 51 if gen.Config.WarningText != "" { 49 52 fmt.Fprintf(gen.Out, "// %s\n\n", gen.Config.WarningText) ··· 65 68 return nil 66 69 } 67 70 68 - func (gen *FlatGenerator) PkgName() string { 71 + func (gen *CodeGenerator) PkgName() string { 69 72 n := nsidPkgName(gen.Lex.NSID) 70 73 if gen.Config.LegacyMode { 71 74 switch n { ··· 82 85 return n 83 86 } 84 87 85 - func (gen *FlatGenerator) baseName() string { 88 + func (gen *CodeGenerator) baseName() string { 86 89 // TODO: memoize this value? this method gets called a lot 87 90 return nsidBaseName(gen.Lex.NSID) 88 91 } 89 92 90 - func (gen *FlatGenerator) FileName() string { 93 + func (gen *CodeGenerator) FileName() string { 91 94 return nsidFileName(gen.Lex.NSID) + ".go" 92 95 } 93 96 94 - func (gen *FlatGenerator) deps() map[string]bool { 97 + func (gen *CodeGenerator) deps() map[string]bool { 95 98 d := map[string]bool{ 96 99 "\"context\"": true, 97 100 "\"fmt\"": true, ··· 103 106 } 104 107 105 108 for ext, _ := range gen.Lex.ExternalRefs { 106 - // TODO: replace this with 109 + // TODO: replace this with configurable/extensible mappings 107 110 if strings.HasPrefix(ext, "com.atproto.") { 108 111 d["comatproto \"github.com/bluesky-social/indigo/api/atproto\""] = true 109 112 } else if strings.HasPrefix(ext, "app.bsky.") { ··· 120 123 return d 121 124 } 122 125 123 - func (gen *FlatGenerator) WriteType(ft *FlatType) error { 126 + func (gen *CodeGenerator) WriteType(ft *FlatType) error { 124 127 125 128 switch v := ft.Schema.Inner.(type) { 126 129 case lexicon.SchemaRecord: ··· 177 180 return false 178 181 } 179 182 180 - func (gen *FlatGenerator) fieldType(fname string, def *lexicon.SchemaDef, optional bool) (string, error) { 183 + func (gen *CodeGenerator) fieldType(fname string, def *lexicon.SchemaDef, optional bool) (string, error) { 181 184 // NOTE: SchemaObject and SchemaUnion should be handled outside this function; as well as arrays of those types also count 182 185 // TODO: another pass to check for type completeness 183 186 switch v := def.Inner.(type) { ··· 301 304 } 302 305 } 303 306 304 - func (gen *FlatGenerator) externalRefType(ref string) (string, error) { 307 + func (gen *CodeGenerator) externalRefType(ref string) (string, error) { 305 308 s, err := gen.Cat.Resolve(ref) 306 309 if err != nil { 307 310 return "", fmt.Errorf("could not resolve lexicon reference (%s): %w", ref, err) ··· 338 341 } 339 342 } 340 343 341 - func (gen *FlatGenerator) writeStruct(ft *FlatType, obj *lexicon.SchemaObject) error { 344 + func (gen *CodeGenerator) writeStruct(ft *FlatType, obj *lexicon.SchemaObject) error { 342 345 343 346 name := gen.baseName() 344 347 if ft.DefName != "main" { ··· 466 469 LexName string 467 470 } 468 471 469 - func (gen *FlatGenerator) writeUnion(ft *FlatType, union *lexicon.SchemaUnion) error { 472 + func (gen *CodeGenerator) writeUnion(ft *FlatType, union *lexicon.SchemaUnion) error { 470 473 471 474 name := gen.baseName() 472 475 if ft.DefName != "main" { ··· 612 615 return nil 613 616 } 614 617 615 - func (gen *FlatGenerator) writeEndpoint(ft *FlatType, desc string, params *lexicon.SchemaParams, output, input *lexicon.SchemaBody, isProcedure bool) error { 618 + func (gen *CodeGenerator) writeEndpoint(ft *FlatType, desc string, params *lexicon.SchemaParams, output, input *lexicon.SchemaBody, isProcedure bool) error { 616 619 name := gen.baseName() 617 620 618 621 fmt.Fprintf(gen.Out, "// %s calls the XRPC method \"%s\".\n", name, gen.Lex.NSID)
+17
lex/lexgen/doc.go
··· 1 + /* 2 + Package implementing Go code generation for lexicon schemas. 3 + 4 + Used by the 'lexgen' CLI tool to output Go structs and client API helpers based on Lexicon schemas. This package currently includes a "legacy" mode to stay as close as possible to the previous code generation output. 5 + 6 + WARNING: this package is still a work in progress. Both the package API and the generated code are likely to change, possibly in backwards-incompatible ways. 7 + 8 + # Package Structure 9 + 10 + The package works in two steps: 11 + 12 + - "flattening" parses a full lexicon schema file and copies nested type definitions in to a top-level array 13 + - code generation outputs a single Go source code file corresponding to a flattened lexicon schema file 14 + 15 + Wrapping code is expected to handle code formatting and fixing imports (which mostly means removing unused imports). 16 + */ 17 + package lexgen