Пример #1
0
func TestPublicData_WithArray(t *testing.T) {

	o := new(test.TestObjectWithFacade)
	o1 := new(test.TestObjectWithFacade)
	o2 := new(test.TestObjectWithFacade)

	arr := []interface{}{o, o1, o2}

	o.Mock.On("PublicData", map[string]interface{}{}).Return(objx.New(map[string]interface{}{"theName": "1"}), nil)
	o1.Mock.On("PublicData", map[string]interface{}{}).Return(objx.New(map[string]interface{}{"theName": "2"}), nil)
	o2.Mock.On("PublicData", map[string]interface{}{}).Return(objx.New(map[string]interface{}{"theName": "3"}), nil)

	public, err := PublicData(arr, map[string]interface{}{})

	if assert.Nil(t, err) {
		assert.Equal(t, reflect.Slice, reflect.TypeOf(public).Kind(), "Result should be array not %v", reflect.TypeOf(public))
	}

	mock.AssertExpectationsForObjects(t, o.Mock, o1.Mock, o2.Mock)

	publicArray := public.([]interface{})
	if assert.Equal(t, 3, len(publicArray)) {
		assert.Equal(t, publicArray[0].(objx.Map).Get("theName").Str(), "1", "o")
		assert.Equal(t, publicArray[1].(objx.Map).Get("theName").Str(), "2", "o1")
		assert.Equal(t, publicArray[2].(objx.Map).Get("theName").Str(), "3", "o2")
	}

}
Пример #2
0
// PublicDataMap calls PublicData and returns the result after type asserting to objx.Map
func PublicDataMap(object interface{}, options map[string]interface{}) (objx.Map, error) {

	data, err := publicData(object, 0, options)

	if err != nil {
		return nil, err
	}

	if data == nil {
		return nil, nil
	}

	switch data.(type) {
	case map[string]interface{}:
		return objx.New(data.(map[string]interface{})), nil
	case objx.Map:
		return data.(objx.Map), nil
	default:
		if dataMap, ok := data.(objx.Map); ok {
			return dataMap, nil
		} else {
			panic(fmt.Sprintf("codecs: PublicDataMap must refer to a map[string]interface{} or objx.Map, not %s.  Did you mean to implement the Facade interface?", reflect.TypeOf(data)))
		}
	}
}
Пример #3
0
func TestPublicDataMap(t *testing.T) {

	o := new(test.TestObjectWithFacade)
	o.Mock.On("PublicData", map[string]interface{}{}).Return(objx.New(map[string]interface{}{"theName": "Mat"}), nil)

	public, err := PublicDataMap(o, map[string]interface{}{})

	if assert.Nil(t, err) {
		assert.Equal(t, public.Get("theName").Str(), "Mat")
	}

	mock.AssertExpectationsForObjects(t, o.Mock)

}
Пример #4
0
// Marshal converts an object to a []byte representation.
// You can optionally pass additional arguments to further customize this call.
func (c *SimpleXmlCodec) Marshal(object interface{}, options map[string]interface{}) ([]byte, error) {

	var output []string

	// add the declaration
	output = append(output, XMLDeclaration)

	// add the rest of the XML
	bytes, err := marshal(object, true, 0, objx.New(options))

	if err != nil {
		return nil, err
	}

	output = append(output, string(bytes))

	// return the output
	return []byte(strings.Join(output, "")), nil
}
Пример #5
0
func TestPublicData_WithRecursion_WithObjects(t *testing.T) {

	o := new(test.TestObjectWithFacade)
	o1 := new(test.TestObjectWithFacade)
	o2 := new(test.TestObjectWithFacade)

	args := map[string]interface{}{constants.OptionKeyClientCallback: "~d"}

	o.Mock.On("PublicData", args).Return(o1, nil)
	o1.Mock.On("PublicData", args).Return(o2, nil)
	o2.Mock.On("PublicData", args).Return(objx.New(map[string]interface{}{"theName": "Mat"}), nil)

	public, err := PublicData(o, args)

	if assert.Nil(t, err) {
		assert.Equal(t, public.(objx.Map).Get("theName").Str(), "Mat")
	}

	mock.AssertExpectationsForObjects(t, o.Mock, o1.Mock, o2.Mock)

}