From da00db0608ffc84fac4597cf28b185ab29d0fd3d Mon Sep 17 00:00:00 2001 From: Hayden <64056131+hay-kot@users.noreply.github.com> Date: Thu, 16 Feb 2023 10:13:09 -0900 Subject: [PATCH] fix: code generation and type processing (#292) regular expressions are order specific and when applied in a random order you can get a variety of outputs. Using a list preserves order and ensures that the data-contracts.ts file is deterministic. --- frontend/lib/api/types/data-contracts.ts | 1 + scripts/process-types/main.go | 58 +++++++++++++----------- 2 files changed, 33 insertions(+), 26 deletions(-) diff --git a/frontend/lib/api/types/data-contracts.ts b/frontend/lib/api/types/data-contracts.ts index a4a0d90..949a287 100644 --- a/frontend/lib/api/types/data-contracts.ts +++ b/frontend/lib/api/types/data-contracts.ts @@ -159,6 +159,7 @@ export interface ItemUpdate { soldTime: Date | string; soldTo: string; warrantyDetails: string; + /** Sold */ warrantyExpires: Date | string; } diff --git a/scripts/process-types/main.go b/scripts/process-types/main.go index cb9f684..e92a881 100644 --- a/scripts/process-types/main.go +++ b/scripts/process-types/main.go @@ -6,12 +6,23 @@ import ( "regexp" ) -func dateTypes(names []string) map[*regexp.Regexp]string { - result := make(map[*regexp.Regexp]string) - for _, name := range names { - result[regexp.MustCompile(fmt.Sprintf(`%s: string`, name))] = fmt.Sprintf(`%s: Date | string`, name) +type ReReplace struct { + Regex *regexp.Regexp + Text string +} + +func NewReReplace(regex string, replace string) ReReplace { + return ReReplace{ + Regex: regexp.MustCompile(regex), + Text: replace, + } +} + +func NewReDate(dateStr string) ReReplace { + return ReReplace{ + Regex: regexp.MustCompile(fmt.Sprintf(`%s: string`, dateStr)), + Text: fmt.Sprintf(`%s: Date | string`, dateStr), } - return result } func main() { @@ -37,29 +48,24 @@ func main() { } text += string(data) - regexReplace := map[*regexp.Regexp]string{ - regexp.MustCompile(` PaginationResultRepo`): " PaginationResult", - regexp.MustCompile(` Repo`): " ", - regexp.MustCompile(` Services`): " ", - regexp.MustCompile(` V1`): " ", - regexp.MustCompile(`\?:`): ":", + replaces := [...]ReReplace{ + NewReReplace(` Repo`, " "), + NewReReplace(` PaginationResultRepo`, " PaginationResult"), + NewReReplace(` Services`, " "), + NewReReplace(` V1`, " "), + NewReReplace(`\?:`, ":"), + NewReDate("createdAt"), + NewReDate("updatedAt"), + NewReDate("soldTime"), + NewReDate("purchaseTime"), + NewReDate("warrantyExpires"), + NewReDate("expiresAt"), + NewReDate("date"), } - for regex, replace := range dateTypes([]string{ - "createdAt", - "updatedAt", - "soldTime", - "purchaseTime", - "warrantyExpires", - "expiresAt", - "date", - }) { - regexReplace[regex] = replace - } - - for regex, replace := range regexReplace { - fmt.Printf("Replacing '%v' -> '%s'\n", regex, replace) - text = regex.ReplaceAllString(text, replace) + for _, replace := range replaces { + fmt.Printf("Replacing '%v' -> '%s'\n", replace.Regex, replace.Text) + text = replace.Regex.ReplaceAllString(text, replace.Text) } err = os.WriteFile(path, []byte(text), 0644)