lreflect

package
v0.0.0-...-9392aba Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package lreflect provides reflect extensions for use by the lifetime package.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CopySlice

func CopySlice(slice reflect.Value) reflect.Value

CopySlice makes a copy of the provided slice. When slice is not a slice, the behavior is undefined.

func FilterSliceInterface

func FilterSliceInterface(slice reflect.Value, I reflect.Type) (reflect.Value, error)

FilterSliceInterface filters the slice S by all elements which implement the interface I and returns a new slice of I. slice must be a slice of some type (preferably some interface), I must be an interface.

func FirstAssignableInterfaceElement

func FirstAssignableInterfaceElement(slice reflect.Value, V reflect.Type) (reflect.Value, error)

FirstAssignableElement finds the first element in slice that is assignable V. If no such element exists, returns the zero value of V.

slice must be a slice of some interface type.

func ImplementsAsSliceInterface

func ImplementsAsSliceInterface(I reflect.Type, T reflect.Type) (bool, error)

ImplementsAsSliceInterface checks if T is a slice type with an interface element that implements I. I must be an interface, T may be any type.

func ImplementsAsStructPointer

func ImplementsAsStructPointer(I reflect.Type, T reflect.Type) (bool, error)

ImplementsStructAsPointer checks if T implements I and T is a pointer to a struct. I must be an interface type, T may be any type.

func SortSliceByRank

func SortSliceByRank(slice reflect.Value) error

SortSliceByRank sorts slice by a magic rank function found on the element type of slice. Slice must be a slice of some value.

The rank function has to be called "Rank${ElementType}" and have signature func()T. T must be comparable, meaning it is of kind int, uint, float or string. If no such function exists on the element type of slice, it is returned unchanged.

The sort performed is guaranteed to be stable, meaning to equally do not change positions.

Example
//spellchecker:words lreflect
package main

//spellchecker:words reflect
import (
	"fmt"
	"reflect"
)

//spellchecker:words rankable

type RankableStruct string

// RankableStruct implements a special method RankRankableStruct
// That can be used to sort the slice by length
func (r RankableStruct) RankRankableStruct() uint64 {
	return uint64(len(r))
}

// RankableStruct also implements a special method RankRankableInterface.
// That can be used to sort the slice in inverted length fashion
func (r RankableStruct) RankRankableInterface() int {
	return -int(len(r))
}

type RankableInterface interface {
	// RankRankableInterface sorts slices of type RankableInterface
	RankRankableInterface() int
}

func main() {
	{
		// take a slice of type RankableStruct, and sort by RankRankableStruct
		values := []RankableStruct{
			"yoda",
			"am",
			"i",
		}

		_ = SortSliceByRank(reflect.ValueOf(values))
		fmt.Println(values)
	}

	{
		// take a slice of type RankableInterface, and sort by RankRankableInterface
		values := []RankableInterface{
			RankableStruct("i"),
			RankableStruct("yoda"),
			RankableStruct("am"),
		}
		_ = SortSliceByRank(reflect.ValueOf(values))

		fmt.Println(values)
	}

	{
		// take a slice of type string, and don't sort it (because no sort method exists)
		values := []string{
			"i",
			"yoda",
			"am",
		}
		_ = SortSliceByRank(reflect.ValueOf(values))

		fmt.Println(values)
	}

}
Output:

[i am yoda]
[yoda am i]
[i yoda am]

func UnsafeSetAnyValue

func UnsafeSetAnyValue(v, x reflect.Value) error

UnsafeSetAnyValue is like v.Set(x) except that it permits a value obtained from an unexported field to be set. It never panics, and instead returns an error.

DO NOT USE THIS UNLESS YOU KNOW WHAT YOU'RE DOING.

Example
private := HasAPrivateField{}

// get and set the private field
value := reflect.ValueOf(&private).Elem().FieldByName("private")
_ = UnsafeSetAnyValue(value, reflect.ValueOf("I was set via reflect"))

fmt.Println(private.Private())
Output:

I was set via reflect

Types

This section is empty.

Jump to

Keyboard shortcuts

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