Skip to content

A continuation of HomeBox the inventory and organization system built for the Home User

fix: code generation and type processing (#292)

Haydenda00db0608ff
diff --git a/frontend/lib/api/types/data-contracts.ts b/frontend/lib/api/types/data-contracts.ts
index a4a0d908..949a287f 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 cb9f684d..e92a881c 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(`\?:`): ":",
- }
-
- for regex, replace := range dateTypes([]string{
- "createdAt",
- "updatedAt",
- "soldTime",
- "purchaseTime",
- "warrantyExpires",
- "expiresAt",
- "date",
- }) {
- regexReplace[regex] = replace
+ 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 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)