reflect

package
Version: v1.1.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 12, 2023 License: MIT Imports: 8 Imported by: 5

Documentation

Index

Constants

This section is empty.

Variables

View Source
var MethodReflector = &TMethodReflector{}
View Source
var ObjectReader = &TObjectReader{}
View Source
var ObjectWriter = &TObjectWriter{}
View Source
var PropertyReflector = &TPropertyReflector{}
View Source
var RecursiveObjectReader = &TRecursiveObjectReader{}
View Source
var RecursiveObjectWriter = &TRecursiveObjectWriter{}
View Source
var TypeMatcher = &TTypeMatcher{}

Functions

This section is empty.

Types

type IValueWrapper

type IValueWrapper interface {
	InnerValue() interface{}
}

type TMethodReflector

type TMethodReflector struct{}

Helper class to perform method introspection and dynamic invocation.

This class has symmetric implementation across all languages supported by Pip.Services toolkit and used to support dynamic data processing.

Because all languages have different casing and case sensitivity rules, this MethodReflector treats all method names as case insensitive.

Example:

myObj = MyObject();

methods = MethodReflector.GetMethodNames();
MethodReflector.HasMethod(myObj, "myMethod");
MethodReflector.InvokeMethod(myObj, "myMethod", 123);

func (*TMethodReflector) GetMethodNames

func (c *TMethodReflector) GetMethodNames(obj interface{}) []string

Gets names of all methods implemented in specified object. Parameters:

  • obj interface{} an objec to introspect.

Returns []string a list with method names.

func (*TMethodReflector) HasMethod

func (c *TMethodReflector) HasMethod(obj interface{}, name string) bool

Checks if object has a method with specified name.. Parameters:

  • obj interface{} an object to introspect.
  • name string a name of the method to check.

Returns bool true if the object has the method and false if it doesn't.

func (*TMethodReflector) InvokeMethod

func (c *TMethodReflector) InvokeMethod(obj interface{}, name string, args ...interface{}) interface{}

Invokes an object method by its name with specified parameters. Parameters:

  • obj interface{} an object to invoke.
  • name string a name of the method to invoke.
  • args ...interface{} a list of method arguments.

Returns interface{} the result of the method invocation or null if method returns void.

type TObjectReader

type TObjectReader struct{}

Helper class to perform property introspection and dynamic reading.

In contrast to PropertyReflector which only introspects regular objects, this ObjectReader is also able to handle maps and arrays. For maps properties are key-pairs identified by string keys, For arrays properties are elements identified by integer index.

This class has symmetric implementation across all languages supported by Pip.Services toolkit and used to support dynamic data processing.

Because all languages have different casing and case sensitivity rules, this ObjectReader treats all property names as case insensitive.

see PropertyReflector

Example:

myObj := MyObject{}

properties := ObjectReader.GetPropertyNames()
ObjectReader.HasProperty(myObj, "myProperty")
value := PropertyReflector.GetProperty(myObj, "myProperty")

myMap := { key1: 123, key2: "ABC" }
ObjectReader.HasProperty(myMap, "key1")
value := ObjectReader.GetProperty(myMap, "key1")

myArray := [1, 2, 3]
ObjectReader.HasProperty(myArrat, "0")
value := ObjectReader.GetProperty(myArray, "0")

func (*TObjectReader) GetProperties

func (c *TObjectReader) GetProperties(obj interface{}) map[string]interface{}

Get values of all properties in specified object and returns them as a map. The object can be a user defined object, map or array. Returned properties correspondently are object properties, map key-pairs or array elements with their indexes. Parameters:

  • obj interface{} an object to get properties from.

Returns map[string]interface{} a map, containing the names of the object's properties and their values.

func (*TObjectReader) GetProperty

func (c *TObjectReader) GetProperty(obj interface{}, name string) interface{}

Gets value of object property specified by its name. The object can be a user defined object, map or array. The property name correspondently must be object property, map key or array index. Parameters:

  • obj interface an object to read property from.
  • name string a name of the property to get.

Returns interface{} the property value or null if property doesn't exist or introspection failed.

func (*TObjectReader) GetPropertyNames

func (c *TObjectReader) GetPropertyNames(obj interface{}) []string

Gets names of all properties implemented in specified object. The object can be a user defined object, map or array. Returned property name correspondently are object properties, map keys or array indexes. Parameters:

  • obj interface{} an objec to introspect.

Returns []string a list with property names.

func (*TObjectReader) GetStrIndexFormat added in v1.1.2

func (c *TObjectReader) GetStrIndexFormat(len int) string

func (*TObjectReader) GetValue

