this repo has no description
0
fork

Configure Feed

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

tidy lexgen main cmd

+46 -28
+33 -15
cmd/lexgen/main.go
··· 45 45 46 46 var cmdLegacy = &cli.Command{ 47 47 Name: "legacy", 48 - Usage: "generate code with legacy behaviors", 48 + Usage: "generate code with legacy behaviors (for indigo only)", 49 49 ArgsUsage: `<file-or-dir>*`, 50 50 Flags: []cli.Flag{ 51 51 &cli.StringFlag{ ··· 85 85 Usage: "base directory for output packages", 86 86 Sources: cli.EnvVars("OUTPUT_DIR"), 87 87 }, 88 + &cli.BoolFlag{ 89 + Name: "no-imports-tidy", 90 + Usage: "skip cleanup of go imports in writen output", 91 + }, 88 92 }, 89 93 Action: runGen, 90 94 } 91 95 92 - func runGen(ctx context.Context, cmd *cli.Command) error { 96 + func collectPaths(cmd *cli.Command) ([]string, lexicon.Catalog, error) { 93 97 paths := cmd.Args().Slice() 94 98 if !cmd.Args().Present() { 95 99 paths = []string{cmd.String("lexicons-dir")} 96 100 _, err := os.Stat(paths[0]) 97 101 if err != nil { 98 - return fmt.Errorf("no path arguments specified and default lexicon directory not found\n%w", err) 102 + return nil, nil, fmt.Errorf("no path arguments specified and default lexicon directory not found\n%w", err) 99 103 } 100 104 } 101 105 ··· 105 109 ldinfo, err := os.Stat(lexDir) 106 110 if err == nil && ldinfo.IsDir() { 107 111 if err := cat.LoadDirectory(lexDir); err != nil { 108 - return err 112 + return nil, nil, err 109 113 } 110 114 } 111 115 116 + filePaths := []string{} 117 + 112 118 for _, p := range paths { 113 119 finfo, err := os.Stat(p) 114 120 if err != nil { 115 - return fmt.Errorf("failed loading %s: %w", p, err) 121 + return nil, nil, fmt.Errorf("failed loading %s: %w", p, err) 116 122 } 117 123 if finfo.IsDir() { 118 124 if p != cmd.String("lexicons-dir") { 119 125 // HACK: load first directory 120 126 if err := cat.LoadDirectory(p); err != nil { 121 - return err 127 + return nil, nil, err 122 128 } 123 129 } 124 130 if err := filepath.WalkDir(p, func(fp string, d fs.DirEntry, err error) error { 125 131 if d.IsDir() || path.Ext(fp) != ".json" { 126 132 return nil 127 133 } 128 - return genFile(ctx, cmd, &cat, fp) 134 + filePaths = append(filePaths, fp) 135 + return nil 129 136 }); err != nil { 130 - return err 137 + return nil, nil, err 131 138 } 132 139 continue 133 140 } 134 - if err := genFile(ctx, cmd, &cat, p); err != nil { 141 + filePaths = append(filePaths, p) 142 + } 143 + return filePaths, &cat, nil 144 + } 145 + 146 + func runGen(ctx context.Context, cmd *cli.Command) error { 147 + 148 + filePaths, cat, err := collectPaths(cmd) 149 + if err != nil { 150 + return err 151 + } 152 + 153 + for _, p := range filePaths { 154 + if err := genFile(ctx, cmd, cat, p); err != nil { 135 155 return err 136 156 } 137 157 } ··· 145 165 } 146 166 147 167 // parse file regularly 148 - // TODO: use json/v2 when available for case-sensitivity 168 + // NOTE: use json/v2 when it stabilizes for case-sensitivity 149 169 var sf lexicon.SchemaFile 150 170 151 - // two-part parsing before looking at errors 152 171 err = json.Unmarshal(b, &sf) 153 172 if err == nil { 154 173 err = sf.FinishParse() ··· 162 181 return fmt.Errorf("internal codegen flattening error (%s): %w", p, err) 163 182 } 164 183 165 - cfg := &lexgen.GenConfig{} 184 + cfg := lexgen.NewGenConfig() 166 185 if cmd.Bool("legacy-mode") { 167 186 cfg = lexgen.LegacyConfig() 168 187 } ··· 183 202 return err 184 203 } 185 204 186 - // TODO: this gets really slow if any imports are missing. should make this a CLI arg 187 - fixImports := true 188 - if fixImports { 205 + if !cmd.Bool("no-imports-tidy") { 206 + // NOTE: processing imports per file gets slow if imports are missing 189 207 fmtOpts := imports.Options{ 190 208 Comments: true, 191 209 TabIndent: false,
+13 -13
lex/lexgen/codegen.go
··· 20 20 LegacyMode bool 21 21 } 22 22 23 + func NewGenConfig() *GenConfig { 24 + return &GenConfig{ 25 + UnknownType: "map-string-any", 26 + WarningText: "Code generated by indigo lexgen tool. DO NOT EDIT MANUALLY.", 27 + } 28 + } 29 + 23 30 func LegacyConfig() *GenConfig { 24 31 return &GenConfig{ 25 32 RegisterLexiconTypeID: true, ··· 76 83 } 77 84 78 85 func (gen *FlatGenerator) baseName() string { 79 - // TODO: memoize? 86 + // TODO: memoize this value? this method gets called a lot 80 87 return nsidBaseName(gen.Lex.NSID) 81 88 } 82 89 ··· 94 101 "lexutil \"github.com/bluesky-social/indigo/lex/util\"": true, 95 102 "cbg \"github.com/whyrusleeping/cbor-gen\"": true, 96 103 } 97 - /* TODO: 98 - for _, t := range gen.Lex.Types { 99 - switch t.Type { 100 - case "query", "procedure": 101 - d["\"context\""] = true 102 - case "record": 103 - d["cbg \"github.com/whyrusleeping/cbor-gen\""] = true 104 - } 105 - } 106 - */ 104 + 107 105 for ext, _ := range gen.Lex.ExternalRefs { 106 + // TODO: replace this with 108 107 if strings.HasPrefix(ext, "com.atproto.") { 109 108 d["comatproto \"github.com/bluesky-social/indigo/api/atproto\""] = true 110 109 } else if strings.HasPrefix(ext, "app.bsky.") { ··· 114 113 } else if strings.HasPrefix(ext, "chat.bsky.") { 115 114 d["chatbsky \"github.com/bluesky-social/indigo/api/chat\""] = true 116 115 } else { 117 - // TODO: configurable mappings 118 - slog.Warn("unhandled external reference", "ref", ext) 116 + // TODO: configurable mappings; and return error if none found 117 + slog.Error("unhandled external reference", "ref", ext) 119 118 } 120 119 } 121 120 return d ··· 559 558 fmt.Fprintf(gen.Out, " }\n") 560 559 fmt.Fprintf(gen.Out, "}\n\n") 561 560 561 + // only import CBOR marshalling of unions in legacy mode 562 562 if !gen.Config.LegacyMode { 563 563 return nil 564 564 }