summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/go-telegram-bot-api/telegram-bot-api/v5/params.go
diff options
context:
space:
mode:
authorWim <wim@42.be>2021-12-12 00:35:32 +0100
committerGitHub <noreply@github.com>2021-12-12 00:35:32 +0100
commit02e3d7852bf37cc2dd65aa9d37fc3501e17203f8 (patch)
treefb15a500adadafa78dc90449af30109d1b77f925 /vendor/github.com/go-telegram-bot-api/telegram-bot-api/v5/params.go
parent3893a035be347a7687a41d2054dd1b274d3a0504 (diff)
downloadmatterbridge-msglm-02e3d7852bf37cc2dd65aa9d37fc3501e17203f8.tar.gz
matterbridge-msglm-02e3d7852bf37cc2dd65aa9d37fc3501e17203f8.tar.bz2
matterbridge-msglm-02e3d7852bf37cc2dd65aa9d37fc3501e17203f8.zip
Update telegram-bot-api to v5 (#1660)
Diffstat (limited to 'vendor/github.com/go-telegram-bot-api/telegram-bot-api/v5/params.go')
-rw-r--r--vendor/github.com/go-telegram-bot-api/telegram-bot-api/v5/params.go97
1 files changed, 97 insertions, 0 deletions
diff --git a/vendor/github.com/go-telegram-bot-api/telegram-bot-api/v5/params.go b/vendor/github.com/go-telegram-bot-api/telegram-bot-api/v5/params.go
new file mode 100644
index 00000000..134f85e4
--- /dev/null
+++ b/vendor/github.com/go-telegram-bot-api/telegram-bot-api/v5/params.go
@@ -0,0 +1,97 @@
+package tgbotapi
+
+import (
+ "encoding/json"
+ "reflect"
+ "strconv"
+)
+
+// Params represents a set of parameters that gets passed to a request.
+type Params map[string]string
+
+// AddNonEmpty adds a value if it not an empty string.
+func (p Params) AddNonEmpty(key, value string) {
+ if value != "" {
+ p[key] = value
+ }
+}
+
+// AddNonZero adds a value if it is not zero.
+func (p Params) AddNonZero(key string, value int) {
+ if value != 0 {
+ p[key] = strconv.Itoa(value)
+ }
+}
+
+// AddNonZero64 is the same as AddNonZero except uses an int64.
+func (p Params) AddNonZero64(key string, value int64) {
+ if value != 0 {
+ p[key] = strconv.FormatInt(value, 10)
+ }
+}
+
+// AddBool adds a value of a bool if it is true.
+func (p Params) AddBool(key string, value bool) {
+ if value {
+ p[key] = strconv.FormatBool(value)
+ }
+}
+
+// AddNonZeroFloat adds a floating point value that is not zero.
+func (p Params) AddNonZeroFloat(key string, value float64) {
+ if value != 0 {
+ p[key] = strconv.FormatFloat(value, 'f', 6, 64)
+ }
+}
+
+// AddInterface adds an interface if it is not nil and can be JSON marshalled.
+func (p Params) AddInterface(key string, value interface{}) error {
+ if value == nil || (reflect.ValueOf(value).Kind() == reflect.Ptr && reflect.ValueOf(value).IsNil()) {
+ return nil
+ }
+
+ b, err := json.Marshal(value)
+ if err != nil {
+ return err
+ }
+
+ p[key] = string(b)
+
+ return nil
+}
+
+// AddFirstValid attempts to add the first item that is not a default value.
+//
+// For example, AddFirstValid(0, "", "test") would add "test".
+func (p Params) AddFirstValid(key string, args ...interface{}) error {
+ for _, arg := range args {
+ switch v := arg.(type) {
+ case int:
+ if v != 0 {
+ p[key] = strconv.Itoa(v)
+ return nil
+ }
+ case int64:
+ if v != 0 {
+ p[key] = strconv.FormatInt(v, 10)
+ return nil
+ }
+ case string:
+ if v != "" {
+ p[key] = v
+ return nil
+ }
+ case nil:
+ default:
+ b, err := json.Marshal(arg)
+ if err != nil {
+ return err
+ }
+
+ p[key] = string(b)
+ return nil
+ }
+ }
+
+ return nil
+}