Skip to content

Commit

Permalink
sort inputs in ts codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
slugalisk committed Nov 23, 2021
1 parent d068cc0 commit 02071e8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
31 changes: 25 additions & 6 deletions cmd/ts/ts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"log"
"sort"
"strings"

"github.com/MemeLabs/protobuf/pkg/pgsutil"
Expand Down Expand Up @@ -68,6 +69,17 @@ func (g *generator) generateFile(f pgs.File) {
}
}

type importSet struct {
set map[string]struct{}
entities []pgs.Entity
}

type pgsEntitySlice []pgs.Entity

func (s pgsEntitySlice) Len() int { return len(s) }
func (s pgsEntitySlice) Less(i, j int) bool { return s[i].Name() < s[j].Name() }
func (s pgsEntitySlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }

func (g *generator) generateImports(f pgs.File) {
root := g.runtimePath + "/lib/"
if g.runtimePath == "self" {
Expand All @@ -78,7 +90,7 @@ func (g *generator) generateImports(f pgs.File) {
g.Linef(`import Writer from "%spb/writer";`, root)
g.LineBreak()

imports := map[string]map[string]pgs.Entity{}
imports := map[string]*importSet{}
for _, m := range f.AllMessages() {
EachField:
for _, f := range m.Fields() {
Expand All @@ -95,14 +107,21 @@ func (g *generator) generateImports(f pgs.File) {
fk := e.File().InputPath().String()
ek := e.FullyQualifiedName()
if _, ok := imports[fk]; !ok {
imports[fk] = map[string]pgs.Entity{}
imports[fk] = &importSet{set: map[string]struct{}{}}
}
set := imports[fk]
if _, ok := set.set[ek]; !ok {
set.set[ek] = struct{}{}
set.entities = append(set.entities, e)
}
imports[fk][ek] = e
}
}
for _, i := range f.Imports() {
entities := imports[i.InputPath().String()].entities
sort.Sort(pgsEntitySlice(entities))

g.Line(`import {`)
for _, t := range imports[i.InputPath().String()] {
for _, t := range entities {
g.Linef(
"%s as %s,",
strings.TrimPrefix(strings.TrimPrefix(t.FullyQualifiedName(), i.FullyQualifiedName()), "."),
Expand Down Expand Up @@ -209,9 +228,9 @@ func (g *generator) generateMessage(m pgs.Message) {
}
} else if f.Type().IsMap() {
if f.Type().Element().IsEmbed() {
g.Linef(`if (v?.%s) this.%s = new Map((v.%s instanceof Map ? Array.from(v.%s.entries()) : Object.entries(v.%s)).map(([k, v]) => [k, new %s(v)]));`, name, name, name, name, name, g.fieldInfo(f).tsBaseType)
g.Linef(`if (v?.%s) this.%s = new Map((v.%s instanceof Map ? Array.from(v.%s) : Object.entries(v.%s)).map(([k, v]) => [k, new %s(v)]));`, name, name, name, name, name, g.fieldInfo(f).tsBaseType)
} else {
g.Linef(`if (v?.%s) this.%s = v.%s instanceof Map ? v.%s : new Map(Object.entries(v.%s));`, name, name, name, name, name)
g.Linef(`if (v?.%s) this.%s = new Map(v.%s instanceof Map ? v.%s : Object.entries(v.%s));`, name, name, name, name, name)
}
g.Linef(`else this.%s = %s;`, name, fi.zeroValue)
} else if f.Type().IsEmbed() {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@memelabs/protobuf",
"version": "0.2.2",
"version": "0.2.3",
"description": "",
"license": "MIT",
"repository": {
Expand Down
8 changes: 6 additions & 2 deletions src/rpc/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ export default class Host {
objectMode: true,
});

e.on("close", () => this.call("CANCEL", new Cancel(), Call.Kind.CALL_KIND_CANCEL, call.id));
const handleClose = () => {
this.call("CANCEL", new Cancel(), Call.Kind.CALL_KIND_CANCEL, call.id);
};
e.on("close", handleClose);

this.callbacks.set(call.id, (res: Call) => {
switch (res.kind) {
Expand All @@ -97,7 +100,8 @@ export default class Host {
break;
case Call.Kind.CALL_KIND_CLOSE:
this.callbacks.delete(call.id);
e.push(null);
e.off("close", handleClose);
e.end();
break;
}
});
Expand Down

0 comments on commit 02071e8

Please sign in to comment.