Skip to content

Commit

Permalink
Feat: simplify API by remove optional task passing
Browse files Browse the repository at this point in the history
Problem: 
- currently it is not possible to pass task options with the default implementation
- one would be required to pass in a Task and then options for that task
- this is not a good API

Solution: 
- Stick to only blocking Task calls for now
- update code generators
  • Loading branch information
mindreframer committed Nov 24, 2023
1 parent a33edac commit 9221fb5
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 210 deletions.
45 changes: 8 additions & 37 deletions gen/src/ApiGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ export class ApiGenerator extends GenBase {
alias Surrealix.Socket
alias Surrealix.Util
defp exec_method(pid, {method, args, task}, opts \\\\ []) do
Socket.exec_method(pid, {method, args, task}, opts)
defp exec_method(pid, {method, args, task_opts}) do
Socket.exec_method(pid, {method, args, task_opts})
end
@doc """
Expand Down Expand Up @@ -110,71 +110,42 @@ export class ApiGenerator extends GenBase {
genMethod(method: IMethod) {
this.genMethodDoc(method);
if (method.argType == "inline") {
this.genInlineMethod(method);
this.genInlineTaskMethod(method);
}
if (method.argType == "payload") {
this.genPayloadMethod(method);
this.genPayloadTaskMethod(method);
}
}

genPayloadMethod(method: IMethod) {
this.push(`def ${method.name}(pid, payload) do`);
this.push(` exec_method(pid, {"${method.name}", [payload: payload], nil})`);
this.push("end");
this.push("");
}

genPayloadTaskMethod(method: IMethod) {
this.push(
`def ${method.name}(pid, payload, task, opts \\\\ Config.task_opts_default()) do`
`def ${method.name}(pid, payload, task_opts \\\\ Config.task_opts_default()) do`
);
this.push(
` exec_method(pid, {"${method.name}", [payload: payload], task}, opts)`
` exec_method(pid, {"${method.name}", [payload: payload], task_opts})`
);
this.push("end");
this.push("");
}

genInlineMethod(method: IMethod) {
if (method.parameter.length == 0) {
this.push(`def ${method.name}(pid) do`);
}
if (method.parameter.length > 0) {
let params = method.parameter
.map((param) => {
if (param.must) {
return param.name;
}
return `${param.name} \\\\ ${param.default}`;
})
.join(", ");
this.push(`def ${method.name}(pid, ${params}) do`);
}
let params = method.parameter
.map((param) => `${param.name}: ${param.name}`)
.join(", ");
this.push(` exec_method(pid, {"${method.name}", [${params}], nil})`);
this.push("end");
this.push("");
}

genInlineTaskMethod(method: IMethod) {
if (method.parameter.length == 0) {
this.push(
`def ${method.name}(pid, task, opts \\\\ Config.task_opts_default()) do`
`def ${method.name}(pid, task_opts \\\\ Config.task_opts_default()) do`
);
}
if (method.parameter.length > 0) {
let names = method.parameter.map((param) => param.name);
names = names.concat(["task", "opts \\\\ Config.task_opts_default()"]);
names = names.concat(["task_opts \\\\ Config.task_opts_default()"]);
let params = names.join(", ");
this.push(`def ${method.name}(pid, ${params}) do`);
}
let params = method.parameter
.map((param) => `${param.name}: ${param.name}`)
.join(", ");
this.push(` exec_method(pid, {"${method.name}", [${params}], task}, opts)`);
this.push(` exec_method(pid, {"${method.name}", [${params}], task_opts})`);
this.push("end");
this.push("");
}
Expand Down
10 changes: 2 additions & 8 deletions gen/src/MainGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,7 @@ export class MainGenerator extends GenBase {

genPayloadMethod(method: IMethod) {
this.push(`defdelegate ${method.name}(pid, payload), to: Api`);
this.push(`defdelegate ${method.name}(pid, payload, task), to: Api`);
this.push(
`defdelegate ${method.name}(pid, payload, task, opts), to: Api`
);
this.push(`defdelegate ${method.name}(pid, payload, task_opts), to: Api`);
}

genInlineMethod(method: IMethod) {
Expand All @@ -95,10 +92,7 @@ export class MainGenerator extends GenBase {

let params = method.parameter.map((param) => param.name).join(", ");
this.push(`defdelegate ${method.name}(pid, ${directParams}), to: Api`);
this.push(`defdelegate ${method.name}(pid, ${params}, task), to: Api`);
this.push(
`defdelegate ${method.name}(pid, ${params}, task, opts), to: Api`
);
this.push(`defdelegate ${method.name}(pid, ${params}, task_opts), to: Api`);
}
}
genMethodDoc(method: IMethod) {
Expand Down
48 changes: 16 additions & 32 deletions lib/surrealix.ex
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ defmodule Surrealix do
}
"""
defdelegate use(pid, ns, db), to: Api
defdelegate use(pid, ns, db, task), to: Api
defdelegate use(pid, ns, db, task, opts), to: Api
defdelegate use(pid, ns, db, task_opts), to: Api

@doc """
info
Expand Down Expand Up @@ -115,8 +114,7 @@ defmodule Surrealix do
}
"""
defdelegate signup(pid, payload), to: Api
defdelegate signup(pid, payload, task), to: Api
defdelegate signup(pid, payload, task, opts), to: Api
defdelegate signup(pid, payload, task_opts), to: Api

@doc """
signin [ NS, DB, SC, ... ]
Expand Down Expand Up @@ -164,8 +162,7 @@ defmodule Surrealix do
}
"""
defdelegate signin(pid, payload), to: Api
defdelegate signin(pid, payload, task), to: Api
defdelegate signin(pid, payload, task, opts), to: Api
defdelegate signin(pid, payload, task_opts), to: Api

@doc """
authenticate [ token ]
Expand All @@ -187,8 +184,7 @@ defmodule Surrealix do
}
"""
defdelegate authenticate(pid, token), to: Api
defdelegate authenticate(pid, token, task), to: Api
defdelegate authenticate(pid, token, task, opts), to: Api
defdelegate authenticate(pid, token, task_opts), to: Api

@doc """
invalidate
Expand Down Expand Up @@ -229,8 +225,7 @@ defmodule Surrealix do
}
"""
defdelegate let(pid, name, value), to: Api
defdelegate let(pid, name, value, task), to: Api
defdelegate let(pid, name, value, task, opts), to: Api
defdelegate let(pid, name, value, task_opts), to: Api

@doc """
unset [ name ]
Expand All @@ -252,8 +247,7 @@ defmodule Surrealix do
}
"""
defdelegate unset(pid, name), to: Api
defdelegate unset(pid, name, task), to: Api
defdelegate unset(pid, name, task, opts), to: Api
defdelegate unset(pid, name, task_opts), to: Api

@doc """
live [ table ]
Expand Down Expand Up @@ -292,8 +286,7 @@ defmodule Surrealix do
}
"""
defdelegate live(pid, table, diff \\ false), to: Api
defdelegate live(pid, table, diff, task), to: Api
defdelegate live(pid, table, diff, task, opts), to: Api
defdelegate live(pid, table, diff, task_opts), to: Api

@doc """
kill [ queryUuid ]
Expand All @@ -315,8 +308,7 @@ defmodule Surrealix do
}
"""
defdelegate kill(pid, queryUuid), to: Api
defdelegate kill(pid, queryUuid, task), to: Api
defdelegate kill(pid, queryUuid, task, opts), to: Api
defdelegate kill(pid, queryUuid, task_opts), to: Api

@doc """
query [ sql, vars ]
Expand Down Expand Up @@ -362,8 +354,7 @@ defmodule Surrealix do
}
"""
defdelegate query(pid, sql, vars \\ %{}), to: Api
defdelegate query(pid, sql, vars, task), to: Api
defdelegate query(pid, sql, vars, task, opts), to: Api
defdelegate query(pid, sql, vars, task_opts), to: Api

@doc """
select [ thing ]
Expand All @@ -390,8 +381,7 @@ defmodule Surrealix do
}
"""
defdelegate select(pid, thing), to: Api
defdelegate select(pid, thing, task), to: Api
defdelegate select(pid, thing, task, opts), to: Api
defdelegate select(pid, thing, task_opts), to: Api

@doc """
create [ thing, data ]
Expand Down Expand Up @@ -421,8 +411,7 @@ defmodule Surrealix do
}
"""
defdelegate create(pid, thing, data \\ %{}), to: Api
defdelegate create(pid, thing, data, task), to: Api
defdelegate create(pid, thing, data, task, opts), to: Api
defdelegate create(pid, thing, data, task_opts), to: Api

@doc """
insert [ thing, data ]
Expand Down Expand Up @@ -486,8 +475,7 @@ defmodule Surrealix do
}
"""
defdelegate insert(pid, thing, data \\ %{}), to: Api
defdelegate insert(pid, thing, data, task), to: Api
defdelegate insert(pid, thing, data, task, opts), to: Api
defdelegate insert(pid, thing, data, task_opts), to: Api

@doc """
update [ thing, data ]
Expand Down Expand Up @@ -516,8 +504,7 @@ defmodule Surrealix do
}
"""
defdelegate update(pid, thing, data \\ %{}), to: Api
defdelegate update(pid, thing, data, task), to: Api
defdelegate update(pid, thing, data, task, opts), to: Api
defdelegate update(pid, thing, data, task_opts), to: Api

@doc """
merge [ thing, data ]
Expand Down Expand Up @@ -554,8 +541,7 @@ defmodule Surrealix do
}
"""
defdelegate merge(pid, thing, data \\ %{}), to: Api
defdelegate merge(pid, thing, data, task), to: Api
defdelegate merge(pid, thing, data, task, opts), to: Api
defdelegate merge(pid, thing, data, task_opts), to: Api

@doc """
patch [ thing, patches, diff ]
Expand Down Expand Up @@ -600,8 +586,7 @@ defmodule Surrealix do
}
"""
defdelegate patch(pid, thing, patches, diff \\ false), to: Api
defdelegate patch(pid, thing, patches, diff, task), to: Api
defdelegate patch(pid, thing, patches, diff, task, opts), to: Api
defdelegate patch(pid, thing, patches, diff, task_opts), to: Api

@doc """
delete [ thing ]
Expand Down Expand Up @@ -629,6 +614,5 @@ defmodule Surrealix do
}
"""
defdelegate delete(pid, thing), to: Api
defdelegate delete(pid, thing, task), to: Api
defdelegate delete(pid, thing, task, opts), to: Api
defdelegate delete(pid, thing, task_opts), to: Api
end
Loading

0 comments on commit 9221fb5

Please sign in to comment.