unsafetools

package module
v0.0.0-...-fa20690 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2024 License: CC0-1.0 Imports: 3 Imported by: 17

README

Coverage Status GoDoc Go Report Card

Description

This package provides function FieldByName to access to any field (including private/unexported) of a structure.

Use case

This package is supposed to be used for unit-tests only. If you think about using it in a real production then it seems it is something wrong in your program. However yes, you can use is if you want :)

An use case example in github.com/xaionaro-go/picapi.

Example

github.com/xaionaro-go/unsafetools/test/types.go

package test

type privateStruct struct {
	enableBonus bool
}

type StructWithPrivate struct {
	privateStruct

	initialized bool
}

func (s *StructWithPrivate) HelloWorld() (result string) {
	defer func() {
		if s.enableBonus {
			result += ` (bonus!)`
		}
	}()

	if !s.initialized {
		return ``
	}

	return `hello world!`
}

github.com/xaionaro-go/unsafetools/unsafetools_test.go

import (
	"testing"

	"github.com/stretchr/testify/assert"

	"github.com/xaionaro-go/unsafetools/test"
)

func TestFindByName_positive(t *testing.T) {
	s := &test.StructWithPrivate{}

	assert.Equal(t, ``, s.HelloWorld())

	*FieldByName(s, `initialized`).(*bool) = true
	assert.Equal(t, `hello world!`, s.HelloWorld())

	*FieldByName(s, `initialized`).(*bool) = false
	assert.Equal(t, ``, s.HelloWorld())
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BytesOf

func BytesOf(obj any) []byte

func BytesToSliceHeader

func BytesToSliceHeader(b []byte) *reflect.SliceHeader

func CastBytesToString

func CastBytesToString(b []byte) string

CastBytesToString casts a slice of bytes to a string in an unsafe way: the string is not guaranteed to be immutable (since it just reuses the same headers as the slice of bytes, which is modifiable).

This is a zero-memory-allocation function.

func CastStringToBytes

func CastStringToBytes(s string) []byte

CastStringToBytes casts a string to a slice of bytes in an unsafe way: the string is not guaranteed to be immutable (since the returned slice has the same pointer underneath).

func FieldByIndex

func FieldByIndex[F any, T any, PTR Pointer[T]](obj PTR, idx int) *F

FieldByIndex receives a pointer to a struct into `obj` and returns a pointer to the value of the field with index `idx` (even if it's a private field), similarly to how `reflect.(*Value).Field` works.

It will panic if it was passed non-pointer or/and to non-structure to `obj`, or if field with index `idx` does not exist in the structure.

func FieldByIndexInValue

func FieldByIndexInValue(v reflect.Value, idx int) reflect.Value

FieldByIndexInValue does the same as FieldByIndex, but works with reflect.Value instead of interfaces.

func FieldByName

func FieldByName[F any, T any, PTR Pointer[T]](obj PTR, fieldName string) *F

FieldByName receives a pointer to a struct into `obj` and returns a pointer to the value of the field with name `fieldName` (even if it's a private field).

It will panic if it was passed non-pointer or/and to non-structure to `obj`, or if field `fieldName` does not exist in the structure.

func FieldByNameInValue

func FieldByNameInValue(v reflect.Value, fieldName string) reflect.Value

FieldByNameInValue does the same as FieldByName, but works with reflect.Value instead of interfaces.

func FieldByPath

func FieldByPath[F any, T any, PTR Pointer[T]](obj PTR, path []string) *F

FieldByPath receives a pointer to a struct into `obj` and returns a pointer to the value of the field by path `path` (even if there're private fields in the path).

`path` is a series of field names which are used to recursively deep into the structure to get the required field.

It will panic if it was passed non-pointer or/and to non-structure to `obj`, or if a field from the path does not exist in the structure.

func FieldByPathInValue

func FieldByPathInValue(in reflect.Value, path []string) reflect.Value

FieldByPathInValue does the same as FieldByPath, but works with reflect.Value instead of interfaces.

func SetPointerToBytes

func SetPointerToBytes(pointerPointer interface{}, b []byte) error

Types

type ErrSliceTooShort

type ErrSliceTooShort struct {
	RequiredSize uintptr
	RealSize     uintptr
}

func (*ErrSliceTooShort) Error

func (err *ErrSliceTooShort) Error() string

type Pointer

type Pointer[T any] interface {
	*T
}

Pointer is a constraint for pointers only.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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