···1111 "github.com/bluesky-social/indigo/atproto/syntax"
1212)
13131414+// Configuration for [CodeGenerator] output
1415type GenConfig struct {
1516 RegisterLexiconTypeID bool
1617 PackageMappings map[string]string
···3637 }
3738}
38393939-type FlatGenerator struct {
4040+// Core implementation of Go code generation for a single Lexicon schema file (multiple definitions), building on pre-parsed [FlatLexicon]
4141+type CodeGenerator struct {
4042 Config *GenConfig
4143 Lex *FlatLexicon
4244 Cat lexicon.Catalog
4345 Out io.Writer
4446}
45474646-func (gen *FlatGenerator) WriteLexicon() error {
4848+// Outputs Go source code to the "Out" [io.Writer].
4949+func (gen *CodeGenerator) WriteLexicon() error {
47504851 if gen.Config.WarningText != "" {
4952 fmt.Fprintf(gen.Out, "// %s\n\n", gen.Config.WarningText)
···6568 return nil
6669}
67706868-func (gen *FlatGenerator) PkgName() string {
7171+func (gen *CodeGenerator) PkgName() string {
6972 n := nsidPkgName(gen.Lex.NSID)
7073 if gen.Config.LegacyMode {
7174 switch n {
···8285 return n
8386}
84878585-func (gen *FlatGenerator) baseName() string {
8888+func (gen *CodeGenerator) baseName() string {
8689 // TODO: memoize this value? this method gets called a lot
8790 return nsidBaseName(gen.Lex.NSID)
8891}
89929090-func (gen *FlatGenerator) FileName() string {
9393+func (gen *CodeGenerator) FileName() string {
9194 return nsidFileName(gen.Lex.NSID) + ".go"
9295}
93969494-func (gen *FlatGenerator) deps() map[string]bool {
9797+func (gen *CodeGenerator) deps() map[string]bool {
9598 d := map[string]bool{
9699 "\"context\"": true,
97100 "\"fmt\"": true,
···103106 }
104107105108 for ext, _ := range gen.Lex.ExternalRefs {
106106- // TODO: replace this with
109109+ // TODO: replace this with configurable/extensible mappings
107110 if strings.HasPrefix(ext, "com.atproto.") {
108111 d["comatproto \"github.com/bluesky-social/indigo/api/atproto\""] = true
109112 } else if strings.HasPrefix(ext, "app.bsky.") {
···120123 return d
121124}
122125123123-func (gen *FlatGenerator) WriteType(ft *FlatType) error {
126126+func (gen *CodeGenerator) WriteType(ft *FlatType) error {
124127125128 switch v := ft.Schema.Inner.(type) {
126129 case lexicon.SchemaRecord:
···177180 return false
178181}
179182180180-func (gen *FlatGenerator) fieldType(fname string, def *lexicon.SchemaDef, optional bool) (string, error) {
183183+func (gen *CodeGenerator) fieldType(fname string, def *lexicon.SchemaDef, optional bool) (string, error) {
181184 // NOTE: SchemaObject and SchemaUnion should be handled outside this function; as well as arrays of those types also count
182185 // TODO: another pass to check for type completeness
183186 switch v := def.Inner.(type) {
···301304 }
302305}
303306304304-func (gen *FlatGenerator) externalRefType(ref string) (string, error) {
307307+func (gen *CodeGenerator) externalRefType(ref string) (string, error) {
305308 s, err := gen.Cat.Resolve(ref)
306309 if err != nil {
307310 return "", fmt.Errorf("could not resolve lexicon reference (%s): %w", ref, err)
···338341 }
339342}
340343341341-func (gen *FlatGenerator) writeStruct(ft *FlatType, obj *lexicon.SchemaObject) error {
344344+func (gen *CodeGenerator) writeStruct(ft *FlatType, obj *lexicon.SchemaObject) error {
342345343346 name := gen.baseName()
344347 if ft.DefName != "main" {
···466469 LexName string
467470}
468471469469-func (gen *FlatGenerator) writeUnion(ft *FlatType, union *lexicon.SchemaUnion) error {
472472+func (gen *CodeGenerator) writeUnion(ft *FlatType, union *lexicon.SchemaUnion) error {
470473471474 name := gen.baseName()
472475 if ft.DefName != "main" {
···612615 return nil
613616}
614617615615-func (gen *FlatGenerator) writeEndpoint(ft *FlatType, desc string, params *lexicon.SchemaParams, output, input *lexicon.SchemaBody, isProcedure bool) error {
618618+func (gen *CodeGenerator) writeEndpoint(ft *FlatType, desc string, params *lexicon.SchemaParams, output, input *lexicon.SchemaBody, isProcedure bool) error {
616619 name := gen.baseName()
617620618621 fmt.Fprintf(gen.Out, "// %s calls the XRPC method \"%s\".\n", name, gen.Lex.NSID)
+17
lex/lexgen/doc.go
···11+/*
22+Package implementing Go code generation for lexicon schemas.
33+44+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.
55+66+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.
77+88+# Package Structure
99+1010+The package works in two steps:
1111+1212+- "flattening" parses a full lexicon schema file and copies nested type definitions in to a top-level array
1313+- code generation outputs a single Go source code file corresponding to a flattened lexicon schema file
1414+1515+Wrapping code is expected to handle code formatting and fixing imports (which mostly means removing unused imports).
1616+*/
1717+package lexgen