Each exercise starts from exercises/N/start: the day-to-day program with the exercise's examples added, and a stub when there is one. una test . fails there until the exercise is done. Work in the start package: edit the files it names, run una fmt . to put the code in canonical form, then una check . until it passes, then una test ..
17.9.1 Exercise 1: release the domain
The operations team counts notices by mail provider, so it needs the domain of each address, and nothing else. Add a policy email_domain to privacy.una, from secret[PII] Text to public Text, that gives the part after the @, or (none) when there is no single @. The start package already imports it, so una check . reports U1001 until it exists, and it ends with:
exercises/1/start/src/notices.unauna
example"the domain of an address"doemail_domain("rosa@example.invalid")
expectresult == "example.invalid"example"an address with no domain has none"doemail_domain("no address")
expectresult == "(none)"
Hint: write it in two parts, as masked_email is. First the policy, with its five lines: from, to, via, a purpose and an owner. Then a private via function with the same types apart from labels, which splits the address on @, as mask_email does, and gives the second part when there are exactly two.
Check your answer with una test ..
Answer (check, run, test; exercises/1/answer).
Add the policy and its via function to privacy.una:
src/privacy.unadiff
--- a/src/privacy.una+++ b/src/privacy.una@@ -19 +19,18 @@
"{address.slice(start: 0, end: 1)}***@{domain}"
++--- Releases the domain of an email address, and nothing of the mailbox.+pub policy email_domain+ from secret[PII] Text+ to public Text+ via domain_of+ purpose "count notices by mail provider"+ owner support++domain_of(address: secret[PII] Text) -> public Text+=+ let parts = address.split(on: "@")+ if parts.len != 2+ "(none)"+ else+ parts.at(index: 1)+ if none use "(none)"
una test . reports 11 examples passing. una report . now lists the calls of email_domain under information, with their purpose.
17.9.2 Exercise 2: priority notices
Some customers have a priority flag, which the store keeps confidential. Their notice's subject is Priority: order A-1001 has shipped instead of Order A-1001 has shipped. The start package adds the field vip: confidential Bool to Customer, uses subject in notify, and has a stub of it that ignores the flag:
exercises/2/start/src/notices.unauna
--- The subject of a notice. Exercise 2: mark it `Priority:` for a priority customer.pubsubject(customer: Customer, shipment: Shipment) -> confidentialText = "Order {shipment.order} has shipped"
Hint: an if … then … else … on customer.vip, with an interpolated subject in each branch (How the program counter rises). Keep the result type: try -> Text once, and read what the checker says.
Check your answer with una test .. On the start package it reports:
Output
example notices "a priority customer's subject says so" ... FAILED
src/notices.una:166: expect result == "Priority: order A-1001 has shipped"
it is false, with `result` = `"Order A-1001 has shipped"`
11 examples: 10 passed, 1 failed
Answer (check, run, test; exercises/2/answer).
Branch on the flag:
src/notices.unadiff
--- a/src/notices.una+++ b/src/notices.una@@ -38,4 +38,4 @@---- The subject of a notice. Exercise 2: mark it `Priority:` for a priority customer.-pub subject(customer: Customer, shipment: Shipment) -> confidential Text = "Order {shipment.order} has shipped"+--- The subject of a notice. It is decided by the confidential `vip` flag, so it is confidential.+pub subject(customer: Customer, shipment: Shipment) -> confidential Text = if customer.vip then "Priority: order {shipment.order} has shipped" else "Order {shipment.order} has shipped"
The subject is confidential because a confidential flag chose it. The mail service accepts secret[PII], which is above confidential, so the subject may go there, and nowhere public.
17.9.3 Exercise 3: a blocked domain
A partner sends its own notices to the addresses at blocked.example.invalid, so the store must not mail them. The start package adds the variant Blocked(id: Text), a customer at that domain, and this example:
exercises/3/start/src/notices.unauna
example"an address at a blocked domain is rejected before any mail is sent"given@customers = directory(sample_customers())
given@mailer = mail_down()
donotify(Shipment(order: "A-1006", customer: "C-30", carrier: "Parcelo"))
expectfailsBlocked
Like the failure paths of the program, it installs mail_down, so it passes only if notify fails with Blocked before any mail goes out.
The direct way, a branch on customer.email.ends_with(suffix: "@blocked.example.invalid") in notify, does not check. Among the diagnostics una reports:
Diagnostic
[U4002]Error: this `fail` depends on `secret[PII]` data, so its failure would reveal it; return a labeled value instead of failing
Hint:notify needs one public fact about the address: is it at the blocked domain? A policy can release exactly that bit (What to release), and notify can branch on a public Bool and fail. Add the policy to privacy.una, import it in notices.una next to masked_email, and add an else if branch to notify.
Check your answer with una test ..
Answer (check, run, test; exercises/3/answer).
Add a policy to privacy.una that releases one Bool:
src/privacy.unadiff
--- a/src/privacy.una+++ b/src/privacy.una@@ -19 +19,11 @@
"{address.slice(start: 0, end: 1)}***@{domain}"
++--- Releases one fact about an address: whether it is at the blocked domain.+pub policy blocked_domain+ from secret[PII] Text+ to public Bool+ via is_blocked+ purpose "skip the domain whose notices a partner sends"+ owner support++is_blocked(address: secret[PII] Text) -> public Bool = address.ends_with(suffix: "@blocked.example.invalid")
Import it in notices.una, and branch on it in notify:
The policy releases one bit of personal data, on purpose, and the report lists the call. Without it, the fail would reveal that same bit to every caller with nothing to show for it in review.