Skip to content

Commit

Permalink
feat: add a replacewith tag in struct
Browse files Browse the repository at this point in the history
  • Loading branch information
tauslim committed Mar 1, 2024
1 parent 8cce285 commit 843d0a5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ type Parser struct {
type field struct {
// Name of the column.
Name string
// Has a "replacewith" option in the tag. If present, the name of the column will be converted to this name
ReplaceWith string
// Has a "sort" option in the tag.
Sortable bool
// Has a "filter" option in the tag.
Expand Down Expand Up @@ -229,6 +231,8 @@ func (p *Parser) parseField(sf reflect.StructField) error {
f.Filterable = true
case strings.HasPrefix(opt, "column"):
f.Name = strings.TrimPrefix(opt, "column=")
case strings.HasPrefix(opt, "replacewith"):
f.ReplaceWith = strings.TrimPrefix(opt, "replacewith=")
case strings.HasPrefix(opt, "layout"):
layout = strings.TrimPrefix(opt, "layout=")
// if it's one of the standard layouts, like: RFC822 or Kitchen.
Expand Down
15 changes: 15 additions & 0 deletions validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,13 @@ func (p *Parser) GetFieldValidationFunc() ValidationFunc {
if !ok || !f.Filterable {
return fmt.Errorf("field name (arg: %s) is not filterable", v)
}
if !IsValidField(v) {
return fmt.Errorf("field name (arg: %s) is not valid", v)
}
field = f
if field.ReplaceWith != "" {
n.Args[i] = field.ReplaceWith
}
} else {
if field == nil {
return fmt.Errorf("no field is found for node value %s", v)
Expand Down Expand Up @@ -178,3 +184,12 @@ func (p *Parser) validateLimit(l string) error {
}
return nil
}

func IsValidField(s string) bool {
for _, ch := range s {
if !IsLetter(ch) && !IsDigit(ch) && ch != '_' && ch != '-' && ch != '.' {
return false
}
}
return true
}

0 comments on commit 843d0a5

Please sign in to comment.