func (c *TObjectReader) GetValue(obj interface{}) interface{}

Gets a real object value. If object is a wrapper, it unwraps the value behind it. Otherwise it returns the same object value. Parameters:

  • obj interface{} an object to unwrap..

Returns interface{} an actual (unwrapped) object value.

func (*TObjectReader) HasProperty

func (c *TObjectReader) HasProperty(obj interface{}, name string) bool

Checks if object has a property with specified name. The object can be a user defined object, map or array. The property name correspondently must be object property, map key or array index. Parameters:

  • obj interface{} an object to introspect.
  • name string a name of the property to check.

Returns bool true if the object has the property and false if it doesn't.

type TObjectWriter

type TObjectWriter struct{}

Helper class to perform property introspection and dynamic writing.

In contrast to PropertyReflector which only introspects regular objects, this ObjectWriter is also able to handle maps and arrays. For maps properties are key-pairs identified by string keys, For arrays properties are elements identified by integer index.

This class has symmetric implementation across all languages supported by Pip.Services toolkit and used to support dynamic data processing.

Because all languages have different casing and case sensitivity rules, this ObjectWriter treats all property names as case insensitive.

see PropertyReflector

Example:

myObj := MyObject{}

ObjectWriter.SetProperty(myObj, "myProperty", 123)

myMap := { key1: 123, key2: "ABC" }
ObjectWriter.SetProperty(myMap, "key1", "XYZ")

myArray := [1, 2, 3]
ObjectWriter.SetProperty(myArray, "0", 123)

func (*TObjectWriter) GetValue

func (c *TObjectWriter) GetValue(obj interface{}) interface{}

Gets a real object value. If object is a wrapper, it unwraps the value behind it. Otherwise it returns the same object value. Parameters:

  • obj interface{} an object to unwrap..

Returns interface{} an actual (unwrapped) object value.

func (*TObjectWriter) SetProperties

func (c *TObjectWriter) SetProperties(obj interface{}, values map[string]interface{})

Sets values of some (all) object properties. The object can be a user defined object, map or array. Property values correspondently are object properties, map key-pairs or array elements with their indexes. If some properties do not exist or introspection fails they are just silently skipped and no errors thrown. see setProperty Parameters:

  • obj interface{} an object to write properties to.
  • values map[string]interface{} a map, containing property names and their values.

func (*TObjectWriter) SetProperty

func (c *TObjectWriter) SetProperty(obj interface{}, name string, value interface{})

Sets value of object property specified by its name. The object can be a user defined object, map or array. The property name correspondently must be object property, map key or array index. If the property does not exist or introspection fails this method doesn't do anything and doesn't any throw errors. Parameters:

  • obj interface{} an object to write property to.
  • name string a name of the property to set.
  • value interface{} a new value for the property to set.

type TPropertyReflector

type TPropertyReflector struct{}

Helper class to perform property introspection and dynamic reading and writing.

This class has symmetric implementation across all languages supported by Pip.Services toolkit and used to support dynamic data processing.

Because all languages have different casing and case sensitivity rules, this PropertyReflector treats all property names as case insensitive.

Example:

myObj := MyObject{}

properties := PropertyReflector.GetPropertyNames()
PropertyReflector.HasProperty(myObj, "myProperty")
value := PropertyReflector.GetProperty(myObj, "myProperty")
PropertyReflector.SetProperty(myObj, "myProperty", 123)

func (*TPropertyReflector) GetProperties

func (c *TPropertyReflector) GetProperties(obj interface{}) map[string]interface{}

Get values of all properties in specified object and returns them as a map. Parameters:

  • obj interface{} an object to get properties from.

Returns map[string]interface{} a map, containing the names of the object's properties and their values.

func (*TPropertyReflector) GetProperty

func (c *TPropertyReflector) GetProperty(obj interface{}, name string) interface{}

Gets value of object property specified by its name. Parameters:

  • obj interface{} an object to read property from.
  • name string a name of the property to get.

Returns interface{} the property value or null if property doesn't exist or introspection failed.

func (*TPropertyReflector) GetPropertyNames

func (c *TPropertyReflector) GetPropertyNames(obj interface{}) []string

Gets names of all properties implemented in specified object. Parameters:

  • obj interface{} an object to introspect.

Returns []string a list with property names.

func (*TPropertyReflector) HasProperty

func (c *TPropertyReflector) HasProperty(obj interface{}, name string) bool

Checks if object has a property with specified name.. Parameters:

  • obj interface{} an object to introspect.
  • name string a name of the property to check.

Returns bool true if the object has the property and false if it doesn't.

func (*TPropertyReflector) SetProperties

