utils

package
v0.0.0-...-6622c04 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 1, 2022 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package utils provides utility functions for Mybot.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CalcBools

func CalcBools(b1, b2, add bool) bool

CalcBools calculates an addition/subtraction result of b1 and b2. This method adds the two if add is true and otherwise subtracts.

Example
package main

import (
	"github.com/iwataka/mybot/backend/utils"

	"fmt"
)

func main() {
	fmt.Println(utils.CalcBools(true, true, true))
	fmt.Println(utils.CalcBools(false, false, true))
	fmt.Println(utils.CalcBools(true, true, false))
	fmt.Println(utils.CalcBools(false, false, false))
}
Output:

true
false
false
false

func CalcStringSlices

func CalcStringSlices(s1, s2 []string, add bool) []string

CalcStringSlices calculates an addition/subtraction result of s1 and s2. If add is true then this method adds the two and otherwise subtracts.

Example
package main

import (
	"github.com/iwataka/mybot/backend/utils"

	"fmt"
)

func main() {
	ss1 := []string{"foo", "bar"}
	ss2 := []string{"foo", "other"}
	fmt.Println(len(utils.CalcStringSlices(ss1, ss2, true)))
	fmt.Println(len(utils.CalcStringSlices(ss1, ss2, false)))
}
Output:

3
1

func CheckStringContained

func CheckStringContained(ss []string, str string) bool

CheckStringContained returns true if ss contains str and otherwise false.

Example
package main

import (
	"github.com/iwataka/mybot/backend/utils"

	"fmt"
)

func main() {
	ss := []string{"foo", "bar"}
	fmt.Println(utils.CheckStringContained(ss, "foo"))
	fmt.Println(utils.CheckStringContained(ss, "other"))
}
Output:

true
false

func Decode

func Decode(ext string, bs []byte, v interface{}) error

func DecodeFile

func DecodeFile(file string, v interface{}) error

DecodeFile decodes file and write the content to v. This method selects a proper decoder by the file extension (json decoder by default).

func Encode

func Encode(ext string, v interface{}) ([]byte, error)

func EncodeFile

func EncodeFile(file string, v interface{}) error

EncodeFile encodes v into file. This method selects a proper encoder by the file extension (json decoder by default).

func FalsePtr

func FalsePtr() *bool

FalsePtr returns a pointer of false value.

func Float64Ptr

func Float64Ptr(f float64) *float64

Float64Ptr returns a pointer of f.

func GenerateRandString

func GenerateRandString(n int) string

GenerateRandString generates a random string consisted of n upper/lower-case alphabets.

func IntPtr

func IntPtr(n int) *int

IntPtr returns a pointer of n.

func TruePtr

func TruePtr() *bool

TruePtr returns a pointer of true value.

func UniqStrSlice

func UniqStrSlice(ss []string) []string

UniqStrSlice returns a slice contains only unique elements of ss. This keeps order of the original slice ss.

func WithStack

func WithStack(err error) error

WithStack return an error with the current stack trace.

Types

type Deletable

type Deletable interface {
	Delete() error
}

type Loadable

type Loadable interface {
	Load() error
}

Loadable provides a feature to load data from an outside storage and write it into this instance.

type Savable

type Savable interface {
	Save() error
}

Savable provides a feature to save the instance data into an outside storage.

type StackTracer

type StackTracer interface {
	StackTrace() errors.StackTrace
}

StackTracer provides a feature to get stack trace.

type TomlUndecodedKeysError

type TomlUndecodedKeysError struct {
	Undecoded []toml.Key
	File      string
}

TomlUndecodedKeysError is an error indicating that there are some undecoded keys in File. In other words, there are some keys which exist in File but not in a destination object.

Example
package main

import (
	"github.com/BurntSushi/toml"
	"github.com/iwataka/mybot/backend/utils"

	"fmt"
)

func main() {
	err := utils.TomlUndecodedKeysError{[]toml.Key{[]string{"foo"}}, "foo.toml"}
	fmt.Println(err.Error())
}
Output:

[foo] undecoded in foo.toml

func (*TomlUndecodedKeysError) Error

func (e *TomlUndecodedKeysError) Error() string

Error returns a message of this error.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL