Skip to content

Commit

Permalink
Merge pull request #64 from open-rpc/feat/types-for-method
Browse files Browse the repository at this point in the history
feat(method-typings): get types for method
  • Loading branch information
BelfordZ authored Apr 16, 2019
2 parents 88f487d + 35b6483 commit 352b057
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/method-typings/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,68 @@ describe("MethodTypings", () => {
expect(methodTypings).toBeInstanceOf(MethodTypings);
});

describe("getTypeDefinitionsForMethod", () => {

it("throws if types not generated yet", () => {
const methodTypings = new MethodTypings(testOpenRPCDocument);
expect(() => methodTypings.getTypeDefinitionsForMethod(testOpenRPCDocument.methods[0], "typescript")).toThrow();
});

describe("typscript", () => {

it("returns a string of typings for a method", async () => {
const methodTypings = new MethodTypings(testOpenRPCDocument);
await methodTypings.generateTypings();

expect(methodTypings.getTypeDefinitionsForMethod(testOpenRPCDocument.methods[0], "typescript")).toBe([
"export type TNiptip = number;",
"export interface IRipslip {",
" reepadoop?: number;",
" [k: string]: any;",
"}",
"",
].join("\n"));
});

});

describe("rust", () => {

it("returns a string of typings where the typeNames are unique", async () => {
const methodTypings = new MethodTypings(testOpenRPCDocument);
await methodTypings.generateTypings();

expect(methodTypings.getTypeDefinitionsForMethod(testOpenRPCDocument.methods[0], "rust")).toBe([
"pub type Niptip = f64;",
"#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]",
"#[cfg_attr(test, derive(Random))]",
"#[serde(untagged)]",
"pub enum Ripslip {",
" AnythingArray(Vec<Option<serde_json::Value>>),",
"",
" Bool(bool),",
"",
" Double(f64),",
"",
" Integer(i64),",
"",
" RipslipClass(RipslipClass),",
"",
" String(String),",
"}",
"#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]",
"#[cfg_attr(test, derive(Random))]",
"pub struct RipslipClass {",
" #[serde(rename = \"reepadoop\")]",
" reepadoop: Option<f64>,",
"}",
].join("\n"));
});

});

});

describe("getAllUniqueTypings", () => {

it("throws if types not generated yet", () => {
Expand Down
22 changes: 22 additions & 0 deletions src/method-typings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,28 @@ export default class MethodTypings {
return true;
}

/**
* Gives you all the types needed for a particular method.
*
* @param method The method you need the types for.
* @param langeuage The langauge you want the signature to be in.
*
* @returns A string containing all the typings
*
*/
public getTypeDefinitionsForMethod(method: MethodObject, language: TLanguages): string {
if (Object.keys(this.typingMapByLanguage).length === 0) {
throw new Error("typings have not yet been generated. Please run generateTypings first.");
}

return _.chain(this.typingMapByLanguage[language])
.values()
.filter(({ typeId }) => _.startsWith(typeId, method.name))
.map("typing")
.value()
.join("");
}

/**
* A method that returns all the types as a string, useful to directly inserting into code.
*
Expand Down

0 comments on commit 352b057

Please sign in to comment.