Skip to content

Commit

Permalink
unify line endings to unix line endings
Browse files Browse the repository at this point in the history
  • Loading branch information
jogramming committed May 26, 2019
1 parent a2fa41f commit 23cdb2b
Show file tree
Hide file tree
Showing 14 changed files with 1,732 additions and 1,732 deletions.
482 changes: 241 additions & 241 deletions bot/paginatedmessages/pageinatedmessage.go

Large diffs are not rendered by default.

270 changes: 135 additions & 135 deletions common/config/config.go
Original file line number Diff line number Diff line change
@@ -1,135 +1,135 @@
package config

import (
"strconv"
"strings"
)

type ConfigSource interface {
GetValue(key string) interface{}
}

type ConfigOption struct {
Name string
Description string
DefaultValue interface{}
LoadedValue interface{}
Manager *ConfigManager
}

func (opt *ConfigOption) LoadValue() {
newVal := opt.DefaultValue

for i := len(opt.Manager.sources) - 1; i >= 0; i-- {
source := opt.Manager.sources[i]

v := source.GetValue(opt.Name)
if v != nil {
newVal = v
break
}
}

// parse ahead of time
if opt.DefaultValue != nil {
if _, ok := opt.DefaultValue.(int); ok {
newVal = interface{}(intVal(newVal))
} else if _, ok := opt.DefaultValue.(bool); ok {
newVal = interface{}(boolVal(newVal))
}
}

opt.LoadedValue = newVal
}

func (opt *ConfigOption) GetString() string {
return strVal(opt.LoadedValue)
}

func (opt *ConfigOption) GetInt() int {
return intVal(opt.LoadedValue)
}

func (opt *ConfigOption) GetBool() bool {
return boolVal(opt.LoadedValue)
}

type ConfigManager struct {
sources []ConfigSource
options map[string]*ConfigOption
}

func NewConfigManager() *ConfigManager {
return &ConfigManager{
options: make(map[string]*ConfigOption),
}
}

func (c *ConfigManager) AddSource(source ConfigSource) {
c.sources = append(c.sources, source)
}

func (c *ConfigManager) RegisterOption(name, desc string, defaultValue interface{}) *ConfigOption {
opt := &ConfigOption{
Name: name,
Description: desc,
DefaultValue: defaultValue,
Manager: c,
}

c.options[name] = opt
return opt
}

func (c *ConfigManager) Load() {
for _, v := range c.options {
v.LoadValue()
}
}

func strVal(i interface{}) string {
switch t := i.(type) {
case string:
return t
case int:
return strconv.FormatInt(int64(t), 10)
case Stringer:
return t.String()
}

return ""
}

type Stringer interface {
String() string
}

func intVal(i interface{}) int {
switch t := i.(type) {
case string:
n, _ := strconv.ParseInt(t, 10, 64)
return int(n)
case int:
return t
}

return 0
}

func boolVal(i interface{}) bool {
switch t := i.(type) {
case string:
lower := strings.ToLower(strings.TrimSpace(t))
if lower == "true" || lower == "yes" || lower == "on" || lower == "enabled" || lower == "1" {
return true
}

return false
case int:
return t > 0
case bool:
return t
}

return false
}
package config

import (
"strconv"
"strings"
)

type ConfigSource interface {
GetValue(key string) interface{}
}

type ConfigOption struct {
Name string
Description string
DefaultValue interface{}
LoadedValue interface{}
Manager *ConfigManager
}

func (opt *ConfigOption) LoadValue() {
newVal := opt.DefaultValue

for i := len(opt.Manager.sources) - 1; i >= 0; i-- {
source := opt.Manager.sources[i]

v := source.GetValue(opt.Name)
if v != nil {
newVal = v
break
}
}

// parse ahead of time
if opt.DefaultValue != nil {
if _, ok := opt.DefaultValue.(int); ok {
newVal = interface{}(intVal(newVal))
} else if _, ok := opt.DefaultValue.(bool); ok {
newVal = interface{}(boolVal(newVal))
}
}

opt.LoadedValue = newVal
}

func (opt *ConfigOption) GetString() string {
return strVal(opt.LoadedValue)
}

func (opt *ConfigOption) GetInt() int {
return intVal(opt.LoadedValue)
}

func (opt *ConfigOption) GetBool() bool {
return boolVal(opt.LoadedValue)
}

type ConfigManager struct {
sources []ConfigSource
options map[string]*ConfigOption
}

func NewConfigManager() *ConfigManager {
return &ConfigManager{
options: make(map[string]*ConfigOption),
}
}

func (c *ConfigManager) AddSource(source ConfigSource) {
c.sources = append(c.sources, source)
}

func (c *ConfigManager) RegisterOption(name, desc string, defaultValue interface{}) *ConfigOption {
opt := &ConfigOption{
Name: name,
Description: desc,
DefaultValue: defaultValue,
Manager: c,
}

c.options[name] = opt
return opt
}

func (c *ConfigManager) Load() {
for _, v := range c.options {
v.LoadValue()
}
}

func strVal(i interface{}) string {
switch t := i.(type) {
case string:
return t
case int:
return strconv.FormatInt(int64(t), 10)
case Stringer:
return t.String()
}

return ""
}

type Stringer interface {
String() string
}

func intVal(i interface{}) int {
switch t := i.(type) {
case string:
n, _ := strconv.ParseInt(t, 10, 64)
return int(n)
case int:
return t
}

return 0
}

func boolVal(i interface{}) bool {
switch t := i.(type) {
case string:
lower := strings.ToLower(strings.TrimSpace(t))
if lower == "true" || lower == "yes" || lower == "on" || lower == "enabled" || lower == "1" {
return true
}

return false
case int:
return t > 0
case bool:
return t
}

return false
}
36 changes: 18 additions & 18 deletions common/config/envsource.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package config

import (
"os"
"strings"
)

type EnvSource struct{}

func (e *EnvSource) GetValue(key string) interface{} {
properKey := strings.ToUpper(key)
properKey = strings.Replace(properKey, ".", "_", -1)
v := os.Getenv(properKey)
if v == "" {
return nil
}
return v
}
package config

import (
"os"
"strings"
)

type EnvSource struct{}

func (e *EnvSource) GetValue(key string) interface{} {
properKey := strings.ToUpper(key)
properKey = strings.Replace(properKey, ".", "_", -1)
v := os.Getenv(properKey)
if v == "" {
return nil
}
return v
}
30 changes: 15 additions & 15 deletions common/config/singleton.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package config

var singleton = NewConfigManager()

func AddSource(source ConfigSource) {
singleton.AddSource(source)
}

func RegisterOption(name, desc string, defaultValue interface{}) *ConfigOption {
return singleton.RegisterOption(name, desc, defaultValue)
}

func Load() {
singleton.Load()
}
package config

var singleton = NewConfigManager()

func AddSource(source ConfigSource) {
singleton.AddSource(source)
}

func RegisterOption(name, desc string, defaultValue interface{}) *ConfigOption {
return singleton.RegisterOption(name, desc, defaultValue)
}

func Load() {
singleton.Load()
}
Loading

0 comments on commit 23cdb2b

Please sign in to comment.