func (c *TPropertyReflector) SetProperties(obj interface{}, values map[string]interface{})

Sets values of some (all) object properties. If some properties do not exist or introspection fails they are just silently skipped and no errors thrown. see SetProperty Parameters:

  • obj interface{} an object to write properties to.
  • values map[string]interface{} a map, containing property names and their values.

func (*TPropertyReflector) SetProperty

func (c *TPropertyReflector) SetProperty(obj interface{}, name string, value interface{})

Sets value of object property specified by its name. If the property does not exist or introspection fails this method doesn't do anything and doesn't any throw errors. Parameters:

  • obj interface{} an object to write property to. name string a name of the property to set.
  • value interface{} a new value for the property to set.

type TRecursiveObjectReader

type TRecursiveObjectReader struct{}

Helper class to perform property introspection and dynamic reading.

It is similar to ObjectReader but reads properties recursively through the entire object graph. Nested property names are defined using dot notation as "object.subobject.property"

func (*TRecursiveObjectReader) GetProperties

func (c *TRecursiveObjectReader) GetProperties(obj interface{}) map[string]interface{}

Get values of all properties in specified object and its subobjects and returns them as a map. The object can be a user defined object, map or array. Returned properties correspondently are object properties, map key-pairs or array elements with their indexes. Parameters:

  • obj interface{} an object to get properties from.

Returns map[string]interface{} a map, containing the names of the object's properties and their values.

func (*TRecursiveObjectReader) GetProperty

func (c *TRecursiveObjectReader) GetProperty(obj interface{}, name string) interface{}

Recursively gets value of object or its subobjects property specified by its name. The object can be a user defined object, map or array. The property name correspondently must be object property, map key or array index. Parameters:

  • obj interface{} an object to read property from.
  • name string a name of the property to get.

Returns interface{} the property value or null if property doesn't exist or introspection failed.

func (*TRecursiveObjectReader) GetPropertyNames

func (c *TRecursiveObjectReader) GetPropertyNames(obj interface{}) []string

Recursively gets names of all properties implemented in specified object and its subobjects. The object can be a user defined object, map or array. Returned property name correspondently are object properties, map keys or array indexes. Parameters:

  • obj interface{} an object to introspect.

Returns []string a list with property names.

func (*TRecursiveObjectReader) HasProperty

func (c *TRecursiveObjectReader) HasProperty(obj interface{}, name string) bool

type TRecursiveObjectWriter

type TRecursiveObjectWriter struct{}

Helper class to perform property introspection and dynamic writing.

It is similar to ObjectWriter but writes properties recursively through the entire object graph. Nested property names are defined using dot notation as "object.subobject.property"

func (*TRecursiveObjectWriter) CopyProperties

func (c *TRecursiveObjectWriter) CopyProperties(dest interface{}, src interface{})

Copies content of one object to another object by recursively reading all properties from source object and then recursively writing them to destination object. Parameters:

  • dest interface{} a destination object to write properties to.
  • src interface{} a source object to read properties from

func (*TRecursiveObjectWriter) SetProperties

func (c *TRecursiveObjectWriter) SetProperties(obj interface{}, values map[string]interface{})

Recursively sets values of some (all) object and its subobjects properties. The object can be a user defined object, map or array. Property values correspondently are object properties, map key-pairs or array elements with their indexes. If some properties do not exist or introspection fails they are just silently skipped and no errors thrown. see SetProperty Parameters:

  • obj interface{} an object to write properties to.
  • values map[atring]inteerface{} a map, containing property names and their values.

func (*TRecursiveObjectWriter) SetProperty

func (c *TRecursiveObjectWriter) SetProperty(obj interface{}, name string, value interface{})

type TTypeMatcher

type TTypeMatcher struct{}

Helper class matches value types for equality.

This class has symmetric implementation across all languages supported by Pip.Services toolkit and used to support dynamic data processing.

func (*TTypeMatcher) MatchType

func (c *TTypeMatcher) MatchType(expectedType interface{}, actualType refl.Type) bool

Matches expected type to an actual type. The types can be specified as types, type names or TypeCode. Parameters:

  • expectedType interface{} an expected type to match.
  • actualType refl.Type n actual type to match.

Returns bool true if types are matching and false if they don't.

func (*TTypeMatcher) MatchTypeByName

func (c *TTypeMatcher) MatchTypeByName(expectedType string, actualType refl.Type) bool

Matches expected type to an actual type. Parameters:

  • expectedType string an expected type name to match.
  • actualType refl.Type an actual type to match defined by type code.

Returns bool true if types are matching and false if they don't.

func (*TTypeMatcher) MatchValue

