Skip to content

Commit

Permalink
fix: cli minor bug fixes (#50)
Browse files Browse the repository at this point in the history
* fix: throw text and python codegen multiple namespaces

* Fix api not generating when only has subnamespaces

* fix missing typed file for python

* Fix task generates always with returns
  • Loading branch information
unimonkiez committed Jun 11, 2023
1 parent 5cb3970 commit ac5c96c
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 38 deletions.
2 changes: 1 addition & 1 deletion cli/src/core/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ Now: "${current.value.schemaPath}".`,
name,
existing,
current
) => `Enum.${name} is already defined once.
) => `Model.${name} is already defined once.
First time: "${existing.value.schemaPath}".
Now: "${current.value.schemaPath}".`,
}),
Expand Down
28 changes: 21 additions & 7 deletions cli/src/languages/python/__snapshots__/codegen-python.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -532,37 +532,51 @@ class MemorixUser(MemorixBase):
self.cache = MemorixCacheUser(self)
class MemorixCache(MemorixCacheBase):
class MemorixCacheSpaceship(MemorixCacheBase):
def __init__(self, api: MemorixBase) -> None:
super().__init__(api=api)
self.favoriteUser = MemorixCacheItemNoKey[str](
self.bio = MemorixCacheItemNoKey[str](
api=api,
id=\\"favoriteUser\\",
id=\\"bio\\",
payload_class=str,
)
class Memorix(MemorixBase):
class MemorixSpaceship(MemorixBase):
def __init__(
self,
redis_url: str,
ref: typing.Optional[MemorixBase] = None,
) -> None:
super().__init__(redis_url=redis_url, ref=ref)
self._namespace_name_tree = []
self._namespace_name_tree = [\\"spaceship\\"]
self._default_options = MemorixBase.DefaultOptions(
cache=MemorixCacheItem.Options(
expire=MemorixCacheItem.Options.Expire(
value=6,
value=5,
),
),
)
self.cache = MemorixCacheSpaceship(self)
class Memorix(MemorixBase):
def __init__(
self,
redis_url: str,
ref: typing.Optional[MemorixBase] = None,
) -> None:
super().__init__(redis_url=redis_url, ref=ref)
self._namespace_name_tree = []
self.user = MemorixUser(redis_url=redis_url, ref=self)
self.spaceship = MemorixSpaceship(redis_url=redis_url, ref=self)
self.cache = MemorixCache(self)"
"
`;

exports[`python codegen pubsub can generate with inline types 1`] = `
Expand Down
18 changes: 10 additions & 8 deletions cli/src/languages/python/codegen-python.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,16 +366,18 @@ describe("python codegen", () => {
}
}
}
DefaultOptions {
cache: {
expire: {
value: 6
Namespace spaceship {
DefaultOptions {
cache: {
expire: {
value: 5
}
}
}
}
Cache {
favoriteUser {
payload: string
Cache {
bio {
payload: string
}
}
}
`
Expand Down
16 changes: 9 additions & 7 deletions cli/src/languages/python/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ ${blockToCode(namespace.task!)}`
: []
)
.concat(
hasApi
hasApi || namespace.subNamespacesByName.size !== 0
? `class Memorix${nameCamel}(MemorixBase):
${getTabs(1)}def __init__(
${getTabs(2)}self,
Expand All @@ -282,12 +282,14 @@ ${getTabs(2)}self._default_options = ${defaultOptionsToCode(
: ""
}
${Array.from(namespace.subNamespacesByName.keys()).map(
(namespaceName) =>
`${getTabs(2)}self.${namespaceName} = Memorix${nameCamel}${camelCase(
namespaceName
)}(redis_url=redis_url, ref=self)`
)}${
${Array.from(namespace.subNamespacesByName.keys())
.map(
(namespaceName) =>
`${getTabs(2)}self.${namespaceName} = Memorix${nameCamel}${camelCase(
namespaceName
)}(redis_url=redis_url, ref=self)`
)
.join("\n")}${
Array.from(namespace.subNamespacesByName.keys()).length !== 0
? "\n\n"
: ""
Expand Down
31 changes: 25 additions & 6 deletions cli/src/languages/ts/__snapshots__/codegen-ts.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -231,18 +231,23 @@ class MemorixUser extends MemorixBase {
};
}
export class Memorix extends MemorixBase {
protected namespaceNameTree = [];
class MemorixSpaceship extends MemorixBase {
protected namespaceNameTree = [\\"spaceship\\"];
// prettier-ignore
protected defaultOptions = {cache:{expire:{value:6}}};
user = this.getNamespaceItem(MemorixUser);
protected defaultOptions = {cache:{expire:{value:5}}};
cache = {
favoriteUser: this.getCacheItemNoKey<string>(\\"favoriteUser\\"),
bio: this.getCacheItemNoKey<string>(\\"bio\\"),
};
}
export class Memorix extends MemorixBase {
protected namespaceNameTree = [];
user = this.getNamespaceItem(MemorixUser);
spaceship = this.getNamespaceItem(MemorixSpaceship);
}
"
`;
Expand Down Expand Up @@ -274,6 +279,20 @@ export class Memorix extends MemorixBase {
"
`;
exports[`ts codegen task can generate with no returns 1`] = `
"/* eslint-disable */
import { MemorixBase } from \\"@memorix/client-redis\\";
export class Memorix extends MemorixBase {
protected namespaceNameTree = [];
task = {
doIt: this.getTaskItem<number, string, undefined>(\\"doIt\\", false),
};
}
"
`;
exports[`ts codegen task can have options 1`] = `
"/* eslint-disable */
import { MemorixBase } from \\"@memorix/client-redis\\";
Expand Down
32 changes: 24 additions & 8 deletions cli/src/languages/ts/codegen-ts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,20 @@ describe("ts codegen", () => {
});
});
describe("task", () => {
it("can generate with no returns", async () => {
expect(
await codegenTs(
`
Task {
doIt {
key: int
payload: string
}
}
`
)
).toMatchSnapshot();
});
it("can generate with inline types", async () => {
expect(
await codegenTs(
Expand Down Expand Up @@ -351,16 +365,18 @@ describe("ts codegen", () => {
}
}
}
DefaultOptions {
cache: {
expire: {
value: 6
Namespace spaceship {
DefaultOptions {
cache: {
expire: {
value: 5
}
}
}
}
Cache {
favoriteUser {
payload: string
Cache {
bio {
payload: string
}
}
}
`
Expand Down
8 changes: 7 additions & 1 deletion cli/src/languages/ts/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,13 @@ ${b.values.map((v) => `${getTabs(1)}${v} = "${v}",`).join(`\n`)}
}`
: ""
}>("${name}"${
hasReturns ? `, ${"returns" in v ? "true" : "false"}` : ""
hasReturns
? `, ${
(v as MapValue<BlockTask["values"]>).returns
? "true"
: "false"
}`
: ""
}${
(
v as
Expand Down
Empty file.

0 comments on commit ac5c96c

Please sign in to comment.