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;
|
||||
soldTo: string;
|
||||
warrantyDetails: string;
|
||||
/** Sold */
|
||||
warrantyExpires: Date | string;
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue