this repo has no description
0
fork

Configure Feed

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

lexgen runs all packages together (#709)

cross-package references can now make a type know that it needs to
include a type field

authored by

Brian Olson and committed by
GitHub
2ca57ad9 a24c6825

+73 -51
+1 -4
Makefile
··· 67 67 68 68 .PHONY: lexgen 69 69 lexgen: ## Run codegen tool for lexicons (lexicon JSON to Go packages) 70 - go run ./cmd/lexgen/ --package bsky --prefix app.bsky --outdir api/bsky $(LEXDIR) 71 - go run ./cmd/lexgen/ --package atproto --prefix com.atproto --outdir api/atproto $(LEXDIR) 72 - go run ./cmd/lexgen/ --package chat --prefix chat.bsky --outdir api/chat $(LEXDIR) 73 - go run ./cmd/lexgen/ --package ozone --prefix tools.ozone --outdir api/ozone $(LEXDIR) 70 + go run ./cmd/lexgen/ $(LEXDIR) 74 71 75 72 .PHONY: cborgen 76 73 cborgen: ## Run codegen tool for CBOR serialization
+11 -37
cmd/lexgen/main.go
··· 11 11 cli "github.com/urfave/cli/v2" 12 12 ) 13 13 14 - func findSchemas(dir string) ([]string, error) { 15 - var out []string 14 + func findSchemas(dir string, out []string) ([]string, error) { 16 15 err := filepath.Walk(dir, func(path string, info fs.FileInfo, err error) error { 17 16 if err != nil { 18 17 return err ··· 29 28 return nil 30 29 }) 31 30 if err != nil { 32 - return nil, err 31 + return out, err 33 32 } 34 33 35 34 return out, nil 36 35 37 36 } 38 37 38 + // for direct .json lexicon files or directories containing lexicon .json files, get one flat list of all paths to .json files 39 39 func expandArgs(args []string) ([]string, error) { 40 40 var out []string 41 41 for _, a := range args { ··· 44 44 return nil, err 45 45 } 46 46 if st.IsDir() { 47 - s, err := findSchemas(a) 47 + out, err = findSchemas(a, out) 48 48 if err != nil { 49 49 return nil, err 50 50 } 51 - out = append(out, s...) 52 51 } else if strings.HasSuffix(a, ".json") { 53 52 out = append(out, a) 54 53 } ··· 82 81 }, 83 82 } 84 83 app.Action = func(cctx *cli.Context) error { 85 - outdir := cctx.String("outdir") 86 - if outdir == "" { 87 - return fmt.Errorf("must specify output directory (--outdir)") 88 - } 89 - 90 - prefix := cctx.String("prefix") 91 - 92 84 paths, err := expandArgs(cctx.Args().Slice()) 93 85 if err != nil { 94 86 return err ··· 108 100 schemas = append(schemas, s) 109 101 } 110 102 111 - pkgname := cctx.String("package") 112 - 113 - imports := map[string]string{ 114 - "app.bsky": "github.com/bluesky-social/indigo/api/bsky", 115 - "com.atproto": "github.com/bluesky-social/indigo/api/atproto", 116 - } 117 - 118 103 if cctx.Bool("gen-server") { 119 - defmap := lex.BuildExtDefMap(schemas, []string{"com.atproto", "app.bsky"}) 104 + pkgname := cctx.String("package") 105 + outdir := cctx.String("outdir") 106 + if outdir == "" { 107 + return fmt.Errorf("must specify output directory (--outdir)") 108 + } 109 + defmap := lex.BuildExtDefMap(schemas) 120 110 _ = defmap 121 111 122 112 paths := cctx.StringSlice("types-import") ··· 133 123 } 134 124 135 125 } else { 136 - defmap := lex.BuildExtDefMap(schemas, []string{"com.atproto", "app.bsky"}) 137 - 138 - // Run this twice as a hack to deal with indirect references referencing indirect references. 139 - // This part of the codegen needs to be redone 140 - lex.FixRecordReferences(schemas, defmap, prefix) 141 - lex.FixRecordReferences(schemas, defmap, prefix) 142 - for i, s := range schemas { 143 - if !strings.HasPrefix(s.ID, prefix) { 144 - continue 145 - } 146 - 147 - fname := filepath.Join(outdir, s.Name()+".go") 148 - 149 - if err := lex.GenCodeForSchema(pkgname, prefix, fname, true, s, defmap, imports); err != nil { 150 - return fmt.Errorf("failed to process schema %q: %w", paths[i], err) 151 - } 152 - } 126 + return lex.Run(schemas) 153 127 } 154 128 155 129 return nil
+61 -10
lex/gen.go
··· 25 25 ) 26 26 27 27 type Schema struct { 28 + path string 28 29 prefix string 29 30 30 31 Lexicon int `json:"lexicon"` ··· 50 51 } 51 52 52 53 type TypeSchema struct { 53 - prefix string 54 - id string 55 - defName string 54 + prefix string // prefix of a major package being processed, e.g. com.atproto 55 + id string // parent Schema.ID 56 + defName string // parent Schema.Defs[defName] points to this *TypeSchema 56 57 defMap map[string]*ExtDef 57 58 needsCbor bool 58 59 needsType bool ··· 149 150 if ts.Type == "ref" { 150 151 refname := ts.Ref 151 152 if strings.HasPrefix(refname, "#") { 152 - fmt.Println("Foo") 153 153 refname = s.ID + ts.Ref 154 154 } 155 155 ··· 222 222 if err := json.NewDecoder(fi).Decode(&s); err != nil { 223 223 return nil, err 224 224 } 225 + s.path = f 225 226 226 227 return &s, nil 227 228 } 228 229 229 - func BuildExtDefMap(ss []*Schema, prefixes []string) map[string]*ExtDef { 230 + // Build total map of all types defined inside schemas. 231 + // Return map from fully qualified type name to its *TypeSchema 232 + func BuildExtDefMap(ss []*Schema) map[string]*ExtDef { 230 233 out := make(map[string]*ExtDef) 231 234 for _, s := range ss { 232 235 for k, d := range s.Defs { ··· 235 238 d.defName = k 236 239 237 240 var pref string 238 - for _, p := range prefixes { 239 - if strings.HasPrefix(s.ID, p) { 240 - pref = p 241 + for _, pkg := range Packages { 242 + if strings.HasPrefix(s.ID, pkg.Prefix) { 243 + pref = pkg.Prefix 241 244 break 242 245 } 243 246 } ··· 300 303 } 301 304 } 302 305 303 - func GenCodeForSchema(pkg string, prefix string, fname string, reqcode bool, s *Schema, defmap map[string]*ExtDef, imports map[string]string) error { 306 + func GenCodeForSchema(pkg string, prefix string, fname string, reqcode bool, s *Schema, defmap map[string]*ExtDef) error { 304 307 buf := new(bytes.Buffer) 305 308 pf := printerf(buf) 306 309 ··· 324 327 pf("\tcbg \"github.com/whyrusleeping/cbor-gen\"\n") 325 328 pf("\t\"github.com/bluesky-social/indigo/xrpc\"\n") 326 329 pf("\t\"github.com/bluesky-social/indigo/lex/util\"\n") 327 - for k, v := range imports { 330 + for k, v := range prefixToGoImport { 328 331 if k != prefix { 329 332 pf("\t%s %q\n", importNameForPrefix(k), v) 330 333 } ··· 1470 1473 1471 1474 return nil 1472 1475 } 1476 + 1477 + type Package struct { 1478 + GoPackage string 1479 + Prefix string 1480 + Outdir string 1481 + } 1482 + 1483 + var Packages []Package = []Package{ 1484 + Package{"bsky", "app.bsky", "api/bsky"}, 1485 + Package{"atproto", "com.atproto", "api/atproto"}, 1486 + Package{"chat", "chat.bsky", "api/chat"}, 1487 + Package{"ozone", "tools.ozone", "api/ozone"}, 1488 + } 1489 + 1490 + var prefixToGoImport map[string]string 1491 + 1492 + func init() { 1493 + prefixToGoImport = map[string]string{ 1494 + "app.bsky": "github.com/bluesky-social/indigo/api/bsky", 1495 + "chat.bsky": "github.com/bluesky-social/indigo/api/chat", 1496 + "com.atproto": "github.com/bluesky-social/indigo/api/atproto", 1497 + "tools.ozone": "github.com/bluesky-social/indigo/api/ozone", 1498 + } 1499 + } 1500 + 1501 + func Run(schemas []*Schema) error { 1502 + defmap := BuildExtDefMap(schemas) 1503 + 1504 + for _, pkg := range Packages { 1505 + prefix := pkg.Prefix 1506 + FixRecordReferences(schemas, defmap, prefix) 1507 + } 1508 + 1509 + for _, pkg := range Packages { 1510 + for _, s := range schemas { 1511 + if !strings.HasPrefix(s.ID, pkg.Prefix) { 1512 + continue 1513 + } 1514 + 1515 + fname := filepath.Join(pkg.Outdir, s.Name()+".go") 1516 + 1517 + if err := GenCodeForSchema(pkg.GoPackage, pkg.Prefix, fname, true, s, defmap); err != nil { 1518 + return fmt.Errorf("failed to process schema %q: %w", s.path, err) 1519 + } 1520 + } 1521 + } 1522 + return nil 1523 + }