Skip to content

Commit c937e0f

Browse files
author
Marvin Zhang
committed
refactor: enhance Spider model and string utility functions
- Updated the Spider model to introduce a new SpiderTemplateParams struct for improved template handling. - Refactored string utility functions in utils/string.go to include a new replaceChars function, streamlining character replacement across multiple functions. - Enhanced ToSnakeCase and ToKebabCase functions to utilize the new replaceChars function for better maintainability and readability. - Added splitStringWithQuotes function to facilitate string manipulation with quotes, improving overall utility in string processing.
1 parent c3c629a commit c937e0f

File tree

2 files changed

+71
-24
lines changed

2 files changed

+71
-24
lines changed

core/models/models/spider.go

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,21 @@ import (
77
type Spider struct {
88
any `collection:"spiders"`
99
BaseModel[Spider] `bson:",inline"`
10-
Name string `json:"name" bson:"name"` // spider name
11-
ColId primitive.ObjectID `json:"col_id" bson:"col_id"` // data collection id (deprecated) # TODO: remove this field in the future
12-
ColName string `json:"col_name,omitempty" bson:"col_name"` // data collection name
13-
DbName string `json:"db_name,omitempty" bson:"db_name"` // database name
14-
DataSourceId primitive.ObjectID `json:"data_source_id" bson:"data_source_id"` // data source id
15-
DataSource *Database `json:"data_source,omitempty" bson:"-"` // data source
16-
Description string `json:"description" bson:"description"` // description
17-
ProjectId primitive.ObjectID `json:"project_id" bson:"project_id"` // Project.Id
18-
Mode string `json:"mode" bson:"mode"` // default Task.Mode
19-
NodeIds []primitive.ObjectID `json:"node_ids" bson:"node_ids"` // default Task.NodeIds
20-
GitId primitive.ObjectID `json:"git_id" bson:"git_id"` // related Git.Id
21-
GitRootPath string `json:"git_root_path" bson:"git_root_path"`
22-
Git *Git `json:"git,omitempty" bson:"-"`
23-
Template string `json:"template,omitempty" bson:"template,omitempty"` // spider template
24-
TemplateParams *struct {
25-
SpiderName string `json:"spider_name,omitempty" bson:"spider_name,omitempty"`
26-
StartUrls string `json:"start_urls,omitempty" bson:"start_urls,omitempty"`
27-
Domains string `json:"domains,omitempty" bson:"domains,omitempty"`
28-
} `json:"template_params,omitempty" bson:"template_params,omitempty"`
10+
Name string `json:"name" bson:"name"` // spider name
11+
ColId primitive.ObjectID `json:"col_id" bson:"col_id"` // data collection id (deprecated) # TODO: remove this field in the future
12+
ColName string `json:"col_name,omitempty" bson:"col_name"` // data collection name
13+
DbName string `json:"db_name,omitempty" bson:"db_name"` // database name
14+
DataSourceId primitive.ObjectID `json:"data_source_id" bson:"data_source_id"` // data source id
15+
DataSource *Database `json:"data_source,omitempty" bson:"-"` // data source
16+
Description string `json:"description" bson:"description"` // description
17+
ProjectId primitive.ObjectID `json:"project_id" bson:"project_id"` // Project.Id
18+
Mode string `json:"mode" bson:"mode"` // default Task.Mode
19+
NodeIds []primitive.ObjectID `json:"node_ids" bson:"node_ids"` // default Task.NodeIds
20+
GitId primitive.ObjectID `json:"git_id" bson:"git_id"` // related Git.Id
21+
GitRootPath string `json:"git_root_path" bson:"git_root_path"`
22+
Git *Git `json:"git,omitempty" bson:"-"`
23+
Template string `json:"template,omitempty" bson:"template,omitempty"` // spider template
24+
TemplateParams *SpiderTemplateParams `json:"template_params,omitempty" bson:"template_params,omitempty"`
2925

3026
// stats
3127
Stat *SpiderStat `json:"stat,omitempty" bson:"-"`
@@ -36,3 +32,10 @@ type Spider struct {
3632
Priority int `json:"priority" bson:"priority"`
3733
AutoInstall bool `json:"auto_install" bson:"auto_install"`
3834
}
35+
36+
type SpiderTemplateParams struct {
37+
ProjectName string `json:"project_name,omitempty" bson:"project_name,omitempty"`
38+
SpiderName string `json:"spider_name,omitempty" bson:"spider_name,omitempty"`
39+
StartUrls string `json:"start_urls,omitempty" bson:"start_urls,omitempty"`
40+
AllowedDomains string `json:"allowed_domains,omitempty" bson:"allowed_domains,omitempty"`
41+
}

core/utils/string.go

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,29 @@ import (
66
"strings"
77
)
88

9+
// replaceChars replaces characters in a string
10+
// Parameters:
11+
// - s: the string to replace characters in
12+
// - o: the characters to replace
13+
// - r: the replacement character
14+
//
15+
// Example:
16+
//
17+
// replaceChars("a-b-c", []string{"-"}, "_") => "a_b_c"
18+
//
19+
// Returns:
20+
// - the string with characters replaced
21+
func replaceChars(s string, o []string, r string) string {
22+
for _, c := range o {
23+
s = strings.ReplaceAll(s, c, r)
24+
}
25+
return s
26+
}
27+
928
func ToSnakeCase(s string) string {
1029
s = strings.TrimSpace(s)
1130
s = strings.ToLower(s)
12-
s = strings.ReplaceAll(s, " ", "_")
13-
s = strings.ReplaceAll(s, "-", "_")
14-
return s
31+
return replaceChars(s, []string{" ", "-", "."}, "_")
1532
}
1633

1734
func ToPascalCase(s string) string {
@@ -25,7 +42,34 @@ func ToPascalCase(s string) string {
2542
func ToKebabCase(s string) string {
2643
s = strings.TrimSpace(s)
2744
s = strings.ToLower(s)
28-
s = strings.ReplaceAll(s, " ", "-")
29-
s = strings.ReplaceAll(s, "_", "-")
45+
return replaceChars(s, []string{" ", "_", "."}, "-")
46+
}
47+
48+
// splitStringWithQuotes splits a string with quotes
49+
// Parameters:
50+
// - s: the string to split
51+
// - q: the quote character
52+
// - d: the delimiter
53+
// - r: the replacement
54+
//
55+
// Example:
56+
//
57+
// splitStringWithQuotes("a,b,c", "'", ",", ", ") => "'a', 'b', 'c'"
58+
//
59+
// Returns:
60+
// - the split string
61+
func splitStringWithQuotes(s, q, d, r string) string {
62+
s = strings.TrimSpace(s)
63+
s = strings.ReplaceAll(s, " ", "")
64+
s = strings.ReplaceAll(s, d, q+r+q)
65+
s = q + s + q
3066
return s
3167
}
68+
69+
func SplitStringWithSingleQuotes(s string) string {
70+
return splitStringWithQuotes(s, "'", ",", ", ")
71+
}
72+
73+
func SplitStringWithDoubleQuotes(s string) string {
74+
return splitStringWithQuotes(s, "\"", ",", "\", \"")
75+
}

0 commit comments

Comments
 (0)