reflect

package
v1.0.1-3 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2022 License: MIT Imports: 8 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

View Source
var MethodReflector = &_TMethodReflector{}

MethodReflector 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);
View Source
var ObjectReader = &_TObjectReader{}

ObjectReader 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")
View Source
var ObjectWriter = &_TObjectWriter{}

ObjectWriter 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)
View Source
var PropertyReflector = &_TPropertyReflector{}

PropertyReflector 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)
View Source
var RecursiveObjectReader = &TRecursiveObjectReader{}

RecursiveObjectReader 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"

View Source
var RecursiveObjectWriter = &_TRecursiveObjectWriter{}

RecursiveObjectWriter 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"

View Source
var TypeMatcher = &_TTypeMatcher{}

TypeMatcher 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.

View Source
var TypeReflector = &TTypeReflector{}

TypeReflector 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);

Functions

This section is empty.

Types

type IValueWrapper

type IValueWrapper interface {
	InnerValue() any
}

type TRecursiveObjectReader

type TRecursiveObjectReader struct{}

func (*TRecursiveObjectReader) GetProperties

func (c *TRecursiveObjectReader) GetProperties(obj any) map[string]any

GetProperties 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 any an object to get properties from.
Returns: map[string]any a map, containing the names of the object's properties and their values.

func (*TRecursiveObjectReader) GetProperty

func (c *TRecursiveObjectReader) GetProperty(obj any, name string) any

GetProperty 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 any an object to read property from.
	- name string a name of the property to get.
Returns: any the property value or null if property doesn't exist or introspection failed.

func (*TRecursiveObjectReader) GetPropertyNames

func (c *TRecursiveObjectReader) GetPropertyNames(obj any) []string

GetPropertyNames 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 any an object to introspect.
Returns: []string a list with property names.

func (*TRecursiveObjectReader) HasProperty

func (c *TRecursiveObjectReader) HasProperty(obj any, name string) bool

HasProperty checks recursively if object or its subobjects 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 any an object to introspect.
	- name string a name of the property to check.
Returns: boolean true if the object has the property and false if it doesn't.

type TTypeReflector

type TTypeReflector struct{}

func (*TTypeReflector) CreateInstance

func (c *TTypeReflector) CreateInstance(name string, pkg string, args ...any) (any, error)

CreateInstance 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 ...any arguments for the object constructor.
Returns: any the created object instance.

func (*TTypeReflector) CreateInstanceByDescriptor

func (c *TTypeReflector) CreateInstanceByDescriptor(typ *TypeDescriptor, args ...any) (any, error)

CreateInstanceByDescriptor creates an instance of an object type specified by type descriptor.

Parameters:
	- descriptor *TypeDescriptor a type descriptor that points to an object type
	- args ...any arguments for the object constructor.
Returns any, error the created object instance and error.

func (*TTypeReflector) CreateInstanceByType

func (c *TTypeReflector) CreateInstanceByType(typ refl.Type, args ...any) (any, error)

CreateInstanceByType creates an instance of an object type.

Parameters:
	- type refl.Type an object type (factory function) to create.
	- args ...any arguments for the object constructor.
Returns any, error  the created object instance and error.

func (*TTypeReflector) GetType

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

GetType 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

GetTypeByDescriptor 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
}

TypeDescriptor is a 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

NewTypeDescriptor 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)

ParseTypeDescriptorFromString 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 any) bool

Equals 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 any a value to compare.
Returns: bool true if value is identical TypeDescriptor and false otherwise.

func (*TypeDescriptor) Name

func (c *TypeDescriptor) Name() string

Name get the name of the object type.

Returns: string the name of the object type.

func (*TypeDescriptor) Package

func (c *TypeDescriptor) Package() string

Package 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

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