yumemi/gen/emit/typed_read
行の写しを生成器が書く読み(B1a・B1b、0.16.0、server.reads の Read)。returns が Entity(List(e.E)・Option(e.E)・
e.E)の読みは、codec の Entity の decoder(storage の写像を通した列)で行を写す ── runtime の読みの表に
'<service>/<query>':{key,args,allow:[],out,columns} の行を足し、口は gen/reads/<service>.gleam に ManualRead と
同じ形で並べる。SQL は ★ の db/queries/<service>/<query>.sql($n は args の並びの順)。
args の型の字は手書きの verb の口と同じ写しの表(manual_verb.slot_of)で引く。List(…) は jsonb の穴で、中の値型は
要素ごとに hook encode_<名> を通す。yumemi.At・yumemi.NewKey は読みには書けない。
e.E は 1 行を要り、0 行なら 404 not_found。Option(e.E) は先頭の 1 行。runtime は先頭の行に decoder の読む列が
揃うかを見て、欠ければ列を名指して落ちる(SELECT * の読みもここで止まる)。
B1b:returns がアプリの record(src/ の下の構成子 1 つの record、Entity でない)なら欄の名で列を引き、欄の型を
写しの表(値型・Option・List・Key(<Entity>))で写す。値型(Option(値型)・List(値型)・素の型)は 1 列で写す。
型は imports と、record の module の import・型の別名(pub type X = …)を辿って引く。写しは runtime の表の
out に ['row', r=>…](行から値を組む JS の式)で書き、record の module は gen/runtime.mjs が import する。
B1c:組 #(…)(字のままと、別名 pub type X = #(…) を辿ったもの)は列の位置で写す ── 組の各欄は record の欄と同じ
写しの表、width は組の大きさ。Option でない欄・素の値型・組の欄の列が NULL なら黙って値にしない:runtime の表の
nonnull(列の名か位置)を runtime が見て、列を名指して落ちる(read.columns と同じ 503)。List の欄の要素の NULL
も写しが名指して落ちる。Entity の Read も、Option でない Property の列を nonnull に書く(codec の decoder は
変えない ── 宣言の無い app の読みは変わらない)。List の Property(jsonb の並び)の NULL は codec と同じく []。
第 68 条 型から写す読み(exit 4、SQL の file が無いのは exit 3):service が宣言に無い、Service の query と同じ名・
同じ Service に同じ query が 2 つ(ManualRead と合わせて)、query・args の名が口に使えない、args の型の字が
写しの表に無い、returns の module が imports に無い、Entity の decoder が無い・Durable Object に置く Entity、
SQL が生成の SQL か root の 1 文を指す、args より多い $n、SELECT の列が Entity の欄の列と食い違う(欠けた欄・
余った列)、列の名が字から分からない式(AS が無い)。SELECT *・t.* は生成では見ず、runtime が列を見る。
B1b で足したもの:returns の型が引けない・写せない(sum・opaque・型の引数を持つ型、Entity を包む別名、中の
Option・List)、record の欄が写せない(入れ子の record・sum・組・Entity、2 段の包み)、SELECT の列が record の欄と
食い違う、値型の読みの SELECT の列が 2 つ以上。
B1c で足したもの:組の欄が写せない(入れ子の組・record・sum・Entity、2 段の包み)、SELECT の列の数が組の大きさと
違う、組の読みの SELECT の列の名が重なる(行の object で 1 つに潰れる)・数字だけ(JS が鍵を並べ替える)。
宣言の無い app は 1 行も出さず、生成物も変えない。
Types
SELECT の 1 列。Named は列の名(引用の無い名は小文字)、Star は *・t.*、Unnamed は名が字から分からない式
pub type Column {
Named(String)
Star
Unnamed(String)
}
Constructors
-
Named(String) -
Star -
Unnamed(String)
型の引けなさ。Unwritable は写せない理由
pub type Problem {
Unwritable(String)
}
Constructors
-
Unwritable(String)
returns の読み。Entity は Rows、Entity の module が imports に無ければ Unimported、ほかは Typed(アプリの
record・値型・組。inner は包みを外した字で、target が units で引く)
pub type Returns {
Rows(entity: model.Entity, shape: Shape)
Unimported(entity: model.Entity, written: String)
Typed(shape: Shape, inner: String)
}
Constructors
-
Rows(entity: model.Entity, shape: Shape) -
Unimported(entity: model.Entity, written: String) -
Typed(shape: Shape, inner: String)
returns の形。List(e.E)・Option(e.E)・e.E
pub type Shape {
Many
Maybe
One
}
Constructors
-
Many -
Maybe -
One
returns の中身。RecordOf は record の module の道・構成子の名・欄(写せない欄は Error に型の字)、Single は値型、
TupleOf は組の欄(位置の順、写せない欄は Error に型の字と理由。B1c)
pub type Target {
RecordOf(
path: String,
name: String,
fields: List(#(String, Result(Copy, String))),
)
Single(Copy)
TupleOf(elements: List(Result(Copy, #(String, String))))
}
Constructors
Values
pub fn hash_rows(reads: List(model.Read)) -> String
口の module の入力の hash に足す Read の行(宣言を替えれば header の hash が変わる)
pub fn merge_imports(lines: List(String)) -> List(String)
import の行の中身(<module>[.{…}][ as <名>])を、module と入る名(別名か道の末尾)の組ごとに 1 行へ(最初に出た順、
名は和)。同じ module を同じ名で 2 度入れると Gleam の build が落ちるが、別名の違う 2 行は警告で通るので束ねない
(束ねると片方の名が消える)。{…} の中の as(type Note as N)は名の側に残す。emit/reads の口の import の束ねも引く
pub fn mouth_reads(
app: model.App,
service: String,
) -> List(#(model.Read, Bool))
Service の Read のうち口を書くもの(returns が Entity か、アプリの record・値型・組)と、口に Option の import を
足すか。module が imports に無い Entity は口を書かない(第 68 条で止まる)
pub fn notes(
app: model.App,
units: List(source.Unit),
app_queries: List(#(String, String)),
keyed: List(String),
) -> List(stop.Note)
第 68 条の行。app_queries は app の db/queries/**(道は .sql 無し)、keyed は生成の key の在る
Entity の型名(第 63 条と同じ判定)
pub fn returns_of(app: model.App, read: model.Read) -> Returns
pub fn runtime_imports(
app: model.App,
units: List(source.Unit),
) -> String
gen/runtime.mjs の頭に足す record の module の import(record を返す Read が無ければ空)
pub fn runtime_rows(
app: model.App,
units: List(source.Unit),
) -> List(String)
runtime の読みの表の行(emit/runtime の reads に足す)。穴は args の順に ['enc', 値型](Key と素の型は
['enc'])、List(…) は ['json', 値型]。Entity の out は ['list'|'option'|'single',['entity','<module>']]、
columns は decoder が読む列(hook decode_<module> の Entity は書かない)。record・値型・組の out は
['row', r=>…] の写しで、record は columns に欄の名、値型は width:1(1 列)、組は width:<組の大きさ>。
nonnull は NULL を許さない列(Entity・record は列の名、値型・組は列の位置。B1c)
pub fn select_columns(sql: String) -> Result(List(Column), Nil)
SQL の最初の深さ 0 の SELECT の列。SELECT が深さ 0 に無ければ Error(生成では見ない)
pub fn target(
app: model.App,
units: List(source.Unit),
read: model.Read,
inner: String,
) -> Result(Target, Problem)
returns の包みを外した字を、宣言の imports と record の module の import・型の別名を辿って引く