import Base import ./ordinary_query_declaration.bend as Query import ./ordinary_app_composer.bend as Build import ./ordinary_app_registration_fields.bend as Records import ./ordinary_app_records.bend as Leaf import ./ordinary_app_recipe.bend as Tree import ./ordinary_app_resource_field_observation.bend as Leaves import ./world.bend as W import ./system.bend as Sys import ./transaction.bend as T import ./ordinary_resource_field_system.bend as P import ./ordinary_app_resource_field_declaration.bend as D import ./ordinary_app_observation_data.bend as DTO def field_owner(~S:Data,~C:Type,~R:Type,~E:Data,~Args:Type,~Error:Data,~Out:Type,~runner:W.World -> Args -> Sys.Outcome,Error,Out>,~clauses:List<&2,Query.Clause>,slot:String,owner:P.Owner,clauses>) -> Records.Token> & Records.Fields: match owner: case P.Owner{registry}:Records.field_registry(~S,~C,~R,~E,~Args,~Error,~Out,~runner,slot,clauses,registry) # The same ordinary resource declaration drives registration, observation and metadata. def extracted(~S:Data,~C:Type,~R:Type,~E:Data,~Owners:Type,~Rows:Data,~observe:W.World -> W.World & DTO.Observation,~fold:Owners -> Owners & List<&2,Records.Fields>,~Ops:Type -> Type,~Args:Type,~Error:Data,~Out:Type,~Value:Data,~declaration:D.Declaration,~body:@-H:Type -> @-caps:Ops(H) -> Args -> H -> H & (T.Outcome & Out),~Output:Type,done:Build.Registered,Owners & Leaf.Record>>,DTO.Product,Tree.observe_pair(~W.World,~Rows,~Value,~observe,~Leaves.resource_observe(~S,~C,~R,~E,~Ops,~Value,~declaration)),Leaf.fold_pair(~S,~Owners,~Leaf.Record>>,~fold,~Leaf.observed(~S,~Records.Token>))> -> Output,failed:Build.Registered,Owners,Rows,observe,fold> -> Output,world:W.World,owners:Owners,debug:Bool,result:Records.Token> & Records.Fields) -> Output: match result: case (token,fields):done(Build.appended(~S,~W.World,~Owners,~Rows,~observe,~fold,~Leaf.Record>>,~Value,~Leaves.resource_observe(~S,~C,~R,~E,~Ops,~Value,~declaration),~Leaf.observed(~S,~Records.Token>),world,owners,Leaf.Record{token,fields},debug)) def defined(~S:Data,~C:Type,~R:Type,~E:Data,~Owners:Type,~Rows:Data,~observe:W.World -> W.World & DTO.Observation,~fold:Owners -> Owners & List<&2,Records.Fields>,~Ops:Type -> Type,~Args:Type,~Error:Data,~Out:Type,~Value:Data,~declaration:D.Declaration,~body:@-H:Type -> @-caps:Ops(H) -> Args -> H -> H & (T.Outcome & Out),~Output:Type,done:Build.Registered,Owners & Leaf.Record>>,DTO.Product,Tree.observe_pair(~W.World,~Rows,~Value,~observe,~Leaves.resource_observe(~S,~C,~R,~E,~Ops,~Value,~declaration)),Leaf.fold_pair(~S,~Owners,~Leaf.Record>>,~fold,~Leaf.observed(~S,~Records.Token>))> -> Output,failed:Build.Registered,Owners,Rows,observe,fold> -> Output,owners:Owners,recipe:Tree.Recipe,Rows,observe>,debug:Bool,slot:String,result:P.Defined,Sys.Registry,P.clauses(~S,~T.Tx,E>,~Ops(T.Tx,E>),D.ordinary(~S,~C,~R,~E,~Ops,~Value,declaration))>) -> Output: match result: case P.Rejected{world}:failed(Build.Registered{world,owners,recipe,debug}) case P.Defined{world,owner}:extracted(~S,~C,~R,~E,~Owners,~Rows,~observe,~fold,~Ops,~Args,~Error,~Out,~Value,~declaration,~body,~Output,done,failed,world,owners,debug,field_owner(~S,~C,~R,~E,~Args,~Error,~Out,~P.runner(~S,~C,~R,~E,~Ops,~Args,~Error,~Out,~D.ordinary(~S,~C,~R,~E,~Ops,~Value,declaration),~body),~P.clauses(~S,~T.Tx,E>,~Ops(T.Tx,E>),D.ordinary(~S,~C,~R,~E,~Ops,~Value,declaration)),slot,owner)) # Add a selected-resource-field system, deriving access and inspection # from the same declaration/binding. done receives extended registration owners; # failed returns the prior App shape with World/owners retained. Does not run # body or replace a field at registration. Raw schema lenses remain trusted. def register(~S:Data,~C:Type,~R:Type,~E:Data,~Owners:Type,~Rows:Data,~observe:W.World -> W.World & DTO.Observation,~fold:Owners -> Owners & List<&2,Records.Fields>,~Ops:Type -> Type,~Args:Type,~Error:Data,~Out:Type,~Value:Data,~declaration:D.Declaration,~body:@-H:Type -> @-caps:Ops(H) -> Args -> H -> H & (T.Outcome & Out),~Output:Type,done:Build.Registered,Owners & Leaf.Record>>,DTO.Product,Tree.observe_pair(~W.World,~Rows,~Value,~observe,~Leaves.resource_observe(~S,~C,~R,~E,~Ops,~Value,~declaration)),Leaf.fold_pair(~S,~Owners,~Leaf.Record>>,~fold,~Leaf.observed(~S,~Records.Token>))> -> Output,failed:Build.Registered,Owners,Rows,observe,fold> -> Output,app:Build.Registered,Owners,Rows,observe,fold>,name:String,slot:String) -> Output: match app: case Build.Registered{world,owners,recipe,debug}:defined(~S,~C,~R,~E,~Owners,~Rows,~observe,~fold,~Ops,~Args,~Error,~Out,~Value,~declaration,~body,~Output,done,failed,owners,recipe,debug,slot,P.define(~S,~C,~R,~E,~Ops,~Args,~Error,~Out,~D.ordinary(~S,~C,~R,~E,~Ops,~Value,declaration),~body,world,name))