Skip to content

Commit c395c3e

Browse files
lvan100lianghuan
authored andcommitted
code refactor
1 parent f91ef89 commit c395c3e

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

gs/gs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func OnFunc(fn CondFunc) Condition {
8585
}
8686

8787
// OnProperty creates a Condition based on a property name and options.
88-
func OnProperty(name string) *gs_cond.CondOnProperty {
88+
func OnProperty(name string) gs_cond.OnPropertyInterface {
8989
return gs_cond.OnProperty(name)
9090
}
9191

gs/internal/gs_cond/cond.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,33 +56,40 @@ func (c *onFunc) String() string {
5656

5757
/******************************* OnProperty **********************************/
5858

59-
// CondOnProperty evaluates a condition based on the existence and value of a property
59+
// OnPropertyInterface defines the methods for evaluating a condition based on a property.
60+
type OnPropertyInterface interface {
61+
gs.Condition
62+
MatchIfMissing() OnPropertyInterface
63+
HavingValue(s string) OnPropertyInterface
64+
}
65+
66+
// onProperty evaluates a condition based on the existence and value of a property
6067
// in the context. It allows for complex matching behaviors such as matching missing
6168
// properties or evaluating expressions.
62-
type CondOnProperty struct {
69+
type onProperty struct {
6370
name string // The name of the property to check.
6471
havingValue string // The expected value or expression to match.
6572
matchIfMissing bool // Whether to match if the property is missing.
6673
}
6774

6875
// OnProperty creates a condition based on the presence and value of a specified property.
69-
func OnProperty(name string) *CondOnProperty {
70-
return &CondOnProperty{name: name}
76+
func OnProperty(name string) OnPropertyInterface {
77+
return &onProperty{name: name}
7178
}
7279

7380
// MatchIfMissing sets the condition to match if the property is missing.
74-
func (c *CondOnProperty) MatchIfMissing() *CondOnProperty {
81+
func (c *onProperty) MatchIfMissing() OnPropertyInterface {
7582
c.matchIfMissing = true
7683
return c
7784
}
7885

7986
// HavingValue sets the expected value or expression to match.
80-
func (c *CondOnProperty) HavingValue(s string) *CondOnProperty {
87+
func (c *onProperty) HavingValue(s string) OnPropertyInterface {
8188
c.havingValue = s
8289
return c
8390
}
8491

85-
func (c *CondOnProperty) Matches(ctx gs.CondContext) (bool, error) {
92+
func (c *onProperty) Matches(ctx gs.CondContext) (bool, error) {
8693

8794
// If the context doesn't have the property, handle accordingly.
8895
if !ctx.Has(c.name) {
@@ -125,7 +132,7 @@ func (c *CondOnProperty) Matches(ctx gs.CondContext) (bool, error) {
125132
return ok, nil
126133
}
127134

128-
func (c *CondOnProperty) String() string {
135+
func (c *onProperty) String() string {
129136
var sb strings.Builder
130137
sb.WriteString("OnProperty(name=")
131138
sb.WriteString(c.name)

0 commit comments

Comments
 (0)