json/token: remove Value
This commit is contained in:
parent
b3a9867999
commit
e9e082dff4
@ -4,7 +4,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
hclstrconv "github.com/hashicorp/hcl/hcl/strconv"
|
|
||||||
hcltoken "github.com/hashicorp/hcl/hcl/token"
|
hcltoken "github.com/hashicorp/hcl/hcl/token"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -98,49 +97,6 @@ func (t Token) String() string {
|
|||||||
return fmt.Sprintf("%s %s %s", t.Pos.String(), t.Type.String(), t.Text)
|
return fmt.Sprintf("%s %s %s", t.Pos.String(), t.Type.String(), t.Text)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Value returns the properly typed value for this token. The type of
|
|
||||||
// the returned interface{} is guaranteed based on the Type field.
|
|
||||||
//
|
|
||||||
// This can only be called for literal types. If it is called for any other
|
|
||||||
// type, this will panic.
|
|
||||||
func (t Token) Value() interface{} {
|
|
||||||
switch t.Type {
|
|
||||||
case BOOL:
|
|
||||||
if t.Text == "true" {
|
|
||||||
return true
|
|
||||||
} else if t.Text == "false" {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
panic("unknown bool value: " + t.Text)
|
|
||||||
case FLOAT:
|
|
||||||
v, err := strconv.ParseFloat(t.Text, 64)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return float64(v)
|
|
||||||
case NULL:
|
|
||||||
return nil
|
|
||||||
case NUMBER:
|
|
||||||
v, err := strconv.ParseInt(t.Text, 0, 64)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return int64(v)
|
|
||||||
case STRING:
|
|
||||||
v, err := hclstrconv.Unquote(t.Text)
|
|
||||||
if err != nil {
|
|
||||||
panic(fmt.Sprintf("unquote %s err: %s", t.Text, err))
|
|
||||||
}
|
|
||||||
|
|
||||||
return v
|
|
||||||
default:
|
|
||||||
panic(fmt.Sprintf("unimplemented Value for type: %s", t.Type))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// HCLToken converts this token to an HCL token.
|
// HCLToken converts this token to an HCL token.
|
||||||
//
|
//
|
||||||
// The token type must be a literal type or this will panic.
|
// The token type must be a literal type or this will panic.
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package token
|
package token
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"reflect"
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -33,25 +32,3 @@ func TestTypeString(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTokenValue(t *testing.T) {
|
|
||||||
var tokens = []struct {
|
|
||||||
tt Token
|
|
||||||
v interface{}
|
|
||||||
}{
|
|
||||||
{Token{Type: BOOL, Text: `true`}, true},
|
|
||||||
{Token{Type: BOOL, Text: `false`}, false},
|
|
||||||
{Token{Type: FLOAT, Text: `3.14`}, float64(3.14)},
|
|
||||||
{Token{Type: NULL, Text: `null`}, nil},
|
|
||||||
{Token{Type: NUMBER, Text: `42`}, int64(42)},
|
|
||||||
{Token{Type: STRING, Text: `"foo"`}, "foo"},
|
|
||||||
{Token{Type: STRING, Text: `"foo\nbar"`}, "foo\nbar"},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, token := range tokens {
|
|
||||||
if val := token.tt.Value(); !reflect.DeepEqual(val, token.v) {
|
|
||||||
t.Errorf("want: %v got:%v\n", token.v, val)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user