Skip to content
This repository was archived by the owner on Jun 12, 2024. It is now read-only.

fix :code generation and type processing #292

Merged
merged 1 commit into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions frontend/lib/api/types/data-contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ export interface ItemUpdate {
soldTime: Date | string;
soldTo: string;
warrantyDetails: string;
/** Sold */
warrantyExpires: Date | string;
}

Expand Down
58 changes: 32 additions & 26 deletions scripts/process-types/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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)
Expand Down