- Float full stack: literals (fraction/exponent; `0..10` still a range), f64
opcodes 34-41, @table column, WAL bit-exact replay, json fractions in and
shortest-round-trip out. IEEE-quiet — FDIV never traps where DIV does.
- Bytes: a wo_str with its own class id, so alloc/free/copy are shared but no
Text builtin accepts one; len/at/slice/eq/concat, base64 both ways, json
boundary as base64; TEXT_COPY preserves the kind.
- No implicit Int/Float mixing (WO-E201 in the typechecker, not the emitter,
which picks the opcode from one side and would misread the other).
- One IEEE deviation: float_cmp total order (NaN last, -0.0 == +0.0) for
indexes and order-by, keys canonicalized to match. `?Float` nil is a
reserved quiet NaN — the zero word is +0.0, WO_NIL_SCALAR's bits are -2.0.
- Renderer prefers fixed over exponential in 1e-6..1e21: pure shortest makes
a price of 900.0 read `9e+02`. One renderer for interp/json/float_to_text.
- Fixed en route: lexer double-counted the leading digit; is_scalar_shaped
took Float/Bytes as Int-shaped; Bytes ownership needed a shared heap-scalar
predicate or temps never dropped; order-by bit-compared negatives backwards.
- Iteration 17: `kind = "library"` (absent = program; bad value = WO-E109),
entry-less check mode retiring the `--emit` workaround, Go's `internal/` as
WO-E108 at the consumer's `use`. Driver-only; VM/.wob/GC untouched.
- Framework reorg: internal/{parse,serve}.wo; http/form.wo split out to keep
media_type/form_values public (parse.wo had grown public surface).
- Docs: link audit (97 -> 88 broken, conflict markers resolved, 2 duplicate
stories removed), 00-code-review verified 26/27, iterations re-sequenced.
- Also carries the pre-staged pub(read)/using/#if work from the index.
- Gates: corpus 103/0, test_wal 156/0, web-app 26/0, oop-accept ALL MET.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
147 lines
5.6 KiB
Text
147 lines
5.6 KiB
Text
-- web-app — the storefront: writeonce-framework (via [deps]) + @table
|
|
-- persistence. Every handler is a class satisfying Handler; the auth gate is
|
|
-- a Middleware; the data layer is the language's own database — no ORM, no
|
|
-- separate process, one binary.
|
|
use env
|
|
use json
|
|
use framework
|
|
use framework/http
|
|
use framework/router
|
|
|
|
-- decode target for POST /products, encode shape for every product answer
|
|
typedef ProductView = { name: Text, price: Float, stock: Int }
|
|
typedef NewOrder = { product: Text, qty: Int }
|
|
|
|
fn view_json(name: Text, price: Float, stock: Int) -> Text {
|
|
return json.encode(ProductView { name: name, price: price, stock: stock });
|
|
}
|
|
|
|
class ListProducts {
|
|
pad: Int
|
|
fn handle(req: Req) -> Resp {
|
|
let body = "[";
|
|
let first = true;
|
|
for p in from x in Product order by x.name select x {
|
|
if first == false { body = body .. ","; }
|
|
first = false;
|
|
body = body .. view_json(p.name, p.price, p.stock);
|
|
}
|
|
return ok_json(body .. "]");
|
|
}
|
|
}
|
|
|
|
class ShowProduct {
|
|
pad: Int
|
|
fn handle(req: Req) -> Resp {
|
|
let name = req.params["name"];
|
|
if name == nil { return bad_request("no name"); }
|
|
let hits = from p in Product where p.name == name take 1 select p;
|
|
if len(hits) == 0 { return not_found(); }
|
|
let p = hits[0];
|
|
return ok_json(view_json(p.name, p.price, p.stock));
|
|
}
|
|
}
|
|
|
|
-- Accepts THREE bodies: multipart/form-data (curl -F), a form post
|
|
-- (application/x-www-form-urlencoded), and JSON — same insert either way.
|
|
fn create_product(name: Text, price: Float, stock: Int) -> Resp {
|
|
let made = try insert Product { name: name, price: price, stock: stock }
|
|
catch (e) nil;
|
|
if made == nil { return conflict("product name already exists"); }
|
|
return created_json(view_json(name, price, stock));
|
|
}
|
|
|
|
class CreateProduct {
|
|
pad: Int
|
|
fn handle(req: Req) -> Resp {
|
|
if media_type(req) == "multipart/form-data" {
|
|
let ps = multipart_parts(req);
|
|
if ps == nil { return bad_request("unreadable multipart body"); }
|
|
let name = part_named(ps, "name");
|
|
if name == nil { return bad_request("multipart needs name, price, stock"); }
|
|
let pstr = part_named(ps, "price");
|
|
if pstr == nil { return bad_request("multipart needs name, price, stock"); }
|
|
let sstr = part_named(ps, "stock");
|
|
if sstr == nil { return bad_request("multipart needs name, price, stock"); }
|
|
-- parse_float answers NaN for unparseable input rather than a ?Float:
|
|
-- "not a number" is already a Float value, and NaN != NaN is the test
|
|
let price = parse_float(pstr);
|
|
if price != price { return bad_request("price must be a number"); }
|
|
let stock = parse_int(sstr);
|
|
if stock == nil { return bad_request("stock must be a number"); }
|
|
return create_product(name, price, stock);
|
|
}
|
|
if media_type(req) == "application/x-www-form-urlencoded" {
|
|
let f = form_values(req);
|
|
if f == nil { return bad_request("unreadable form body"); }
|
|
let name = f["name"];
|
|
if name == nil { return bad_request("form needs name, price, stock"); }
|
|
let ps = f["price"];
|
|
if ps == nil { return bad_request("form needs name, price, stock"); }
|
|
let ss = f["stock"];
|
|
if ss == nil { return bad_request("form needs name, price, stock"); }
|
|
let price = parse_float(ps);
|
|
if price != price { return bad_request("price must be a number"); }
|
|
let stock = parse_int(ss);
|
|
if stock == nil { return bad_request("stock must be a number"); }
|
|
return create_product(name, price, stock);
|
|
}
|
|
let v = json.decode(req.body) as ProductView;
|
|
if v == nil { return bad_request("body must be {name, price, stock}"); }
|
|
return create_product(v.name, v.price, v.stock);
|
|
}
|
|
}
|
|
|
|
class CreateOrder {
|
|
pad: Int
|
|
fn handle(req: Req) -> Resp {
|
|
let v = json.decode(req.body) as NewOrder;
|
|
if v == nil { return bad_request("body must be {product, qty}"); }
|
|
if v.qty < 1 { return bad_request("qty must be positive"); }
|
|
let hits = from p in Product where p.name == v.product take 1 select p;
|
|
if len(hits) == 0 { return not_found(); }
|
|
insert Order { product: hits[0], qty: v.qty };
|
|
return created_json("{\"ok\":true}");
|
|
}
|
|
}
|
|
|
|
class DeleteProduct {
|
|
pad: Int
|
|
fn handle(req: Req) -> Resp {
|
|
let name = req.params["name"];
|
|
if name == nil { return bad_request("no name"); }
|
|
let hits = from p in Product where p.name == name take 1 select p;
|
|
if len(hits) == 0 { return not_found(); }
|
|
let gone = try delete hits[0] catch (e) nil;
|
|
if gone == nil { return conflict("orders still reference this product"); }
|
|
return ok_json("{\"deleted\":true}");
|
|
}
|
|
}
|
|
|
|
fn main(args: multi Text) -> Int {
|
|
if len(args) < 1 {
|
|
print_err("usage: web-app <port> (WA_TOKEN and WO_DATA must be set)");
|
|
return 2;
|
|
}
|
|
let port = parse_int(args[0]);
|
|
if port == nil {
|
|
print_err("web-app: <port> must be a number");
|
|
return 2;
|
|
}
|
|
let token = env.get("WA_TOKEN");
|
|
if token == nil {
|
|
print_err("web-app: WA_TOKEN is required (the auth middleware's bearer token)");
|
|
return 2;
|
|
}
|
|
|
|
let app = App { middleware: [], routes: [] };
|
|
-- the framework's Bearer mechanism: constant-time compare, principal
|
|
-- attached to req.principal for handlers that want "who is this"
|
|
app.use_mw(Mw { m: BearerAuth { token: token, principal: "api" } });
|
|
app.get("/products", ListProducts { pad: 0 });
|
|
app.get("/products/:name", ShowProduct { pad: 0 });
|
|
app.post("/products", CreateProduct { pad: 0 });
|
|
app.post("/orders", CreateOrder { pad: 0 });
|
|
app.delete_("/products/:name", DeleteProduct { pad: 0 });
|
|
return app.serve("127.0.0.1", port);
|
|
}
|