Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

separate rules model from validation #179

Merged
merged 2 commits into from Aug 9, 2018
Merged

Conversation

johnduffell
Copy link
Member

This is just @pvighi doing an experiment/prototype but I couldn't comment without making a PR. This would merge into the main branch not into Master.

s"invalid day of the week: $date is a $dateDayStr. Allowed days are $allowedDaysStr"
}
}
case class DaysOfWeekRule(allowedDays: List[DayOfWeek]) extends DateRule

case class WindowRule(
now: () => LocalDate,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now param could go away from here

now: () => LocalDate,
maybeCutOffDay: Option[DayOfWeek],
maybeStartDelay: Option[Days],
maybeSize: Option[Days]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the last 3 params could be just the WindowRule object

@coveralls
Copy link

coveralls commented Aug 9, 2018

Pull Request Test Coverage Report for Build 178

  • 12 of 30 (40.0%) changed or added relevant lines in 3 files are covered.
  • 3 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.5%) to 69.554%

Changes Missing Coverage Covered Lines Changed/Added Lines %
handlers/new-product-api/src/main/scala/com/gu/newproduct/api/productcatalog/Handler.scala 0 1 0.0%
handlers/new-product-api/src/main/scala/com/gu/newproduct/api/addsubscription/Handler.scala 0 5 0.0%
handlers/new-product-api/src/main/scala/com/gu/newproduct/api/addsubscription/validation/DateValidationRules.scala 12 24 50.0%
Files with Coverage Reduction New Missed Lines %
handlers/new-product-api/src/main/scala/com/gu/newproduct/api/productcatalog/Catalog.scala 3 0.0%
Totals Coverage Status
Change from base Build 165: -0.5%
Covered Lines: 1309
Relevant Lines: 1882

💛 - Coveralls

def contains(date: LocalDate) = {
val isOnOrAfterWindowStart = date.isAfter(start) || date.isEqual(start)
object DateValidator {
def validatorFor(now: () => LocalDate, dateRule: DateRule): LocalDate => ValidationResult[Unit] = dateRule match {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pattern match here makes more sense because DateRule is now a sealed trait - the compiler will warn us if we miss something.

just for background:
there are 3 main ways of doing polymorphism in scala -

  1. is inheritance (which creates a dep from the classes to the polymorphic code, but separates the implementation for each class from each other)
  2. is pattern matching (which reverses the dep - it's now from the blob of polymorphic code to the type, but the implementations are grouped together into one place)
  3. is type classes (which creates a dep from each classes implemention to both the class and the definition, so separates the implementation for each class)

3 produces the most independent code, but it uses implicits, with all their confusing aspects!

Overall I think the way you have proposed is the best solution for now, but we can always revisit.


selectableWindow contains dateToValidate orFailWith errorMessage
}
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this way of splitting up is great, I feel like all the parts have a cohesive responsibliity now and should be easier to test.
I agree that moving stuff around will help a lot but I think in terms of dependencies between parts it's much better 🥇

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just as a side thought now the dependencies are reduced, in terms of organising into packages, we should think whether to group by function (window, days of week) or type (validator, rule, wire catalog) or a combination. I think it would depend on how the remaining connections go, and whether we are likely to make changes by function or by type. The compiler will check it all anyway, so it is just for our convenience how we organise it.

@pvighi pvighi changed the title DO not merge WIP - separate rules model from validation separate rules model from validation Aug 9, 2018
@pvighi pvighi merged commit 74310b3 into dateValidation Aug 9, 2018
@pvighi pvighi deleted the dateValidation_refactor branch August 9, 2018 15:02
@guardian guardian deleted a comment Aug 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants