forked from mirrors/homebox
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.
This commit is contained in:
parent
efd7069fe4
commit
da00db0608
2 changed files with 33 additions and 26 deletions
|
@ -159,6 +159,7 @@ export interface ItemUpdate {
|
||||||
soldTime: Date | string;
|
soldTime: Date | string;
|
||||||
soldTo: string;
|
soldTo: string;
|
||||||
warrantyDetails: string;
|
warrantyDetails: string;
|
||||||
|
/** Sold */
|
||||||
warrantyExpires: Date | string;
|
warrantyExpires: Date | string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,12 +6,23 @@ import (
|
||||||
"regexp"
|
"regexp"
|
||||||
)
|
)
|
||||||
|
|
||||||
func dateTypes(names []string) map[*regexp.Regexp]string {
|
type ReReplace struct {
|
||||||
result := make(map[*regexp.Regexp]string)
|
Regex *regexp.Regexp
|
||||||
for _, name := range names {
|
Text string
|
||||||
result[regexp.MustCompile(fmt.Sprintf(`%s: string`, name))] = fmt.Sprintf(`%s: Date | string`, name)
|
}
|
||||||
|
|
||||||
|
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() {
|
func main() {
|
||||||
|
@ -37,29 +48,24 @@ func main() {
|
||||||
}
|
}
|
||||||
text += string(data)
|
text += string(data)
|
||||||
|
|
||||||
regexReplace := map[*regexp.Regexp]string{
|
replaces := [...]ReReplace{
|
||||||
regexp.MustCompile(` PaginationResultRepo`): " PaginationResult",
|
NewReReplace(` Repo`, " "),
|
||||||
regexp.MustCompile(` Repo`): " ",
|
NewReReplace(` PaginationResultRepo`, " PaginationResult"),
|
||||||
regexp.MustCompile(` Services`): " ",
|
NewReReplace(` Services`, " "),
|
||||||
regexp.MustCompile(` V1`): " ",
|
NewReReplace(` V1`, " "),
|
||||||
regexp.MustCompile(`\?:`): ":",
|
NewReReplace(`\?:`, ":"),
|
||||||
|
NewReDate("createdAt"),
|
||||||
|
NewReDate("updatedAt"),
|
||||||
|
NewReDate("soldTime"),
|
||||||
|
NewReDate("purchaseTime"),
|
||||||
|
NewReDate("warrantyExpires"),
|
||||||
|
NewReDate("expiresAt"),
|
||||||
|
NewReDate("date"),
|
||||||
}
|
}
|
||||||
|
|
||||||
for regex, replace := range dateTypes([]string{
|
for _, replace := range replaces {
|
||||||
"createdAt",
|
fmt.Printf("Replacing '%v' -> '%s'\n", replace.Regex, replace.Text)
|
||||||
"updatedAt",
|
text = replace.Regex.ReplaceAllString(text, replace.Text)
|
||||||
"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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err = os.WriteFile(path, []byte(text), 0644)
|
err = os.WriteFile(path, []byte(text), 0644)
|
||||||
|
|
Loading…
Reference in a new issue