github.com/TIBCOSoftware/flogo-lib@v0.5.9/core/data/path_test.go (about)

     1  package data
     2  
     3  import (
     4  	"fmt"
     5  	mjson "github.com/TIBCOSoftware/flogo-lib/core/mapper/exprmapper/json"
     6  	"github.com/TIBCOSoftware/flogo-lib/core/mapper/exprmapper/json/field"
     7  	"github.com/stretchr/testify/assert"
     8  	"testing"
     9  )
    10  
    11  func TestResolvePathValue(t *testing.T) {
    12  	// Resolution of Old Trigger expression
    13  
    14  	mapVal, _ := CoerceToObject("{\"myParam\":5}")
    15  	path := ".myParam"
    16  	newVal, err := PathGetValue(mapVal, path)
    17  	assert.Nil(t, err)
    18  	assert.Equal(t, 5.0, newVal)
    19  
    20  	// Resolution of Old Trigger expression
    21  	arrVal, _ := CoerceToArray("[1,6,3]")
    22  	path = "[1]"
    23  	newVal, err = PathGetValue(arrVal, path)
    24  	assert.Nil(t, err)
    25  	assert.Equal(t, 6.0, newVal)
    26  
    27  	mapVal, _ = CoerceToObject("{\"myParam\":{\"nestedMap\":1}}")
    28  	path = ".myParam.nestedMap"
    29  	newVal, err = PathGetValue(mapVal, path)
    30  	assert.Nil(t, err)
    31  	assert.Equal(t, 1.0, newVal)
    32  
    33  	mapVal, _ = CoerceToObject("{\"myParam\":{\"nestedMap\":1}}")
    34  	path = `["myParam"].nestedMap`
    35  	newVal, err = PathGetValue(mapVal, path)
    36  	assert.Nil(t, err)
    37  	assert.Equal(t, 1.0, newVal)
    38  
    39  	mapVal, _ = CoerceToObject("{\"myParam\":{\"nestedMap\":1}}")
    40  	path = `.myParam["nestedMap"]`
    41  	newVal, err = PathGetValue(mapVal, path)
    42  	assert.Nil(t, err)
    43  	assert.Equal(t, 1.0, newVal)
    44  
    45  	arrVal, _ = CoerceToArray("[{\"nestedMap1\":1},{\"nestedMap2\":2}]")
    46  	path = "[1].nestedMap2"
    47  	newVal, err = PathGetValue(arrVal, path)
    48  	assert.Nil(t, err)
    49  	assert.Equal(t, 2.0, newVal)
    50  
    51  	mapVal, _ = CoerceToObject("{\"myParam\":{\"nestedArray\":[7,8,9]}}")
    52  	path = ".myParam.nestedArray[1]"
    53  	newVal, err = PathGetValue(mapVal, path)
    54  	assert.Nil(t, err)
    55  	assert.Equal(t, 8.0, newVal)
    56  
    57  	arrVal, _ = CoerceToArray("[{\"nestedMap1\":1},{\"nestedMap2\":{\"nestedArray\":[7,8,9]}}]")
    58  	path = "[1].nestedMap2.nestedArray[2]"
    59  	newVal, err = PathGetValue(arrVal, path)
    60  	assert.Nil(t, err)
    61  	assert.Equal(t, 9.0, newVal)
    62  
    63  	mapVal, _ = CoerceToObject("{\"myParam\":{\"nestedArray\":[7,8,9]}}")
    64  	path = ".myParam.nestedArray"
    65  	newVal, err = PathGetValue(mapVal, path)
    66  	assert.Nil(t, err)
    67  	//todo check if array
    68  
    69  	arrVal, _ = CoerceToArray("[{\"nestedMap1\":1},{\"nestedMap2\":{\"nestedArray\":[7,8,9]}}]")
    70  	path = "[1].nestedMap2"
    71  	newVal, err = PathGetValue(arrVal, path)
    72  	assert.Nil(t, err)
    73  	//todo check if map
    74  
    75  }
    76  
    77  func TestPathSetValue(t *testing.T) {
    78  	// Resolution of Old Trigger expression
    79  
    80  	mapVal, _ := CoerceToObject("{\"myParam\":5}")
    81  	path := ".myParam"
    82  	v, err := PathSetValue(mapVal, path, 6)
    83  	assert.Nil(t, err)
    84  	newVal, err := PathGetValue(mapVal, path)
    85  	assert.Nil(t, err)
    86  	assert.Equal(t, 6, newVal)
    87  
    88  	// Resolution of Old Trigger expression
    89  	arrVal, _ := CoerceToArray("[1,6,3]")
    90  	path = "[1]"
    91  	v, err = PathSetValue(arrVal, path, 4)
    92  	assert.Nil(t, err)
    93  	assert.Equal(t, 4, arrVal[1])
    94  	newVal, err = PathGetValue(arrVal, path)
    95  	assert.Nil(t, err)
    96  	assert.Equal(t, 4, newVal)
    97  	//
    98  	mapVal, _ = CoerceToObject("{\"myParam\":{\"nestedMap\":1}}")
    99  	path = ".myParam.nestedMap"
   100  	assert.Nil(t, err)
   101  	v, err = PathSetValue(mapVal, path, 7)
   102  	newVal, err = PathGetValue(mapVal, path)
   103  	assert.Nil(t, err)
   104  	assert.Equal(t, 7, newVal)
   105  
   106  	mapVal, _ = CoerceToObject("{\"myParam\":{\"nestedMap\":1}}")
   107  	path = `["myParam"].nestedMap`
   108  	assert.Nil(t, err)
   109  	v, err = PathSetValue(mapVal, path, 7)
   110  	newVal, err = PathGetValue(mapVal, path)
   111  	assert.Nil(t, err)
   112  	assert.Equal(t, 7, newVal)
   113  
   114  	mapVal, _ = CoerceToObject("{\"myParam\":{\"nestedMap\":1}}")
   115  	path = `.myParam["nestedMap"]`
   116  	assert.Nil(t, err)
   117  	v, err = PathSetValue(mapVal, path, 7)
   118  	newVal, err = PathGetValue(mapVal, path)
   119  	assert.Nil(t, err)
   120  	assert.Equal(t, 7, newVal)
   121  
   122  	arrVal, _ = CoerceToArray("[{\"nestedMap1\":1},{\"nestedMap2\":2}]")
   123  	path = "[1].nestedMap2"
   124  	v, err = PathSetValue(arrVal, path, 3)
   125  	assert.Nil(t, err)
   126  	newVal, err = PathGetValue(v, path)
   127  	assert.Nil(t, err)
   128  	assert.Equal(t, 3, newVal)
   129  	//
   130  	mapVal, _ = CoerceToObject("{\"myParam\":{\"nestedArray\":[7,8,9]}}")
   131  	path = ".myParam.nestedArray[1]"
   132  	v, err = PathSetValue(mapVal, path, 1)
   133  	assert.Nil(t, err)
   134  	newVal, err = PathGetValue(mapVal, path)
   135  	assert.Nil(t, err)
   136  	assert.Equal(t, 1, newVal)
   137  
   138  	arrVal, _ = CoerceToArray("[{\"nestedMap1\":1},{\"nestedMap2\":{\"nestedArray\":[7,8,9]}}]")
   139  	path = "[1].nestedMap2.nestedArray[2]"
   140  	v, err = PathSetValue(arrVal, path, 5)
   141  	assert.Nil(t, err)
   142  	fmt.Println(fmt.Sprintf("%+v", v))
   143  	newVal, err = PathGetValue(v, path)
   144  	assert.Nil(t, err)
   145  	assert.Equal(t, 5, newVal)
   146  
   147  	newVal, _ = CoerceToObject("{\"myParam\":{\"nestedArray\":[7,8,9]}}")
   148  	path = ".myParam.nestedArray"
   149  	v, err = PathSetValue(newVal, path, 3)
   150  	assert.Nil(t, err)
   151  	newVal, err = PathGetValue(v, path)
   152  	assert.Nil(t, err)
   153  
   154  	//todo check if array
   155  	//arrVal,_ = CoerceToArray("[{\"nestedMap1\":1},{\"nestedMap2\":{\"nestedArray\":[7,8,9]}}]")
   156  	//path = "[1].nestedMap2"
   157  	//assert.Nil(t, err)
   158  	//err = PathSetValue(arrVal, path, 3)
   159  	//newVal,err = PathGetValue(arrVal, path)
   160  	//assert.Nil(t, err)
   161  	//////todo check if map
   162  }
   163  
   164  func PathSetValue(value interface{}, path string, attrValue interface{}) (interface{}, error) {
   165  	mapField, err := field.ParseMappingField(path)
   166  	if err != nil {
   167  		return nil, err
   168  	}
   169  
   170  	return mjson.SetFieldValue(attrValue, value, mapField)
   171  }