Skip to content

Commit

Permalink
Merge 1b63343 into 6bd48b8
Browse files Browse the repository at this point in the history
  • Loading branch information
hawx authored Sep 27, 2024
2 parents 6bd48b8 + 1b63343 commit a3e665a
Show file tree
Hide file tree
Showing 41 changed files with 298 additions and 192 deletions.
8 changes: 7 additions & 1 deletion cmd/enumerator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@
// )
//
// we would be able to check whether a value x had not been set to Yes or No by
// using the boolean value returned by x.Empty().
// using the boolean value returned by x.Empty(). It also alters Parse to accept
// "" as, in this case, YesNo(0).
//
// The -bits flag tells enumerator to consider the type as a field of bits. This
// is useful when an emum is defined using 1<<iota. It causes the following
Expand Down Expand Up @@ -678,6 +679,11 @@ func (g *Generator) buildParseMethod(runs [][]Value, typeName string) {
g.Printf(`func Parse%[1]s(s string) (%[1]s, error) {
switch s {
`, typeName)
if g.empty {
g.Printf(` case "":
return %[1]s(0), nil
`, typeName)
}
for _, values := range runs {
for _, value := range values {
g.Printf(` case "%[1]s":
Expand Down
2 changes: 2 additions & 0 deletions cmd/enumerator/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ func (i Day) IsSunday() bool {
func ParseDay(s string) (Day, error) {
switch s {
case "":
return Day(0), nil
case "Monday":
return Monday, nil
case "Tuesday":
Expand Down
2 changes: 2 additions & 0 deletions internal/donor/donordata/enum_novoucherdecision.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions internal/donor/donordata/enum_yesnomaybe.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,21 @@ func HowDoYouKnowYourCertificateProvider(tmpl template.Template, donorStore Dono
}

type howDoYouKnowYourCertificateProviderForm struct {
How lpadata.CertificateProviderRelationship
Error error
How lpadata.CertificateProviderRelationship
}

func readHowDoYouKnowYourCertificateProviderForm(r *http.Request) *howDoYouKnowYourCertificateProviderForm {
how, err := lpadata.ParseCertificateProviderRelationship(page.PostFormString(r, "how"))
how, _ := lpadata.ParseCertificateProviderRelationship(page.PostFormString(r, "how"))

return &howDoYouKnowYourCertificateProviderForm{
How: how,
Error: err,
How: how,
}
}

func (f *howDoYouKnowYourCertificateProviderForm) Validate() validation.List {
var errors validation.List

errors.Error("how", "howYouKnowCertificateProvider", f.Error,
errors.Enum("how", "howYouKnowCertificateProvider", f.How,
validation.Selected())

return errors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,12 @@ func TestHowDoYouKnowYourCertificateProviderFormValidate(t *testing.T) {
errors validation.List
}{
"valid": {
form: &howDoYouKnowYourCertificateProviderForm{},
},
"invalid": {
form: &howDoYouKnowYourCertificateProviderForm{
Error: expectedError,
How: lpadata.Personally,
},
},
"invalid": {
form: &howDoYouKnowYourCertificateProviderForm{},
errors: validation.With("how", validation.SelectError{Label: "howYouKnowCertificateProvider"}),
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,16 @@ func HowShouldAttorneysMakeDecisions(tmpl template.Template, donorStore DonorSto

type howShouldAttorneysMakeDecisionsForm struct {
DecisionsType lpadata.AttorneysAct
Error error
DecisionsDetails string
errorLabel string
detailsErrorLabel string
}

func readHowShouldAttorneysMakeDecisionsForm(r *http.Request, errorLabel, detailsErrorLabel string) *howShouldAttorneysMakeDecisionsForm {
how, err := lpadata.ParseAttorneysAct(page.PostFormString(r, "decision-type"))
how, _ := lpadata.ParseAttorneysAct(page.PostFormString(r, "decision-type"))

return &howShouldAttorneysMakeDecisionsForm{
DecisionsType: how,
Error: err,
DecisionsDetails: page.PostFormString(r, "mixed-details"),
errorLabel: errorLabel,
detailsErrorLabel: detailsErrorLabel,
Expand All @@ -86,7 +84,7 @@ func readHowShouldAttorneysMakeDecisionsForm(r *http.Request, errorLabel, detail
func (f *howShouldAttorneysMakeDecisionsForm) Validate() validation.List {
var errors validation.List

errors.Error("decision-type", f.errorLabel, f.Error,
errors.Enum("decision-type", f.errorLabel, f.DecisionsType,
validation.Selected())

if f.DecisionsType == lpadata.JointlyForSomeSeverallyForOthers {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ func TestHowShouldAttorneysMakeDecisionsFormValidate(t *testing.T) {
}{
"valid": {
form: &howShouldAttorneysMakeDecisionsForm{
errorLabel: "xyz",
DecisionsType: lpadata.Jointly,
errorLabel: "xyz",
},
},
"valid with detail": {
Expand All @@ -243,7 +244,6 @@ func TestHowShouldAttorneysMakeDecisionsFormValidate(t *testing.T) {
},
"invalid": {
form: &howShouldAttorneysMakeDecisionsForm{
Error: expectedError,
errorLabel: "xyz",
},
errors: validation.With("decision-type", validation.SelectError{Label: "xyz"}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,22 @@ func HowShouldReplacementAttorneysStepIn(tmpl template.Template, donorStore Dono

type howShouldReplacementAttorneysStepInForm struct {
WhenToStepIn lpadata.ReplacementAttorneysStepIn
Error error
OtherDetails string
}

func readHowShouldReplacementAttorneysStepInForm(r *http.Request) *howShouldReplacementAttorneysStepInForm {
when, err := lpadata.ParseReplacementAttorneysStepIn(page.PostFormString(r, "when-to-step-in"))
when, _ := lpadata.ParseReplacementAttorneysStepIn(page.PostFormString(r, "when-to-step-in"))

return &howShouldReplacementAttorneysStepInForm{
WhenToStepIn: when,
Error: err,
OtherDetails: page.PostFormString(r, "other-details"),
}
}

func (f *howShouldReplacementAttorneysStepInForm) Validate() validation.List {
var errors validation.List

errors.Error("when-to-step-in", "whenYourReplacementAttorneysStepIn", f.Error,
errors.Enum("when-to-step-in", "whenYourReplacementAttorneysStepIn", f.WhenToStepIn,
validation.Selected())

if f.WhenToStepIn == lpadata.ReplacementAttorneysStepInAnotherWay {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,12 +310,18 @@ func TestHowShouldReplacementAttorneysStepInFormValidate(t *testing.T) {
expectedErrors validation.List
}{
"valid": {
form: &howShouldReplacementAttorneysStepInForm{},
form: &howShouldReplacementAttorneysStepInForm{
WhenToStepIn: lpadata.ReplacementAttorneysStepInWhenAllCanNoLongerAct,
},
},
"invalid": {
"valid with details": {
form: &howShouldReplacementAttorneysStepInForm{
Error: expectedError,
WhenToStepIn: lpadata.ReplacementAttorneysStepInAnotherWay,
OtherDetails: "hey",
},
},
"invalid": {
form: &howShouldReplacementAttorneysStepInForm{},
expectedErrors: validation.With("when-to-step-in", validation.SelectError{Label: "whenYourReplacementAttorneysStepIn"}),
},
"missing other details": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,10 @@ func HowWouldCertificateProviderPreferToCarryOutTheirRole(tmpl template.Template
type howWouldCertificateProviderPreferToCarryOutTheirRoleForm struct {
CarryOutBy lpadata.Channel
Email string
Error error
}

func readHowWouldCertificateProviderPreferToCarryOutTheirRole(r *http.Request) *howWouldCertificateProviderPreferToCarryOutTheirRoleForm {
channel, err := lpadata.ParseChannel(page.PostFormString(r, "carry-out-by"))
channel, _ := lpadata.ParseChannel(page.PostFormString(r, "carry-out-by"))

email := page.PostFormString(r, "email")
if channel.IsPaper() {
Expand All @@ -69,14 +68,13 @@ func readHowWouldCertificateProviderPreferToCarryOutTheirRole(r *http.Request) *
return &howWouldCertificateProviderPreferToCarryOutTheirRoleForm{
CarryOutBy: channel,
Email: email,
Error: err,
}
}

func (f *howWouldCertificateProviderPreferToCarryOutTheirRoleForm) Validate() validation.List {
var errors validation.List

errors.Error("carry-out-by", "howYourCertificateProviderWouldPreferToCarryOutTheirRole", f.Error,
errors.Enum("carry-out-by", "howYourCertificateProviderWouldPreferToCarryOutTheirRole", f.CarryOutBy,
validation.Selected())

if f.CarryOutBy.IsOnline() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,17 +267,7 @@ func TestHowWouldCertificateProviderPreferToCarryOutTheirRoleFormValidate(t *tes
errors: validation.With("email", validation.EnterError{Label: "certificateProvidersEmail"}),
},
"missing": {
form: &howWouldCertificateProviderPreferToCarryOutTheirRoleForm{
CarryOutBy: lpadata.Channel(0),
Error: expectedError,
},
errors: validation.With("carry-out-by", validation.SelectError{Label: "howYourCertificateProviderWouldPreferToCarryOutTheirRole"}),
},
"invalid": {
form: &howWouldCertificateProviderPreferToCarryOutTheirRoleForm{
CarryOutBy: lpadata.Channel(99),
Error: expectedError,
},
form: &howWouldCertificateProviderPreferToCarryOutTheirRoleForm{},
errors: validation.With("carry-out-by", validation.SelectError{Label: "howYourCertificateProviderWouldPreferToCarryOutTheirRole"}),
},
}
Expand Down
22 changes: 10 additions & 12 deletions internal/donor/donorpage/how_would_you_like_to_send_evidence.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,23 @@ func HowWouldYouLikeToSendEvidence(tmpl template.Template, donorStore DonorStore

type evidenceDeliveryForm struct {
EvidenceDelivery pay.EvidenceDelivery
Error error
ErrorLabel string
}

func (f *evidenceDeliveryForm) Validate() validation.List {
var errors validation.List

errors.Error("evidence-delivery", f.ErrorLabel, f.Error,
validation.Selected())

return errors
}

func readHowWouldYouLikeToSendEvidenceForm(r *http.Request) *evidenceDeliveryForm {
evidenceDelivery, err := pay.ParseEvidenceDelivery(form.PostFormString(r, "evidence-delivery"))
evidenceDelivery, _ := pay.ParseEvidenceDelivery(form.PostFormString(r, "evidence-delivery"))

return &evidenceDeliveryForm{
EvidenceDelivery: evidenceDelivery,
Error: err,
ErrorLabel: "howYouWouldLikeToSendUsYourEvidence",
}
}

func (f *evidenceDeliveryForm) Validate() validation.List {
var errors validation.List

errors.Enum("evidence-delivery", f.ErrorLabel, f.EvidenceDelivery,
validation.Selected())

return errors
}
6 changes: 2 additions & 4 deletions internal/donor/donorpage/life_sustaining_treatment.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,20 @@ func LifeSustainingTreatment(tmpl template.Template, donorStore DonorStore) Hand

type lifeSustainingTreatmentForm struct {
Option lpadata.LifeSustainingTreatment
Error error
}

func readLifeSustainingTreatmentForm(r *http.Request) *lifeSustainingTreatmentForm {
option, err := lpadata.ParseLifeSustainingTreatment(page.PostFormString(r, "option"))
option, _ := lpadata.ParseLifeSustainingTreatment(page.PostFormString(r, "option"))

return &lifeSustainingTreatmentForm{
Option: option,
Error: err,
}
}

func (f *lifeSustainingTreatmentForm) Validate() validation.List {
var errors validation.List

errors.Error("option", "ifTheDonorGivesConsentToLifeSustainingTreatment", f.Error,
errors.Enum("option", "ifTheDonorGivesConsentToLifeSustainingTreatment", f.Option,
validation.Selected())

return errors
Expand Down
8 changes: 4 additions & 4 deletions internal/donor/donorpage/life_sustaining_treatment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,12 @@ func TestLifeSustainingTreatmentFormValidate(t *testing.T) {
errors validation.List
}{
"valid": {
form: &lifeSustainingTreatmentForm{},
},
"invalid": {
form: &lifeSustainingTreatmentForm{
Error: expectedError,
Option: lpadata.LifeSustainingTreatmentOptionA,
},
},
"invalid": {
form: &lifeSustainingTreatmentForm{},
errors: validation.With("option", validation.SelectError{Label: "ifTheDonorGivesConsentToLifeSustainingTreatment"}),
},
}
Expand Down
6 changes: 2 additions & 4 deletions internal/donor/donorpage/lpa_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,22 +79,20 @@ func LpaType(tmpl template.Template, donorStore DonorStore, eventClient EventCli

type lpaTypeForm struct {
LpaType lpadata.LpaType
Error error
}

func readLpaTypeForm(r *http.Request) *lpaTypeForm {
lpaType, err := lpadata.ParseLpaType(page.PostFormString(r, "lpa-type"))
lpaType, _ := lpadata.ParseLpaType(page.PostFormString(r, "lpa-type"))

return &lpaTypeForm{
LpaType: lpaType,
Error: err,
}
}

func (f *lpaTypeForm) Validate(hasTrustCorporation bool) validation.List {
var errors validation.List

errors.Error("lpa-type", "theTypeOfLpaToMake", f.Error,
errors.Enum("lpa-type", "theTypeOfLpaToMake", f.LpaType,
validation.Selected())

if f.LpaType.IsPersonalWelfare() && hasTrustCorporation {
Expand Down
8 changes: 4 additions & 4 deletions internal/donor/donorpage/lpa_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,12 +298,12 @@ func TestLpaTypeFormValidate(t *testing.T) {
errors validation.List
}{
"valid": {
form: &lpaTypeForm{},
},
"invalid": {
form: &lpaTypeForm{
Error: expectedError,
LpaType: lpadata.LpaTypePropertyAndAffairs,
},
},
"invalid": {
form: &lpaTypeForm{},
errors: validation.With("lpa-type", validation.SelectError{Label: "theTypeOfLpaToMake"}),
},
"to personal welfare": {
Expand Down
6 changes: 2 additions & 4 deletions internal/donor/donorpage/previous_fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,20 @@ func PreviousFee(tmpl template.Template, payer Handler, donorStore DonorStore) H

type previousFeeForm struct {
PreviousFee pay.PreviousFee
Error error
}

func readPreviousFeeForm(r *http.Request) *previousFeeForm {
previousFee, err := pay.ParsePreviousFee(page.PostFormString(r, "previous-fee"))
previousFee, _ := pay.ParsePreviousFee(page.PostFormString(r, "previous-fee"))

return &previousFeeForm{
PreviousFee: previousFee,
Error: err,
}
}

func (f *previousFeeForm) Validate() validation.List {
var errors validation.List

errors.Error("previous-fee", "howMuchYouPreviouslyPaid", f.Error,
errors.Enum("previous-fee", "howMuchYouPreviouslyPaid", f.PreviousFee,
validation.Selected())

return errors
Expand Down
8 changes: 4 additions & 4 deletions internal/donor/donorpage/previous_fee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,12 @@ func TestPreviousFeeFormValidate(t *testing.T) {
errors validation.List
}{
"valid": {
form: &previousFeeForm{},
},
"invalid": {
form: &previousFeeForm{
Error: expectedError,
PreviousFee: pay.PreviousFeeFull,
},
},
"invalid": {
form: &previousFeeForm{},
errors: validation.With("previous-fee", validation.SelectError{Label: "howMuchYouPreviouslyPaid"}),
},
}
Expand Down
Loading

0 comments on commit a3e665a

Please sign in to comment.