-
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.
SetVar(key string, value string) GetVar(key string) string DelVar(key string) HasChangedVars() bool
- Loading branch information
1 parent
3c34561
commit 46e9e35
Showing
5 changed files
with
64 additions
and
0 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
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,54 @@ | ||
package botsfwmodels | ||
|
||
import ( | ||
"github.com/strongo/slice" | ||
"slices" | ||
) | ||
|
||
type chatVars struct { | ||
Vars map[string]string `dalgo:"vars,omitempty" firestore:"vars,omitempty"` | ||
changed []string | ||
deleted []string | ||
} | ||
|
||
// GetVar returns a chat variable | ||
func (v *chatVars) GetVar(key string) string { | ||
if v.Vars == nil { | ||
return "" | ||
} | ||
return v.Vars[key] | ||
} | ||
|
||
// SetVar sets a chat variable | ||
func (v *chatVars) SetVar(key, value string) { | ||
if v.Vars == nil { | ||
v.Vars = make(map[string]string) | ||
} else if v.Vars[key] == value { | ||
return | ||
} | ||
v.Vars[key] = value | ||
slice.RemoveInPlaceByValue(v.deleted, key) | ||
if !slices.Contains(v.changed, key) { | ||
v.changed = append(v.changed, key) | ||
} | ||
} | ||
|
||
// DelVar deletes a chat variable | ||
func (v *chatVars) DelVar(key string) { | ||
if v.Vars == nil { | ||
return | ||
} | ||
if _, ok := v.Vars[key]; !ok { | ||
return | ||
} | ||
delete(v.Vars, key) | ||
v.changed = slice.RemoveInPlaceByValue(v.changed, key) | ||
if !slices.Contains(v.deleted, key) { | ||
v.deleted = append(v.deleted, key) | ||
} | ||
} | ||
|
||
// HasChangedVars returns true if vars have been changed | ||
func (v *chatVars) HasChangedVars() bool { | ||
return len(v.changed) > 0 || len(v.deleted) > 0 | ||
} |
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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
github.com/strongo/slice v0.3.1 h1:VWkyYBgcVJn6Hs7wYhL9Vxwgb7V3zQAUFTBV9wo5lc4= | ||
github.com/strongo/slice v0.3.1/go.mod h1:B5ODKCkl0rp2oiG0UBqkN1cCOrSCU2cUuhqCM1sC8r4= | ||
github.com/strongo/validation v0.0.7 h1:gs6YkwPsYtVsepQaQOB+ZF+T0Gu5+nk4ZMND8F85e+U= | ||
github.com/strongo/validation v0.0.7/go.mod h1:YUwoPEItLJd/Bc9X1OCUm03ofhvm3kwZvuihU7/jz58= |