Repository navigation
Expand file tree
/
Copy pathvalue.go
More file actions
175 lines (152 loc) · 4.46 KB
/
Copy pathvalue.go
File metadata and controls
175 lines (152 loc) · 4.46 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package toml
// Copyright (C) 2024 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
import (
"fmt"
"time"
)
// ValueKind represents the type of a TOML value.
type ValueKind int
const (
// KindString represents a string value.
KindString ValueKind = iota
// KindInteger represents an integer value.
KindInteger
// KindFloat represents a floating-point value.
KindFloat
// KindBoolean represents a boolean value.
KindBoolean
// KindDatetime represents a datetime value.
KindDatetime
// KindArray represents an array value.
KindArray
// KindTable represents an inline table.
KindTable
)
// String returns the string representation of the value kind.
func (vk ValueKind) String() string {
switch vk {
case KindString:
return "string"
case KindInteger:
return "integer"
case KindFloat:
return "float"
case KindBoolean:
return "boolean"
case KindDatetime:
return "datetime"
case KindArray:
return "array"
case KindTable:
return "table"
default:
return "unknown"
}
}
// Value represents a TOML value with its type information.
type Value struct {
kind ValueKind
raw any
}
// NewValue creates a new Value with the given kind and raw data.
func NewValue(kind ValueKind, raw any) *Value {
return &Value{
kind: kind,
raw: raw,
}
}
// NewStringValue creates a new string Value.
func NewStringValue(s string) *Value {
return &Value{kind: KindString, raw: s}
}
// NewIntegerValue creates a new integer Value.
func NewIntegerValue(i int64) *Value {
return &Value{kind: KindInteger, raw: i}
}
// NewFloatValue creates a new float Value.
func NewFloatValue(f float64) *Value {
return &Value{kind: KindFloat, raw: f}
}
// NewBooleanValue creates a new boolean Value.
func NewBooleanValue(b bool) *Value {
return &Value{kind: KindBoolean, raw: b}
}
// NewDatetimeValue creates a new datetime Value.
func NewDatetimeValue(t time.Time) *Value {
return &Value{kind: KindDatetime, raw: t}
}
// NewArrayValue creates a new array Value.
func NewArrayValue(a []any) *Value {
return &Value{kind: KindArray, raw: a}
}
// NewTableValue creates a new table Value (for inline tables).
func NewTableValue(t map[string]*Value) *Value {
return &Value{kind: KindTable, raw: t}
}
// Kind returns the kind of the value.
func (v *Value) Kind() ValueKind {
return v.kind
}
// Raw returns the raw value.
func (v *Value) Raw() any {
return v.raw
}
// String converts the value to a string.
// Returns an error if the value is not a string.
func (v *Value) String() (string, error) {
if v.kind != KindString {
return "", NewError("string conversion", fmt.Errorf("expected string, got %v", v.kind), ErrType)
}
return v.raw.(string), nil
}
// Int converts the value to an int64.
// Returns an error if the value is not an integer.
func (v *Value) Int() (int64, error) {
if v.kind != KindInteger {
return 0, NewError("int conversion", fmt.Errorf("expected integer, got %v", v.kind), ErrType)
}
return v.raw.(int64), nil
}
// Float converts the value to a float64.
// Returns an error if the value is not a float.
func (v *Value) Float() (float64, error) {
if v.kind != KindFloat {
return 0, NewError("float conversion", fmt.Errorf("expected float, got %v", v.kind), ErrType)
}
return v.raw.(float64), nil
}
// Bool converts the value to a boolean.
// Returns an error if the value is not a boolean.
func (v *Value) Bool() (bool, error) {
if v.kind != KindBoolean {
return false, NewError("bool conversion", fmt.Errorf("expected boolean, got %v", v.kind), ErrType)
}
return v.raw.(bool), nil
}
// Time converts the value to a time.Time.
// Returns an error if the value is not a datetime.
func (v *Value) Time() (time.Time, error) {
if v.kind != KindDatetime {
return time.Time{}, NewError("time conversion", fmt.Errorf("expected datetime, got %v", v.kind), ErrType)
}
return v.raw.(time.Time), nil
}
// Array converts the value to a slice.
// Returns an error if the value is not an array.
func (v *Value) Array() ([]any, error) {
if v.kind != KindArray {
return nil, NewError("array conversion", fmt.Errorf("expected array, got %v", v.kind), ErrType)
}
return v.raw.([]any), nil
}
// Table converts the value to a map (for inline tables).
// Returns an error if the value is not a table.
func (v *Value) Table() (map[string]*Value, error) {
if v.kind != KindTable {
return nil, NewError("table conversion", fmt.Errorf("expected table, got %v", v.kind), ErrType)
}
return v.raw.(map[string]*Value), nil
}