This repository has been archived by the owner on Oct 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
633 additions
and
418 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/things-go/protogen-saber/internal/annotation" | ||
"github.com/things-go/protogen-saber/internal/protoutil" | ||
"google.golang.org/protobuf/compiler/protogen" | ||
) | ||
|
||
// annotation const value | ||
const ( | ||
Identifier = "asynq" | ||
Attribute_Name_Pattern = "pattern" | ||
Attribute_Name_CronSpec = "cron_spec" | ||
) | ||
|
||
type Task struct { | ||
Enabled bool | ||
Pattern string | ||
CronSpec string | ||
} | ||
|
||
func IsAnnotationTaskEnabled(s protogen.Comments) bool { | ||
annotates, _ := protoutil.NewCommentLines(s).FindAnnotations(Identifier) | ||
return annotates.ContainHeadless(Identifier) | ||
} | ||
|
||
func ParserAnnotationTask(s protogen.Comments) *Task { | ||
ret := &Task{} | ||
annotates, _ := protoutil.NewCommentLines(s).FindAnnotations(Identifier) | ||
for _, annotate := range annotates { | ||
if annotate.IsHeadless() { | ||
ret.Enabled = true | ||
continue | ||
} | ||
for _, attr := range annotate.Attrs { | ||
switch attr.Name { | ||
case Attribute_Name_Pattern: | ||
if vv, ok := attr.Value.(annotation.String); ok { | ||
ret.Pattern = vv.Value | ||
} | ||
case Attribute_Name_CronSpec: | ||
if vv, ok := attr.Value.(annotation.String); ok { | ||
ret.CronSpec = vv.Value | ||
} | ||
} | ||
} | ||
} | ||
return ret | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package annotation | ||
|
||
import ( | ||
"github.com/alecthomas/participle/v2" | ||
) | ||
|
||
var parser = participle.MustBuild[Annotation]( | ||
participle.Unquote(), | ||
participle.Union[Value]( | ||
String{}, Integer{}, Float{}, Bool{}, | ||
StringList{}, IntegerList{}, FloatList{}, BoolList{}, | ||
), | ||
) | ||
|
||
// Annotation an specified identifier and it's attribute list. | ||
// `#[ident]` | ||
// `#[ident(k1=v1,k2=v2)]` | ||
type Annotation struct { | ||
Identifier string `parser:"'#' '[' @Ident"` | ||
Attrs []*NameValue `parser:"('(' (@@ (',' @@)*)? ')')?"` | ||
Empty struct{} `parser:"']'"` | ||
} | ||
|
||
// `#[ident]` only, not contain any attributes. | ||
func (a *Annotation) IsHeadless() bool { | ||
return len(a.Attrs) == 0 | ||
} | ||
|
||
// NameValue like `#[ident(name=value]` | ||
type NameValue struct { | ||
// name | ||
Name string `parser:"@Ident '='"` | ||
// one of follow | ||
// String, Integer, Float, Bool, | ||
// StringList, IntegerList, FloatList, BoolList, | ||
Value Value `parser:"@@"` | ||
} | ||
|
||
// Match 匹配注解 | ||
// `#[ident]` | ||
// `#[ident(k1=v1,k2=v2)]` | ||
func Match(s string) (*Annotation, error) { | ||
return parser.ParseString("", s) | ||
} | ||
|
||
type Annotations []*Annotation | ||
|
||
// ContainHeadless contain headless | ||
func (a Annotations) ContainHeadless(identifier string) bool { | ||
for _, v := range a { | ||
if v.Identifier == identifier && v.IsHeadless() { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func (a Annotations) Find(identifier string) Annotations { | ||
ret := make(Annotations, 0, len(a)) | ||
for _, v := range a { | ||
if v.Identifier == identifier { | ||
ret = append(ret, v) | ||
} | ||
} | ||
return ret | ||
} | ||
|
||
func (a Annotations) FindValue(identifier, name string) []Value { | ||
ret := make([]Value, 0, len(a)) | ||
for _, v := range a { | ||
if v.Identifier == identifier { | ||
for _, vv := range v.Attrs { | ||
if vv.Name == name { | ||
ret = append(ret, vv.Value) | ||
} | ||
} | ||
} | ||
} | ||
return ret | ||
} |
Oops, something went wrong.