reflector

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2023 License: Apache-2.0 Imports: 5 Imported by: 2 modules, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Reflector

Reflector is a Go object manipulator

Sometime we will need to update object dynamically or programatically. This is what reflector is used for.

type childObj struct {
	Name   string
	Values []int
}

type obj struct {
	ID   string
	Name string
	Int  int
	Dec  float64
	Date time.Time

	Children []*childObj
}

func TestReflector(t *testing.T) {
	cv.Convey("reflector", t, func() {
		data := new(obj)
		err := reflector.From(data).
			Set("ID", "Obj1").
			Set("Name", "Obj1 Name").
			Set("Int", 10).
			Set("Dec", float64(20.30)).
			Set("Date", time.Now()).
			Flush()
		cv.So(err, cv.ShouldBeNil)
		cv.So(data.Dec, cv.ShouldEqual, 20.30)

		cv.Convey("update child", func() {
			children := []*childObj{}
			children = append(children, &childObj{"child1", []int{10, 20, 30}}, &childObj{"child2", []int{11, 21, 31}})
			err = reflector.From(data).Set("Children", children).Flush()
			cv.So(err, cv.ShouldBeNil)
			cv.So(data.Children[1].Values[1], cv.ShouldEqual, 21)

			cv.Convey("update child entity", func() {
				err = reflector.From(data.Children[0]).Set("Values", []int{1, 2, 3}).Flush()
				cv.So(err, cv.ShouldBeNil)
				cv.So(data.Children[0].Values[2], cv.ShouldEqual, 3)
			})
		})
	})
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssignSliceItem

func AssignSliceItem(data reflect.Value, index int, dest reflect.Value) error

func AssignValue

func AssignValue(source reflect.Value, dest reflect.Value) error

func CopyAttributeByNames added in v0.0.4

func CopyAttributeByNames[T any](source interface{}, dest T, copiedFieldNames ...string) (T, error)

func CopyAttributes added in v0.0.4

func CopyAttributes[T any](source interface{}, dest T, excludeFieldNames ...string) (T, error)

Types

type Reflector

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

func From

func From(obj interface{}) *Reflector

func (*Reflector) Error

func (r *Reflector) Error() error

func (*Reflector) FieldNames

func (r *Reflector) FieldNames(tag string) ([]string, error)

func (*Reflector) Flush

func (r *Reflector) Flush() error

func (*Reflector) Get

func (r *Reflector) Get(name string) (interface{}, error)

func (*Reflector) Set

func (r *Reflector) Set(name string, value interface{}) *Reflector

Jump to

Keyboard shortcuts

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