func (c *TTypeMatcher) MatchValue(expectedType interface{}, actualValue interface{}) bool

Matches expected type to a type of a value. The expected type can be specified by a type, type name or TypeCode. Parameters:

  • expectedType interface{} an expected type to match.
  • actualValue interface{} a value to match its type to the expected one.

Returns bool true if types are matching and false if they don't.

func (*TTypeMatcher) MatchValueByName

func (c *TTypeMatcher) MatchValueByName(expectedType string, actualValue interface{}) bool

Matches expected type to a type of a value. Parameters:

  • expectedType string an expected type name to match.
  • actualValue interface{} a value to match its type to the expected one.

Returns bool true if types are matching and false if they don't.

type TTypeReflector

type TTypeReflector struct{}

Helper class to perform object type introspection and object instantiation.

This class has symmetric implementation across all languages supported by Pip.Services toolkit and used to support dynamic data processing.

Because all languages have different casing and case sensitivity rules, this TypeReflector treats all type names as case insensitive.

see TypeDescriptor

Example:

descriptor := NewTypeDescriptor("MyObject", "mylibrary");
TypeReflector.GetTypeByDescriptor(descriptor);
myObj = TypeReflector.CreateInstanceByDescriptor(descriptor);
var TypeReflector *TTypeReflector = &TTypeReflector{}

func (*TTypeReflector) CreateInstance

func (c *TTypeReflector) CreateInstance(name string, pkg string, args ...interface{}) (interface{}, error)

Creates an instance of an object type specified by its name and library where it is defined. Parameters:

  • name string an object type name.
  • pkg: string a package (module) where object type is defined.
  • args ...interface{} rguments for the object constructor.

Returns any the created object instance.

func (*TTypeReflector) CreateInstanceByDescriptor

func (c *TTypeReflector) CreateInstanceByDescriptor(typ *TypeDescriptor, args ...interface{}) (interface{}, error)

Returns interface{}, error the created object instance and error.

func (*TTypeReflector) CreateInstanceByType

func (c *TTypeReflector) CreateInstanceByType(typ refl.Type, args ...interface{}) (interface{}, error)

Creates an instance of an object type. Parameters:

  • type refl.Type an object type (factory function) to create. args ...interface{} arguments for the object constructor.

Returns interface{}, error the created object instance and error.

func (*TTypeReflector) GetType

func (c *TTypeReflector) GetType(name string, pkg string) refl.Type

Gets object type by its name and library where it is defined. Parameters:

  • name string an object type name. pkg string a package where the type is defined

Returns refl.Type the object type or nil is the type wasn't found.

func (*TTypeReflector) GetTypeByDescriptor

func (c *TTypeReflector) GetTypeByDescriptor(typ *TypeDescriptor) refl.Type

Gets object type by type descriptor. Parameters:

  • descriptor *TypeDescriptor a type descriptor that points to an object type

Returns refl.Type the object type or nil is the type wasn't found.

type TypeDescriptor

type TypeDescriptor struct {
	// contains filtered or unexported fields
}

Descriptor that points to specific object type by it's name and optional library (or module) where this type is defined.

This class has symmetric implementation across all languages supported by Pip.Services toolkit and used to support dynamic data processing.

func NewTypeDescriptor

func NewTypeDescriptor(name string, pkg string) *TypeDescriptor

Creates a new instance of the type descriptor and sets its values. Parameters:

  • name string a name of the object type.
  • library string a library or module where this object type is implemented.

Returns *TypeDescriptor

func ParseTypeDescriptorFromString

func ParseTypeDescriptorFromString(value string) (*TypeDescriptor, error)

Parses a string to get descriptor fields and returns them as a Descriptor. The string must have format name[,package] throws a ConfigError if the descriptor string is of a wrong format. Parameters:

  • value string a string to parse.

Returns *TypeDescriptor a newly created Descriptor.

func (*TypeDescriptor) Equals

func (c *TypeDescriptor) Equals(obj interface{}) bool

Compares this descriptor to a value. If the value is also a TypeDescriptor it compares their name and library fields. Otherwise this method returns false. Parameters:

  • obj interface{} a value to compare.

Returns bool true if value is identical TypeDescriptor and false otherwise.

func (*TypeDescriptor) Name

func (c *TypeDescriptor) Name() string

Get the name of the object type. Returns string the name of the object type.

func (*TypeDescriptor) Package

func (c *TypeDescriptor) Package() string

Gets the name of the package or module where the object type is defined. Returns string the name of the package or module.

func (*TypeDescriptor) String

func (c *TypeDescriptor) String() string

Gets a string representation of the object. The result has format name[,package] Returns string a string representation of the object.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL