1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-16 11:37:36 +02:00
ferret/pkg/runtime/values/boolean.go

116 lines
1.5 KiB
Go
Raw Normal View History

2018-09-18 22:42:38 +02:00
package values
import (
"crypto/sha512"
"encoding/json"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/pkg/errors"
"strings"
)
type Boolean bool
var False = Boolean(false)
var True = Boolean(true)
func NewBoolean(input bool) Boolean {
return Boolean(input)
}
func ParseBoolean(input interface{}) (Boolean, error) {
b, ok := input.(bool)
if ok {
if b {
return True, nil
}
return False, nil
}
s, ok := input.(string)
if ok {
s := strings.ToLower(s)
if s == "true" {
return True, nil
}
if s == "false" {
return False, nil
}
}
return False, errors.Wrap(core.ErrInvalidType, "expected 'bool'")
}
func ParseBooleanP(input interface{}) Boolean {
res, err := ParseBoolean(input)
if err != nil {
panic(err)
}
return res
}
func (t Boolean) MarshalJSON() ([]byte, error) {
return json.Marshal(bool(t))
}
func (t Boolean) Type() core.Type {
return core.BooleanType
}
func (t Boolean) String() string {
if t {
return "true"
}
return "false"
}
func (t Boolean) Compare(other core.Value) int {
raw := bool(t)
switch other.Type() {
case core.BooleanType:
i := other.Unwrap().(bool)
if raw == i {
return 0
}
if raw == false && i == true {
return -1
}
return +1
case core.NoneType:
return 1
default:
return -1
}
}
func (t Boolean) Unwrap() interface{} {
return bool(t)
}
func (t Boolean) Hash() int {
h := sha512.New()
out, err := h.Write([]byte(t.String()))
if err != nil {
return 0
}
return out
}
2018-09-27 17:53:26 +02:00
func (t Boolean) Clone() core.Value {
return t
}