File: value.go

package info (click to toggle)
golang-github-aws-aws-sdk-go-v2 1.24.1-2~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 554,032 kB
  • sloc: java: 15,941; makefile: 419; sh: 175
file content (93 lines) | stat: -rw-r--r-- 1,765 bytes parent folder | download | duplicates (7)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package ini

import (
	"fmt"
	"strconv"
	"strings"
)

// ValueType is an enum that will signify what type
// the Value is
type ValueType int

func (v ValueType) String() string {
	switch v {
	case NoneType:
		return "NONE"
	case StringType:
		return "STRING"
	}

	return ""
}

// ValueType enums
const (
	NoneType = ValueType(iota)
	StringType
	QuotedStringType
)

// Value is a union container
type Value struct {
	Type ValueType

	str string
	mp  map[string]string
}

// NewStringValue returns a Value type generated using a string input.
func NewStringValue(str string) (Value, error) {
	return Value{str: str}, nil
}

func (v Value) String() string {
	switch v.Type {
	case StringType:
		return fmt.Sprintf("string: %s", string(v.str))
	case QuotedStringType:
		return fmt.Sprintf("quoted string: %s", string(v.str))
	default:
		return "union not set"
	}
}

// MapValue returns a map value for sub properties
func (v Value) MapValue() map[string]string {
	return v.mp
}

// IntValue returns an integer value
func (v Value) IntValue() (int64, bool) {
	i, err := strconv.ParseInt(string(v.str), 0, 64)
	if err != nil {
		return 0, false
	}
	return i, true
}

// FloatValue returns a float value
func (v Value) FloatValue() (float64, bool) {
	f, err := strconv.ParseFloat(string(v.str), 64)
	if err != nil {
		return 0, false
	}
	return f, true
}

// BoolValue returns a bool value
func (v Value) BoolValue() (bool, bool) {
	// we don't use ParseBool as it recognizes more than what we've
	// historically supported
	if strings.EqualFold(v.str, "true") {
		return true, true
	} else if strings.EqualFold(v.str, "false") {
		return false, true
	}
	return false, false
}

// StringValue returns the string value
func (v Value) StringValue() string {
	return v.str
}