Skip to content

Commit

Permalink
Merge pull request starlight-go#12 from awans/rpc
Browse files Browse the repository at this point in the history
RPC, Updates, Starlark improvements
  • Loading branch information
chasehensel authored Jun 20, 2020
2 parents 0f67d36 + 16b9e67 commit 340e7b3
Show file tree
Hide file tree
Showing 48 changed files with 1,633 additions and 254 deletions.
6 changes: 6 additions & 0 deletions client/catalog/src/app/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import DatatypeNew from './datatypes/DatatypeNew.svelte';
import DatatypeList from './datatypes/DatatypeList.svelte';
import DatatypeDetail from './datatypes/DatatypeDetail.svelte';
import RPCNew from './rpc/RPCNew.svelte';
import RPCList from './rpc/RPCList.svelte';
import RPCDetail from './rpc/RPCDetail.svelte';
import Repl from './repl/Repl.svelte';
import Breadcrumbs from './Breadcrumbs.svelte';
import LogList from './LogList.svelte';
Expand All @@ -22,6 +25,9 @@
"/datatypes": DatatypeList,
"/datatypes/new": DatatypeNew,
"/repl": Repl,
"/rpc/:id": RPCDetail,
"/rpcs": RPCList,
"/rpcs/new": RPCNew,
"/log": LogList,
"/": ModelList,
};
Expand Down
1 change: 1 addition & 0 deletions client/catalog/src/app/Nav.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
{name:"Models", path:'/models'},
{name:"Datatypes", path: '/datatypes'},
{name:"Repl", path: '/repl'},
{name:"RPCs", path: '/rpcs'},
{name:"Log", path:"/log"}
];
</script>
Expand Down
12 changes: 10 additions & 2 deletions client/catalog/src/app/repl/Repl.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,18 @@ function setUpCM() {
async function runRepl() {
const d = await client.repl({input: cm.getValue().trim()});
if(repl.getValue() == "") {
repl.setValue(">>> " + cm.getValue().trim() + "\n" + d.output);
if (d.output == "") {
repl.setValue(">>> " + cm.getValue().trim());
} else {
repl.setValue(">>> " + cm.getValue().trim() + "\n" + d.output);
}
repl.setOption("styleActiveLine", true);
} else {
repl.setValue(repl.getValue() + "\n>>> " + cm.getValue().trim() + "\n" + d.output);
if (d.output == "") {
repl.setValue(repl.getValue() + "\n>>> " + cm.getValue().trim());
} else {
repl.setValue(repl.getValue() + "\n>>> " + cm.getValue().trim() + "\n" + d.output);
}
}
repl.setCursor(repl.lastLine(), 0);
cm.focus();
Expand Down
70 changes: 70 additions & 0 deletions client/catalog/src/app/rpc/RPCDetail.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<script>
export let params;
import client from '../../data/client.js';
import {Runtime} from '../../data/enums.js';
import { breadcrumbStore } from '../stores.js';
import {cap} from '../util.js';
import { getContext } from 'svelte'
import HLRowButton from '../../ui/HLRowButton.svelte';
import HLTable from '../../ui/HLTable.svelte';
import HLCodeMirror from '../../ui/HLCodeMirror.svelte';
import HLButton from '../../ui/HLButton.svelte';
import HLRow from '../../ui/HLRow.svelte';
import HLTextBig from '../../ui/HLTextBig.svelte';
import HLSelect from '../../ui/HLSelect.svelte';
let id = params.id;
let load = client.rpc.findOne({where: {id: id}, include: {code: true}});
var cm;
var name = "code";
var code = "";
load.then(obj => {
breadcrumbStore.set(
[{
href: "/rpcs",
text: "RPCs",
}, {
href: "/rpc/" + id,
text: cap(obj.name),
}]
);
code = obj.code.code;
});
function setUpCM() {
cm = getContext(name);
cm.setValue(code);
cm.setOption("readOnly", true);
}
</script>

<style>
.box {
margin: 1em 1.5em;
}
h1 {
font-size: var(--scale-3);
font-weight: 600;
}
h2 {
font-size: var(--scale--1);
font-weight: 500;
line-height: 1;
}
</style>

<div class="box">
{#await load}
&nbsp;
{:then rpc}
<h1>{cap(rpc.name)}</h1>
<HLTable>
<h2>Runtime: {Runtime[rpc.code.runtime]}</h2>
</HLTable>
<HLCodeMirror name={name} on:initialized={setUpCM}></HLCodeMirror>
{:catch error}
<div>Error..</div>
{/await}
</div>

68 changes: 68 additions & 0 deletions client/catalog/src/app/rpc/RPCList.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<script>
import client from '../../data/client.js';
import {Runtime} from '../../data/enums.js';
import {cap} from '../util.js';
let load = client.rpc.findMany({ include: {
code: true,
}
});
import { breadcrumbStore } from '../stores.js';
breadcrumbStore.set(
[{
href: "/rpcs",
text: "RPCs",
}]
);
</script>

<style>
.box {
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
.stuff {
}
a.object-box {
display: flex;
flex-direction: column;
color: inherit;
width: 150px;
padding: 1em 1.5em;
}
a.object-box:hover {
background: var(--background-highlight);
}
.spacer {
width: 0;
}
.obj-title{
font-weight: 600;
}
</style>

<div class="box">
{#await load}
&nbsp;
{:then rpcs}
{#each rpcs as rpc}
<a href="/rpc/{rpc.id}" class="object-box">
<div class="obj-title">{cap(rpc.name)}</div>
<div>{Runtime[rpc.code.runtime]}</div>
</a>
<div class="spacer"/>
{/each}
<a href="/rpcs/new" class="object-box">
<div>+ Add</div>
</a>
<div class="spacer"/>
{:catch error}
<div>Error..</div>
{/await}
</div>
79 changes: 79 additions & 0 deletions client/catalog/src/app/rpc/RPCNew.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<script>
import client from '../../data/client.js';
import {restrictToIdent} from '../util.js';
import { breadcrumbStore } from '../stores.js';
import { getContext } from 'svelte'
import { Storage } from '../../data/enums.js';
import HLButton from '../../ui/HLButton.svelte';
import HLRowButton from '../../ui/HLRowButton.svelte';
import HLRow from '../../ui/HLRow.svelte';
import HLTextBig from '../../ui/HLTextBig.svelte';
import HLSelect from '../../ui/HLSelect.svelte';
import HLTable from '../../ui/HLTable.svelte';
import HLCodeMirror from '../../ui/HLCodeMirror.svelte';
breadcrumbStore.set(
[{
href: "/rpcs",
text: "RPCs",
}, {
href: "/rpcs/new",
text: "New",
}]
);
const newRPCOp = {
name: "",
code : {
create : {
name : "",
runtime: 2,
code: "",
functionSignature: 1,
}
}
}
var cm;
var name = "code";
function setUpCM() {
cm = getContext(name);
cm.setCursor({line: 0, ch: 0});
cm.focus();
};
import {router} from '../router.js';
async function saveRPC() {
newRPCOp.code.create.name = newRPCOp.name;
newRPCOp.code.create.code = cm.getValue();
const d = await client.rpc.create({data: newRPCOp});
router.route("/rpcs");
}
</script>

<style>
.box {
margin: 1em 1.5em;
}
h1 {
font-size: var(--scale-3);
font-weight: 600;
}
h2 {
font-size: var(--scale--1);
font-weight: 500;
line-height: 1;
}
.v-space{
height: .5em;
}
</style>

<div class="box">
<HLTextBig placeholder="Name" bind:value={newRPCOp.name} restrict={restrictToIdent}/>
<h2>RPC</h2>
<HLCodeMirror name={name} on:initialized={setUpCM}></HLCodeMirror>
<HLRowButton on:click={saveRPC}>
Save
</HLRowButton>
</div>
6 changes: 6 additions & 0 deletions client/catalog/src/data/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ const CoreApi = {
findOne: {},
findMany: {},
},
rpc:
{
create: {},
findOne: {},
findMany: {},
},
}

class HttpRpcClient {
Expand Down
3 changes: 3 additions & 0 deletions client/catalog/src/ui/HLCodeMirror.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import CodeMirror from 'codemirror';
import 'codemirror/mode/python/python.js';
import 'codemirror/addon/selection/active-line.js';
import 'codemirror/addon/edit/closebrackets.js';
import 'codemirror/addon/comment/comment.js';
const dispatch = createEventDispatcher();
onMount(async () => {
Expand All @@ -14,6 +16,7 @@
lineNumbers: true,
indentUnit: 4,
theme: "duotone-dark",
autoCloseBrackets: true
});
setContext(name, cm);
dispatch('initialized');
Expand Down
4 changes: 2 additions & 2 deletions internal/api/find_many.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ func (s FindManyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) (err
s.bus.Publish(lib.ParseRequest{Request: request})

recs := request.Operation.Apply(tx)
var rData []IncludeResult

rData := []IncludeResult{}
for _, rec := range recs {
responseData := request.Include.Resolve(tx, rec)
rData = append(rData, responseData)
}
response := FindManyResponse{Data: rData}

// write out the response
bytes, _ := jsoniter.Marshal(&response)
_, _ = w.Write(bytes)
Expand Down
3 changes: 2 additions & 1 deletion internal/api/find_many_op_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ func addTestData(appDB db.DB) {
func toAgeList(sts []db.Record) []int64 {
var ages []int64
for _, st := range sts {
ages = append(ages, st.Get("age").(int64))
age, _ := st.Get("age")
ages = append(ages, age.(int64))
}
return ages
}
Expand Down
10 changes: 8 additions & 2 deletions internal/api/include_op.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,21 @@ func resolve(tx db.Tx, ir *IncludeResult, i Inclusion) error {
ir.SingleIncludes[b.Name()] = hit
case db.BelongsTo:
// FK on this side
thisFK := rec.GetFK(b.Name())
thisFK, err := rec.GetFK(b.Name())
if err != nil {
return err
}
hit, err := tx.FindOne(d.ModelID(), db.Eq("id", thisFK))
if err != nil {
return err
}
ir.SingleIncludes[b.Name()] = hit
case db.HasMany:
// FK on the other side
hits := tx.FindMany(d.ModelID(), db.EqFK(d.Name(), id))
hits, err := tx.FindMany(d.ModelID(), db.EqFK(d.Name(), id))
if err != nil {
return err
}
ir.MultiIncludes[b.Name()] = hits
case db.HasManyAndBelongsToMany:
panic("Not implemented")
Expand Down
10 changes: 10 additions & 0 deletions internal/api/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ func (m *Module) ProvideRoutes() []lib.Route {
Pattern: "/api/{modelName}.create",
Handler: lib.ErrorHandler(CreateHandler{DB: m.db, Bus: m.b}),
},
lib.Route{
Name: "Update",
Pattern: "/api/{modelName}.update",
Handler: lib.ErrorHandler(UpdateHandler{DB: m.db, Bus: m.b}),
},
lib.Route{
Name: "UpdateMany",
Pattern: "/api/{modelName}.updateMany",
Handler: lib.ErrorHandler(UpdateManyHandler{DB: m.db, Bus: m.b}),
},
}
}

Expand Down
Loading

0 comments on commit 340e7b3

Please sign in to comment.