File: value.go

package info (click to toggle)
zfind 0.4.7-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 320 kB
  • sloc: sh: 145; makefile: 5
file content (65 lines) | stat: -rw-r--r-- 1,284 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package filter

import (
	"errors"
	"fmt"
)

// Value is a type that can represent a number, a string, or a boolean value.
type Value struct {
	Number  *int64
	Text    *string
	Boolean *bool
}

// String returns a string representation of the value. If the value is nil, an empty
// string is returned.
func (v *Value) String() string {
	if v == nil {
		return ""
	}
	switch {
	case v.Number != nil:
		return fmt.Sprintf("%d", *v.Number)
	case v.Text != nil:
		return *v.Text
	case v.Boolean != nil:
		return fmt.Sprintf("%t", *v.Boolean)
	default:
		return ""
	}
}

func (v *Value) tovalue(name string) (*value, error) {
	if v != nil {
		return &value{
			Number:  v.Number,
			Text:    v.Text,
			Boolean: (*boolean)(v.Boolean),
		}, nil
	} else {
		return &value{}, errors.New(fmt.Sprintf("\"%s\" is unknown", name))
	}
}

// NumberValue creates a new Value instance that represents the given number.
func NumberValue(f int64) *Value {
	return &Value{
		Number: &f,
	}
}

// TextValue creates a new Value instance that represents the given string.
func TextValue(s string) *Value {
	return &Value{
		Text: &s,
	}
}

// BoolValue creates a new Value instance that represents the given boolean value.
func BoolValue(v bool) *Value {
	b := bool(v)
	return &Value{
		Boolean: &b,
	}
}