Skip to content

Commit

Permalink
No rush to change the interface of Value.Export()
Browse files Browse the repository at this point in the history
  • Loading branch information
robertkrimen committed May 16, 2013
1 parent faaed09 commit adf21d0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
6 changes: 5 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -394,11 +394,15 @@ The return value will (generally) be one of:
#### func (Value) Export

```go
func (self Value) Export() interface{}
func (self Value) Export() (interface{}, error)
```
Export will attempt to convert the value to a Go representation and return it
via an interface{} kind.

WARNING: The interface function will be changing soon to:

Export() interface{}

If a reasonable conversion is not possible, then the original result is
returned.

Expand Down
14 changes: 11 additions & 3 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,10 @@ func sameValue(x Value, y Value) bool {
// Export will attempt to convert the value to a Go representation
// and return it via an interface{} kind.
//
// WARNING: The interface function will be changing soon to:
//
// Export() interface{}
//
// If a reasonable conversion is not possible, then the original
// result is returned.
//
Expand All @@ -573,7 +577,11 @@ func sameValue(x Value, y Value) bool {
// Array -> []interface{}
// Object -> map[string]interface{}
//
func (self Value) Export() interface{} {
func (self Value) Export() (interface{}, error) {
return self.export(), nil
}

func (self Value) export() interface{} {

switch self._valueType {
case valueUndefined:
Expand Down Expand Up @@ -610,13 +618,13 @@ func (self Value) Export() interface{} {
if !object.hasProperty(name) {
continue
}
result = append(result, object.get(name).Export())
result = append(result, object.get(name).export())
}
return result
} else {
result := make(map[string]interface{})
object.enumerate(func(name string) {
result[name] = object.get(name).Export()
result[name] = object.get(name).export()
})
return result
}
Expand Down
26 changes: 13 additions & 13 deletions value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,26 +158,26 @@ func Test_sameValue(t *testing.T) {
IsFalse(sameValue(NaNValue(), toValue("Nothing happens.")))
}

func TestExport(t *testing.T) {
func Testexport(t *testing.T) {
Terst(t)

test := runTest()

Is(test(`null;`).Export(), nil)
Is(test(`undefined;`).Export(), UndefinedValue())
Is(test(`true;`).Export(), true)
Is(test(`false;`).Export(), false)
Is(test(`0;`).Export(), 0)
Is(test(`3.1459`).Export(), 3.1459)
Is(test(`"Nothing happens";`).Export(), "Nothing happens")
Is(test(`String.fromCharCode(97,98,99,100,101,102)`).Export(), "abcdef")
Is(test(`null;`).export(), nil)
Is(test(`undefined;`).export(), UndefinedValue())
Is(test(`true;`).export(), true)
Is(test(`false;`).export(), false)
Is(test(`0;`).export(), 0)
Is(test(`3.1459`).export(), 3.1459)
Is(test(`"Nothing happens";`).export(), "Nothing happens")
Is(test(`String.fromCharCode(97,98,99,100,101,102)`).export(), "abcdef")
{
value := test(`({ abc: 1, def: true });`).Export().(map[string]interface{})
value := test(`({ abc: 1, def: true });`).export().(map[string]interface{})
Is(value["abc"], 1)
Is(value["def"], true)
}
{
value := test(`[ "abc", 1, "def", true, undefined, null ];`).Export().([]interface{})
value := test(`[ "abc", 1, "def", true, undefined, null ];`).export().([]interface{})
Is(value[0], "abc")
Is(value[1], 1)
Is(value[2], "def")
Expand Down Expand Up @@ -210,7 +210,7 @@ func TestExport(t *testing.T) {
input, err := json.Marshal(value)
Is(err, nil)

output, err := json.Marshal(test("(" + string(input) + ");").Export())
output, err := json.Marshal(test("(" + string(input) + ");").export())
Is(err, nil)

Is(string(input), string(output))
Expand All @@ -225,6 +225,6 @@ func TestExport(t *testing.T) {
abc.def = 3
abc.xyz = 3.1459
failSet("abc", abc)
Is(test(`abc;`).Export(), abc)
Is(test(`abc;`).export(), abc)
}
}

0 comments on commit adf21d0

Please sign in to comment.