Full Code of AlekSi/pointer for AI

main 039be97b958c cached
10 files
25.1 KB
8.5k tokens
93 symbols
1 requests
Download .txt
Repository: AlekSi/pointer
Branch: main
Commit: 039be97b958c
Files: 10
Total size: 25.1 KB

Directory structure:
gitextract_87t1mn15/

├── .github/
│   └── workflows/
│       └── go.yml
├── LICENSE
├── README.md
├── generic.go
├── generic_test.go
├── go.mod
├── pointer.go
├── pointer_or_nil.go
├── pointer_test.go
└── value.go

================================================
FILE CONTENTS
================================================

================================================
FILE: .github/workflows/go.yml
================================================
---
name: Go
on:
  push:
    branches:
      - main
  pull_request:
  schedule:
    - cron: '42 3 * * 4'

jobs:
  test:
    name: Test
    runs-on: ubuntu-20.04

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Install Go tip
        run: |
          sudo rm -fr /opt/hostedtoolcache/go /usr/local/go /usr/bin/go /bin/go
          curl -o go.tar.gz -L \
            https://github.com/AlekSi/golang-tip/releases/download/tip/master.linux-amd64.tar.gz
          sudo tar -C /usr/local -xzf go.tar.gz
          sudo ln -s /usr/local/go/bin/* /usr/local/bin/
          go version
          rm go.tar.gz

      - name: Run tests
        run: go test -v


================================================
FILE: LICENSE
================================================
The MIT License (MIT)

Copyright (c) 2015 Alexey Palazhchenko

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.


================================================
FILE: README.md
================================================
# pointer

[![Go Reference](https://pkg.go.dev/badge/github.com/AlekSi/pointer.svg)](https://pkg.go.dev/github.com/AlekSi/pointer)

Go package `pointer` provides helpers to convert between pointers and values of built-in
(and, with Go 1.18+ generics, of any) types.

```
go get github.com/AlekSi/pointer
```

API is stable. [Documentation](https://pkg.go.dev/github.com/AlekSi/pointer).

```go
package motivationalexample

import (
	"encoding/json"

	"github.com/AlekSi/pointer"
)

const (
	defaultName = "some name"
)

// Stuff contains optional fields.
type Stuff struct {
	Name    *string
	Comment *string
	Value   *int64
	Time    *time.Time
}

// SomeStuff makes some JSON-encoded stuff.
func SomeStuff() (data []byte, err error) {
	return json.Marshal(&Stuff{
		Name:    pointer.ToString(defaultName),                                   // can't say &defaultName
		Comment: pointer.ToString("not yet"),                                     // can't say &"not yet"
		Value:   pointer.ToInt64(42),                                             // can't say &42 or &int64(42)
		Time:    pointer.ToTime(time.Date(2014, 6, 25, 12, 24, 40, 0, time.UTC)), // can't say &time.Date(…)
	})
}
```


================================================
FILE: generic.go
================================================
//go:build go1.18
// +build go1.18

package pointer

// To returns a pointer to the passed value.
func To[T any](t T) *T {
	return &t
}

// ToOrNil returns a pointer to the passed value, or nil, if the passed value is a zero value.
// If the passed value has `IsZero() bool` method (for example, time.Time instance),
// it is used to determine if the value is zero.
func ToOrNil[T comparable](t T) *T {
	if z, ok := any(t).(interface{ IsZero() bool }); ok {
		if z.IsZero() {
			return nil
		}
		return &t
	}

	var zero T
	if t == zero {
		return nil
	}
	return &t
}

// Get returns the value from the passed pointer or the zero value if the pointer is nil.
func Get[T any](t *T) T {
	if t == nil {
		var zero T
		return zero
	}
	return *t
}


================================================
FILE: generic_test.go
================================================
//go:build go1.18
// +build go1.18

package pointer

import (
	"testing"
	"time"
)

func TestGeneric(t *testing.T) {
	var x time.Time
	if *To(x) != x {
		t.Errorf("*To(%v)", x)
	}
	if ToOrNil(x) != nil {
		t.Errorf("ToOrNil(%v)", x)
	}
	if Get((*time.Time)(nil)) != x {
		t.Errorf("Time(%v)", nil)
	}

	x = time.Date(2014, 6, 25, 12, 24, 40, 0, time.UTC)
	if *To(x) != x {
		t.Errorf("*To(%v)", x)
	}
	if *ToOrNil(x) != x {
		t.Errorf("*ToOrNil(%v)", x)
	}
	if Get(&x) != x {
		t.Errorf("Get(%v)", &x)
	}
}


================================================
FILE: go.mod
================================================
module github.com/AlekSi/pointer

go 1.18


================================================
FILE: pointer.go
================================================
// Package pointer provides helpers to convert between pointers and values of built-in (and, with generics, of any) types.
package pointer // import "github.com/AlekSi/pointer"

import (
	"time"
)

/*
Order as in spec:
	bool byte complex64 complex128 error float32 float64
	int int8 int16 int32 int64 rune string
	uint uint8 uint16 uint32 uint64 uintptr
	time.Duration time.Time
*/

// ToBool returns a pointer to the passed bool value.
func ToBool(b bool) *bool {
	return &b
}

// ToByte returns a pointer to the passed byte value.
func ToByte(b byte) *byte {
	return &b
}

// ToComplex64 returns a pointer to the passed complex64 value.
func ToComplex64(c complex64) *complex64 {
	return &c
}

// ToComplex128 returns a pointer to the passed complex128 value.
func ToComplex128(c complex128) *complex128 {
	return &c
}

// ToError returns a pointer to the passed error value.
func ToError(e error) *error {
	return &e
}

// ToFloat32 returns a pointer to the passed float32 value.
func ToFloat32(f float32) *float32 {
	return &f
}

// ToFloat64 returns a pointer to the passed float64 value.
func ToFloat64(f float64) *float64 {
	return &f
}

// ToInt returns a pointer to the passed int value.
func ToInt(i int) *int {
	return &i
}

// ToInt8 returns a pointer to the passed int8 value.
func ToInt8(i int8) *int8 {
	return &i
}

// ToInt16 returns a pointer to the passed int16 value.
func ToInt16(i int16) *int16 {
	return &i
}

// ToInt32 returns a pointer to the passed int32 value.
func ToInt32(i int32) *int32 {
	return &i
}

// ToInt64 returns a pointer to the passed int64 value.
func ToInt64(i int64) *int64 {
	return &i
}

// ToRune returns a pointer to the passed rune value.
func ToRune(r rune) *rune {
	return &r
}

// ToString returns a pointer to the passed string value.
func ToString(s string) *string {
	return &s
}

// ToUint returns a pointer to the passed uint value.
func ToUint(u uint) *uint {
	return &u
}

// ToUint8 returns a pointer to the passed uint8 value.
func ToUint8(u uint8) *uint8 {
	return &u
}

// ToUint16 returns a pointer to the passed uint16 value.
func ToUint16(u uint16) *uint16 {
	return &u
}

// ToUint32 returns a pointer to the passed uint32 value.
func ToUint32(u uint32) *uint32 {
	return &u
}

// ToUint64 returns a pointer to the passed uint64 value.
func ToUint64(u uint64) *uint64 {
	return &u
}

// ToUintptr returns a pointer to the passed uintptr value.
func ToUintptr(u uintptr) *uintptr {
	return &u
}

// ToDuration returns a pointer to the passed time.Duration value.
func ToDuration(d time.Duration) *time.Duration {
	return &d
}

// ToTime returns a pointer to the passed time.Time value.
func ToTime(t time.Time) *time.Time {
	return &t
}


================================================
FILE: pointer_or_nil.go
================================================
package pointer

import (
	"time"
)

/*
Order as in spec:
	bool byte complex64 complex128 error float32 float64
	int int8 int16 int32 int64 rune string
	uint uint8 uint16 uint32 uint64 uintptr
	time.Duration time.Time
*/

// ToBoolOrNil returns a pointer to the passed bool value, or nil, if passed value is a zero value.
func ToBoolOrNil(b bool) *bool {
	if b == false {
		return nil
	}
	return &b
}

// ToByteOrNil returns a pointer to the passed byte value, or nil, if passed value is a zero value.
func ToByteOrNil(b byte) *byte {
	if b == 0 {
		return nil
	}
	return &b
}

// ToComplex64OrNil returns a pointer to the passed complex64 value, or nil, if passed value is a zero value.
func ToComplex64OrNil(c complex64) *complex64 {
	if c == 0 {
		return nil
	}
	return &c
}

// ToComplex128OrNil returns a pointer to the passed complex128 value, or nil, if passed value is a zero value.
func ToComplex128OrNil(c complex128) *complex128 {
	if c == 0 {
		return nil
	}
	return &c
}

// ToErrorOrNil returns a pointer to the passed error value, or nil, if passed value is a zero value.
func ToErrorOrNil(e error) *error {
	if e == nil {
		return nil
	}
	return &e
}

// ToFloat32OrNil returns a pointer to the passed float32 value, or nil, if passed value is a zero value.
func ToFloat32OrNil(f float32) *float32 {
	if f == 0 {
		return nil
	}
	return &f
}

// ToFloat64OrNil returns a pointer to the passed float64 value, or nil, if passed value is a zero value.
func ToFloat64OrNil(f float64) *float64 {
	if f == 0 {
		return nil
	}
	return &f
}

// ToIntOrNil returns a pointer to the passed int value, or nil, if passed value is a zero value.
func ToIntOrNil(i int) *int {
	if i == 0 {
		return nil
	}
	return &i
}

// ToInt8OrNil returns a pointer to the passed int8 value, or nil, if passed value is a zero value.
func ToInt8OrNil(i int8) *int8 {
	if i == 0 {
		return nil
	}
	return &i
}

// ToInt16OrNil returns a pointer to the passed int16 value, or nil, if passed value is a zero value.
func ToInt16OrNil(i int16) *int16 {
	if i == 0 {
		return nil
	}
	return &i
}

// ToInt32OrNil returns a pointer to the passed int32 value, or nil, if passed value is a zero value.
func ToInt32OrNil(i int32) *int32 {
	if i == 0 {
		return nil
	}
	return &i
}

// ToInt64OrNil returns a pointer to the passed int64 value, or nil, if passed value is a zero value.
func ToInt64OrNil(i int64) *int64 {
	if i == 0 {
		return nil
	}
	return &i
}

// ToRuneOrNil returns a pointer to the passed rune value, or nil, if passed value is a zero value.
func ToRuneOrNil(r rune) *rune {
	if r == 0 {
		return nil
	}
	return &r
}

// ToStringOrNil returns a pointer to the passed string value, or nil, if passed value is a zero value.
func ToStringOrNil(s string) *string {
	if s == "" {
		return nil
	}
	return &s
}

// ToUintOrNil returns a pointer to the passed uint value, or nil, if passed value is a zero value.
func ToUintOrNil(u uint) *uint {
	if u == 0 {
		return nil
	}
	return &u
}

// ToUint8OrNil returns a pointer to the passed uint8 value, or nil, if passed value is a zero value.
func ToUint8OrNil(u uint8) *uint8 {
	if u == 0 {
		return nil
	}
	return &u
}

// ToUint16OrNil returns a pointer to the passed uint16 value, or nil, if passed value is a zero value.
func ToUint16OrNil(u uint16) *uint16 {
	if u == 0 {
		return nil
	}
	return &u
}

// ToUint32OrNil returns a pointer to the passed uint32 value, or nil, if passed value is a zero value.
func ToUint32OrNil(u uint32) *uint32 {
	if u == 0 {
		return nil
	}
	return &u
}

// ToUint64OrNil returns a pointer to the passed uint64 value, or nil, if passed value is a zero value.
func ToUint64OrNil(u uint64) *uint64 {
	if u == 0 {
		return nil
	}
	return &u
}

// ToUintptrOrNil returns a pointer to the passed uintptr value, or nil, if passed value is a zero value.
func ToUintptrOrNil(u uintptr) *uintptr {
	if u == 0 {
		return nil
	}
	return &u
}

// ToDurationOrNil returns a pointer to the passed time.Duration value, or nil, if passed value is a zero value.
func ToDurationOrNil(d time.Duration) *time.Duration {
	if d == 0 {
		return nil
	}
	return &d
}

// ToTimeOrNil returns a pointer to the passed time.Time value, or nil, if passed value is a zero value (t.IsZero() returns true).
func ToTimeOrNil(t time.Time) *time.Time {
	if t.IsZero() {
		return nil
	}
	return &t
}


================================================
FILE: pointer_test.go
================================================
package pointer

import (
	"encoding/json"
	"errors"
	"fmt"
	"testing"
	"time"
)

/*
Order as in spec:
	bool byte complex64 complex128 error float32 float64
	int int8 int16 int32 int64 rune string
	uint uint8 uint16 uint32 uint64 uintptr
	time.Duration time.Time
*/

func TestBool(t *testing.T) {
	var x bool
	if *ToBool(x) != x {
		t.Errorf("*ToBool(%v)", x)
	}
	if ToBoolOrNil(x) != nil {
		t.Errorf("ToBoolOrNil(%v)", x)
	}
	if GetBool(nil) != x {
		t.Errorf("GetBool(%v)", nil)
	}

	x = true
	if *ToBool(x) != x {
		t.Errorf("*ToBool(%v)", x)
	}
	if *ToBoolOrNil(x) != x {
		t.Errorf("*ToBoolOrNil(%v)", x)
	}
	if GetBool(&x) != x {
		t.Errorf("GetBool(%v)", &x)
	}
}

func TestByte(t *testing.T) {
	var x byte
	if *ToByte(x) != x {
		t.Errorf("*ToByte(%v)", x)
	}
	if ToByteOrNil(x) != nil {
		t.Errorf("ToByteOrNil(%v)", x)
	}
	if GetByte(nil) != x {
		t.Errorf("GetByte(%v)", nil)
	}

	x = 42
	if *ToByte(x) != x {
		t.Errorf("*ToByte(%v)", x)
	}
	if *ToByteOrNil(x) != x {
		t.Errorf("*ToByteOrNil(%v)", x)
	}
	if GetByte(&x) != x {
		t.Errorf("GetByte(%v)", &x)
	}
}

func TestComplex64(t *testing.T) {
	var x complex64
	if *ToComplex64(x) != x {
		t.Errorf("*ToComplex64(%v)", x)
	}
	if ToComplex64OrNil(x) != nil {
		t.Errorf("ToComplex64OrNil(%v)", x)
	}
	if GetComplex64(nil) != x {
		t.Errorf("GetComplex64(%v)", nil)
	}

	x = 42
	if *ToComplex64(x) != x {
		t.Errorf("*ToComplex64(%v)", x)
	}
	if *ToComplex64OrNil(x) != x {
		t.Errorf("*ToComplex64OrNil(%v)", x)
	}
	if GetComplex64(&x) != x {
		t.Errorf("GetComplex64(%v)", &x)
	}
}

func TestComplex128(t *testing.T) {
	var x complex128
	if *ToComplex128(x) != x {
		t.Errorf("*ToComplex128(%v)", x)
	}
	if ToComplex128OrNil(x) != nil {
		t.Errorf("ToComplex128OrNil(%v)", x)
	}
	if GetComplex128(nil) != x {
		t.Errorf("GetComplex128(%v)", nil)
	}

	x = 42
	if *ToComplex128(x) != x {
		t.Errorf("*ToComplex128(%v)", x)
	}
	if *ToComplex128OrNil(x) != x {
		t.Errorf("*ToComplex128OrNil(%v)", x)
	}
	if GetComplex128(&x) != x {
		t.Errorf("GetComplex128(%v)", &x)
	}
}

func TestError(t *testing.T) {
	var x error
	if *ToError(x) != x {
		t.Errorf("*ToError(%v)", x)
	}
	if ToErrorOrNil(x) != nil {
		t.Errorf("ToErrorOrNil(%v)", x)
	}
	if GetError(nil) != x {
		t.Errorf("GetError(%v)", nil)
	}

	x = errors.New("error")
	if *ToError(x) != x {
		t.Errorf("*ToError(%v)", x)
	}
	if *ToErrorOrNil(x) != x {
		t.Errorf("*ToErrorOrNil(%v)", x)
	}
	if GetError(&x) != x {
		t.Errorf("GetError(%v)", &x)
	}
}

func TestFloat32(t *testing.T) {
	var x float32
	if *ToFloat32(x) != x {
		t.Errorf("*ToFloat32(%v)", x)
	}
	if ToFloat32OrNil(x) != nil {
		t.Errorf("ToFloat32OrNil(%v)", x)
	}
	if GetFloat32(nil) != x {
		t.Errorf("GetFloat32(%v)", nil)
	}

	x = 42
	if *ToFloat32(x) != x {
		t.Errorf("*ToFloat32(%v)", x)
	}
	if *ToFloat32OrNil(x) != x {
		t.Errorf("*ToFloat32OrNil(%v)", x)
	}
	if GetFloat32(&x) != x {
		t.Errorf("GetFloat32(%v)", &x)
	}
}

func TestFloat64(t *testing.T) {
	var x float64
	if *ToFloat64(x) != x {
		t.Errorf("*ToFloat64(%v)", x)
	}
	if ToFloat64OrNil(x) != nil {
		t.Errorf("ToFloat64OrNil(%v)", x)
	}
	if GetFloat64(nil) != x {
		t.Errorf("GetFloat64(%v)", nil)
	}

	x = 42
	if *ToFloat64(x) != x {
		t.Errorf("*ToFloat64(%v)", x)
	}
	if *ToFloat64OrNil(x) != x {
		t.Errorf("*ToFloat64OrNil(%v)", x)
	}
	if GetFloat64(&x) != x {
		t.Errorf("GetFloat64(%v)", &x)
	}
}

func TestInt(t *testing.T) {
	var x int
	if *ToInt(x) != x {
		t.Errorf("*ToInt(%v)", x)
	}
	if ToIntOrNil(x) != nil {
		t.Errorf("ToIntOrNil(%v)", x)
	}
	if GetInt(nil) != x {
		t.Errorf("GetInt(%v)", nil)
	}

	x = 42
	if *ToInt(x) != x {
		t.Errorf("*ToInt(%v)", x)
	}
	if *ToIntOrNil(x) != x {
		t.Errorf("*ToIntOrNil(%v)", x)
	}
	if GetInt(&x) != x {
		t.Errorf("GetInt(%v)", &x)
	}
}

func TestInt8(t *testing.T) {
	var x int8
	if *ToInt8(x) != x {
		t.Errorf("*ToInt8(%v)", x)
	}
	if ToInt8OrNil(x) != nil {
		t.Errorf("ToInt8OrNil(%v)", x)
	}
	if GetInt8(nil) != x {
		t.Errorf("GetInt8(%v)", nil)
	}

	x = 42
	if *ToInt8(x) != x {
		t.Errorf("*ToInt8(%v)", x)
	}
	if *ToInt8OrNil(x) != x {
		t.Errorf("*ToInt8OrNil(%v)", x)
	}
	if GetInt8(&x) != x {
		t.Errorf("GetInt8(%v)", &x)
	}
}

func TestInt16(t *testing.T) {
	var x int16
	if *ToInt16(x) != x {
		t.Errorf("*ToInt16(%v)", x)
	}
	if ToInt16OrNil(x) != nil {
		t.Errorf("ToInt16OrNil(%v)", x)
	}
	if GetInt16(nil) != x {
		t.Errorf("GetInt16(%v)", nil)
	}

	x = 42
	if *ToInt16(x) != x {
		t.Errorf("*ToInt16(%v)", x)
	}
	if *ToInt16OrNil(x) != x {
		t.Errorf("*ToInt16OrNil(%v)", x)
	}
	if GetInt16(&x) != x {
		t.Errorf("GetInt16(%v)", &x)
	}
}

func TestInt32(t *testing.T) {
	var x int32
	if *ToInt32(x) != x {
		t.Errorf("*ToInt32(%v)", x)
	}
	if ToInt32OrNil(x) != nil {
		t.Errorf("ToInt32OrNil(%v)", x)
	}
	if GetInt32(nil) != x {
		t.Errorf("GetInt32(%v)", nil)
	}

	x = 42
	if *ToInt32(x) != x {
		t.Errorf("*ToInt32(%v)", x)
	}
	if *ToInt32OrNil(x) != x {
		t.Errorf("*ToInt32OrNil(%v)", x)
	}
	if GetInt32(&x) != x {
		t.Errorf("GetInt32(%v)", &x)
	}
}

func TestInt64(t *testing.T) {
	var x int64
	if *ToInt64(x) != x {
		t.Errorf("*ToInt64(%v)", x)
	}
	if ToInt64OrNil(x) != nil {
		t.Errorf("ToInt64OrNil(%v)", x)
	}
	if GetInt64(nil) != x {
		t.Errorf("GetInt64(%v)", nil)
	}

	x = 42
	if *ToInt64(x) != x {
		t.Errorf("*ToInt64(%v)", x)
	}
	if *ToInt64OrNil(x) != x {
		t.Errorf("*ToInt64OrNil(%v)", x)
	}
	if GetInt64(&x) != x {
		t.Errorf("GetInt64(%v)", &x)
	}
}

func TestRune(t *testing.T) {
	var x rune
	if *ToRune(x) != x {
		t.Errorf("*ToRune(%v)", x)
	}
	if ToRuneOrNil(x) != nil {
		t.Errorf("ToRuneOrNil(%v)", x)
	}
	if GetRune(nil) != x {
		t.Errorf("GetRune(%v)", nil)
	}

	x = 'x'
	if *ToRune(x) != x {
		t.Errorf("*ToRune(%v)", x)
	}
	if *ToRuneOrNil(x) != x {
		t.Errorf("*ToRuneOrNil(%v)", x)
	}
	if GetRune(&x) != x {
		t.Errorf("GetRune(%v)", &x)
	}
}

func TestString(t *testing.T) {
	var x string
	if *ToString(x) != x {
		t.Errorf("*ToString(%v)", x)
	}
	if ToStringOrNil(x) != nil {
		t.Errorf("ToStringOrNil(%v)", x)
	}
	if GetString(nil) != x {
		t.Errorf("GetString(%v)", nil)
	}

	x = "x"
	if *ToString(x) != x {
		t.Errorf("*ToString(%v)", x)
	}
	if *ToStringOrNil(x) != x {
		t.Errorf("*ToStringOrNil(%v)", x)
	}
	if GetString(&x) != x {
		t.Errorf("GetString(%v)", &x)
	}
}

func TestUint(t *testing.T) {
	var x uint
	if *ToUint(x) != x {
		t.Errorf("*ToUint(%v)", x)
	}
	if ToUintOrNil(x) != nil {
		t.Errorf("ToUintOrNil(%v)", x)
	}
	if GetUint(nil) != x {
		t.Errorf("GetUint(%v)", nil)
	}

	x = 42
	if *ToUint(x) != x {
		t.Errorf("*ToUint(%v)", x)
	}
	if *ToUintOrNil(x) != x {
		t.Errorf("*ToUintOrNil(%v)", x)
	}
	if GetUint(&x) != x {
		t.Errorf("GetUint(%v)", &x)
	}
}

func TestUint8(t *testing.T) {
	var x uint8
	if *ToUint8(x) != x {
		t.Errorf("*ToUint8(%v)", x)
	}
	if ToUint8OrNil(x) != nil {
		t.Errorf("ToUint8OrNil(%v)", x)
	}
	if GetUint8(nil) != x {
		t.Errorf("GetUint8(%v)", nil)
	}

	x = 42
	if *ToUint8(x) != x {
		t.Errorf("*ToUint8(%v)", x)
	}
	if *ToUint8OrNil(x) != x {
		t.Errorf("*ToUint8OrNil(%v)", x)
	}
	if GetUint8(&x) != x {
		t.Errorf("GetUint8(%v)", &x)
	}
}

func TestUint16(t *testing.T) {
	var x uint16
	if *ToUint16(x) != x {
		t.Errorf("*ToUint16(%v)", x)
	}
	if ToUint16OrNil(x) != nil {
		t.Errorf("ToUint16OrNil(%v)", x)
	}
	if GetUint16(nil) != x {
		t.Errorf("GetUint16(%v)", nil)
	}

	x = 42
	if *ToUint16(x) != x {
		t.Errorf("*ToUint16(%v)", x)
	}
	if *ToUint16OrNil(x) != x {
		t.Errorf("*ToUint16OrNil(%v)", x)
	}
	if GetUint16(&x) != x {
		t.Errorf("GetUint16(%v)", &x)
	}
}

func TestUint32(t *testing.T) {
	var x uint32
	if *ToUint32(x) != x {
		t.Errorf("*ToUint32(%v)", x)
	}
	if ToUint32OrNil(x) != nil {
		t.Errorf("ToUint32OrNil(%v)", x)
	}
	if GetUint32(nil) != x {
		t.Errorf("GetUint32(%v)", nil)
	}

	x = 42
	if *ToUint32(x) != x {
		t.Errorf("*ToUint32(%v)", x)
	}
	if *ToUint32OrNil(x) != x {
		t.Errorf("*ToUint32OrNil(%v)", x)
	}
	if GetUint32(&x) != x {
		t.Errorf("GetUint32(%v)", &x)
	}
}

func TestUint64(t *testing.T) {
	var x uint64
	if *ToUint64(x) != x {
		t.Errorf("*ToUint64(%v)", x)
	}
	if ToUint64OrNil(x) != nil {
		t.Errorf("ToUint64OrNil(%v)", x)
	}
	if GetUint64(nil) != x {
		t.Errorf("GetUint64(%v)", nil)
	}

	x = 42
	if *ToUint64(x) != x {
		t.Errorf("*ToUint64(%v)", x)
	}
	if *ToUint64OrNil(x) != x {
		t.Errorf("*ToUint64OrNil(%v)", x)
	}
	if GetUint64(&x) != x {
		t.Errorf("GetUint64(%v)", &x)
	}
}

func TestUintptr(t *testing.T) {
	var x uintptr
	if *ToUintptr(x) != x {
		t.Errorf("*ToUintptr(%v)", x)
	}
	if ToUintptrOrNil(x) != nil {
		t.Errorf("ToUintptrOrNil(%v)", x)
	}
	if GetUintptr(nil) != x {
		t.Errorf("GetUintptr(%v)", nil)
	}

	x = 42
	if *ToUintptr(x) != x {
		t.Errorf("*ToUintptr(%v)", x)
	}
	if *ToUintptrOrNil(x) != x {
		t.Errorf("*ToUintptrOrNil(%v)", x)
	}
	if GetUintptr(&x) != x {
		t.Errorf("GetUintptr(%v)", &x)
	}
}

func TestDuration(t *testing.T) {
	var x time.Duration
	if *ToDuration(x) != x {
		t.Errorf("*ToDuration(%v)", x)
	}
	if ToDurationOrNil(x) != nil {
		t.Errorf("ToDurationOrNil(%v)", x)
	}
	if GetDuration(nil) != x {
		t.Errorf("GetDuration(%v)", nil)
	}

	x = time.Second
	if *ToDuration(x) != x {
		t.Errorf("*ToDuration(%v)", x)
	}
	if *ToDurationOrNil(x) != x {
		t.Errorf("*ToDurationOrNil(%v)", x)
	}
	if GetDuration(&x) != x {
		t.Errorf("GetDuration(%v)", &x)
	}
}

func TestTime(t *testing.T) {
	var x time.Time
	if *ToTime(x) != x {
		t.Errorf("*ToTime(%v)", x)
	}
	if ToTimeOrNil(x) != nil {
		t.Errorf("ToTimeOrNil(%v)", x)
	}
	if GetTime(nil) != x {
		t.Errorf("GetTime(%v)", nil)
	}

	x = time.Date(2014, 6, 25, 12, 24, 40, 0, time.UTC)
	if *ToTime(x) != x {
		t.Errorf("*ToTime(%v)", x)
	}
	if *ToTimeOrNil(x) != x {
		t.Errorf("*ToTimeOrNil(%v)", x)
	}
	if GetTime(&x) != x {
		t.Errorf("GetTime(%v)", &x)
	}
}

func Example() {
	const (
		defaultName = "some name"
	)

	// Stuff contains optional fields.
	type Stuff struct {
		Name    *string
		Comment *string
		Value   *int64
		Time    *time.Time
	}

	b, _ := json.Marshal(&Stuff{
		Name:    ToString(defaultName),                                   // can't say &defaultName
		Comment: ToString("not yet"),                                     // can't say &"not yet"
		Value:   ToInt64(42),                                             // can't say &42 or &int64(42)
		Time:    ToTime(time.Date(2014, 6, 25, 12, 24, 40, 0, time.UTC)), // can't say &time.Date(…)
	})

	fmt.Printf("%s", b)

	// Output: {"Name":"some name","Comment":"not yet","Value":42,"Time":"2014-06-25T12:24:40Z"}
}


================================================
FILE: value.go
================================================
package pointer

import (
	"time"
)

/*
Order as in spec:
	bool byte complex64 complex128 error float32 float64
	int int8 int16 int32 int64 rune string
	uint uint8 uint16 uint32 uint64 uintptr
	time.Duration time.Time
*/

// GetBool returns the value of the bool pointer passed in or false if the pointer is nil.
func GetBool(b *bool) bool {
	if b == nil {
		return false
	}
	return *b
}

// GetByte returns the value of the byte pointer passed in or 0 if the pointer is nil.
func GetByte(b *byte) byte {
	if b == nil {
		return 0
	}
	return *b
}

// GetComplex64 returns the value of the complex64 pointer passed in or 0 if the pointer is nil.
func GetComplex64(c *complex64) complex64 {
	if c == nil {
		return 0
	}
	return *c
}

// GetComplex128 returns the value of the complex128 pointer passed in or 0 if the pointer is nil.
func GetComplex128(c *complex128) complex128 {
	if c == nil {
		return 0
	}
	return *c
}

// GetError returns the value of the error pointer passed in or nil if the pointer is nil.
func GetError(e *error) error {
	if e == nil {
		return nil
	}
	return *e
}

// GetFloat32 returns the value of the float32 pointer passed in or 0 if the pointer is nil.
func GetFloat32(f *float32) float32 {
	if f == nil {
		return 0
	}
	return *f
}

// GetFloat64 returns the value of the float64 pointer passed in or 0 if the pointer is nil.
func GetFloat64(f *float64) float64 {
	if f == nil {
		return 0
	}
	return *f
}

// GetInt returns the value of the int pointer passed in or 0 if the pointer is nil.
func GetInt(i *int) int {
	if i == nil {
		return 0
	}
	return *i
}

// GetInt8 returns the value of the int8 pointer passed in or 0 if the pointer is nil.
func GetInt8(i *int8) int8 {
	if i == nil {
		return 0
	}
	return *i
}

// GetInt16 returns the value of the int16 pointer passed in or 0 if the pointer is nil.
func GetInt16(i *int16) int16 {
	if i == nil {
		return 0
	}
	return *i
}

// GetInt32 returns the value of the int32 pointer passed in or 0 if the pointer is nil.
func GetInt32(i *int32) int32 {
	if i == nil {
		return 0
	}
	return *i
}

// GetInt64 returns the value of the int64 pointer passed in or 0 if the pointer is nil.
func GetInt64(i *int64) int64 {
	if i == nil {
		return 0
	}
	return *i
}

// GetRune returns the value of the rune pointer passed in or 0 if the pointer is nil.
func GetRune(r *rune) rune {
	if r == nil {
		return 0
	}
	return *r
}

// GetString returns the value of the string pointer passed in or empty string if the pointer is nil.
func GetString(s *string) string {
	if s == nil {
		return ""
	}
	return *s
}

// GetUint returns the value of the uint pointer passed in or 0 if the pointer is nil.
func GetUint(u *uint) uint {
	if u == nil {
		return 0
	}
	return *u
}

// GetUint8 returns the value of the uint8 pointer passed in or 0 if the pointer is nil.
func GetUint8(u *uint8) uint8 {
	if u == nil {
		return 0
	}
	return *u
}

// GetUint16 returns the value of the uint16 pointer passed in or 0 if the pointer is nil.
func GetUint16(u *uint16) uint16 {
	if u == nil {
		return 0
	}
	return *u
}

// GetUint32 returns the value of the uint32 pointer passed in or 0 if the pointer is nil.
func GetUint32(u *uint32) uint32 {
	if u == nil {
		return 0
	}
	return *u
}

// GetUint64 returns the value of the uint64 pointer passed in or 0 if the pointer is nil.
func GetUint64(u *uint64) uint64 {
	if u == nil {
		return 0
	}
	return *u
}

// GetUintptr returns the value of the uintptr pointer passed in or 0 if the pointer is nil.
func GetUintptr(u *uintptr) uintptr {
	if u == nil {
		return 0
	}
	return *u
}

// GetDuration returns the value of the duration pointer passed in or 0 if the pointer is nil.
func GetDuration(d *time.Duration) time.Duration {
	if d == nil {
		return 0
	}
	return *d
}

// GetTime returns the value of the time pointer passed in or zero time.Time if the pointer is nil.
func GetTime(t *time.Time) time.Time {
	if t == nil {
		return time.Time{}
	}
	return *t
}
Download .txt
gitextract_87t1mn15/

├── .github/
│   └── workflows/
│       └── go.yml
├── LICENSE
├── README.md
├── generic.go
├── generic_test.go
├── go.mod
├── pointer.go
├── pointer_or_nil.go
├── pointer_test.go
└── value.go
Download .txt
SYMBOL INDEX (93 symbols across 6 files)

FILE: generic.go
  function To (line 7) | func To[T any](t T) *T {
  function ToOrNil (line 14) | func ToOrNil[T comparable](t T) *T {
  function Get (line 30) | func Get[T any](t *T) T {

FILE: generic_test.go
  function TestGeneric (line 11) | func TestGeneric(t *testing.T) {

FILE: pointer.go
  function ToBool (line 17) | func ToBool(b bool) *bool {
  function ToByte (line 22) | func ToByte(b byte) *byte {
  function ToComplex64 (line 27) | func ToComplex64(c complex64) *complex64 {
  function ToComplex128 (line 32) | func ToComplex128(c complex128) *complex128 {
  function ToError (line 37) | func ToError(e error) *error {
  function ToFloat32 (line 42) | func ToFloat32(f float32) *float32 {
  function ToFloat64 (line 47) | func ToFloat64(f float64) *float64 {
  function ToInt (line 52) | func ToInt(i int) *int {
  function ToInt8 (line 57) | func ToInt8(i int8) *int8 {
  function ToInt16 (line 62) | func ToInt16(i int16) *int16 {
  function ToInt32 (line 67) | func ToInt32(i int32) *int32 {
  function ToInt64 (line 72) | func ToInt64(i int64) *int64 {
  function ToRune (line 77) | func ToRune(r rune) *rune {
  function ToString (line 82) | func ToString(s string) *string {
  function ToUint (line 87) | func ToUint(u uint) *uint {
  function ToUint8 (line 92) | func ToUint8(u uint8) *uint8 {
  function ToUint16 (line 97) | func ToUint16(u uint16) *uint16 {
  function ToUint32 (line 102) | func ToUint32(u uint32) *uint32 {
  function ToUint64 (line 107) | func ToUint64(u uint64) *uint64 {
  function ToUintptr (line 112) | func ToUintptr(u uintptr) *uintptr {
  function ToDuration (line 117) | func ToDuration(d time.Duration) *time.Duration {
  function ToTime (line 122) | func ToTime(t time.Time) *time.Time {

FILE: pointer_or_nil.go
  function ToBoolOrNil (line 16) | func ToBoolOrNil(b bool) *bool {
  function ToByteOrNil (line 24) | func ToByteOrNil(b byte) *byte {
  function ToComplex64OrNil (line 32) | func ToComplex64OrNil(c complex64) *complex64 {
  function ToComplex128OrNil (line 40) | func ToComplex128OrNil(c complex128) *complex128 {
  function ToErrorOrNil (line 48) | func ToErrorOrNil(e error) *error {
  function ToFloat32OrNil (line 56) | func ToFloat32OrNil(f float32) *float32 {
  function ToFloat64OrNil (line 64) | func ToFloat64OrNil(f float64) *float64 {
  function ToIntOrNil (line 72) | func ToIntOrNil(i int) *int {
  function ToInt8OrNil (line 80) | func ToInt8OrNil(i int8) *int8 {
  function ToInt16OrNil (line 88) | func ToInt16OrNil(i int16) *int16 {
  function ToInt32OrNil (line 96) | func ToInt32OrNil(i int32) *int32 {
  function ToInt64OrNil (line 104) | func ToInt64OrNil(i int64) *int64 {
  function ToRuneOrNil (line 112) | func ToRuneOrNil(r rune) *rune {
  function ToStringOrNil (line 120) | func ToStringOrNil(s string) *string {
  function ToUintOrNil (line 128) | func ToUintOrNil(u uint) *uint {
  function ToUint8OrNil (line 136) | func ToUint8OrNil(u uint8) *uint8 {
  function ToUint16OrNil (line 144) | func ToUint16OrNil(u uint16) *uint16 {
  function ToUint32OrNil (line 152) | func ToUint32OrNil(u uint32) *uint32 {
  function ToUint64OrNil (line 160) | func ToUint64OrNil(u uint64) *uint64 {
  function ToUintptrOrNil (line 168) | func ToUintptrOrNil(u uintptr) *uintptr {
  function ToDurationOrNil (line 176) | func ToDurationOrNil(d time.Duration) *time.Duration {
  function ToTimeOrNil (line 184) | func ToTimeOrNil(t time.Time) *time.Time {

FILE: pointer_test.go
  function TestBool (line 19) | func TestBool(t *testing.T) {
  function TestByte (line 43) | func TestByte(t *testing.T) {
  function TestComplex64 (line 67) | func TestComplex64(t *testing.T) {
  function TestComplex128 (line 91) | func TestComplex128(t *testing.T) {
  function TestError (line 115) | func TestError(t *testing.T) {
  function TestFloat32 (line 139) | func TestFloat32(t *testing.T) {
  function TestFloat64 (line 163) | func TestFloat64(t *testing.T) {
  function TestInt (line 187) | func TestInt(t *testing.T) {
  function TestInt8 (line 211) | func TestInt8(t *testing.T) {
  function TestInt16 (line 235) | func TestInt16(t *testing.T) {
  function TestInt32 (line 259) | func TestInt32(t *testing.T) {
  function TestInt64 (line 283) | func TestInt64(t *testing.T) {
  function TestRune (line 307) | func TestRune(t *testing.T) {
  function TestString (line 331) | func TestString(t *testing.T) {
  function TestUint (line 355) | func TestUint(t *testing.T) {
  function TestUint8 (line 379) | func TestUint8(t *testing.T) {
  function TestUint16 (line 403) | func TestUint16(t *testing.T) {
  function TestUint32 (line 427) | func TestUint32(t *testing.T) {
  function TestUint64 (line 451) | func TestUint64(t *testing.T) {
  function TestUintptr (line 475) | func TestUintptr(t *testing.T) {
  function TestDuration (line 499) | func TestDuration(t *testing.T) {
  function TestTime (line 523) | func TestTime(t *testing.T) {
  function Example (line 547) | func Example() {

FILE: value.go
  function GetBool (line 16) | func GetBool(b *bool) bool {
  function GetByte (line 24) | func GetByte(b *byte) byte {
  function GetComplex64 (line 32) | func GetComplex64(c *complex64) complex64 {
  function GetComplex128 (line 40) | func GetComplex128(c *complex128) complex128 {
  function GetError (line 48) | func GetError(e *error) error {
  function GetFloat32 (line 56) | func GetFloat32(f *float32) float32 {
  function GetFloat64 (line 64) | func GetFloat64(f *float64) float64 {
  function GetInt (line 72) | func GetInt(i *int) int {
  function GetInt8 (line 80) | func GetInt8(i *int8) int8 {
  function GetInt16 (line 88) | func GetInt16(i *int16) int16 {
  function GetInt32 (line 96) | func GetInt32(i *int32) int32 {
  function GetInt64 (line 104) | func GetInt64(i *int64) int64 {
  function GetRune (line 112) | func GetRune(r *rune) rune {
  function GetString (line 120) | func GetString(s *string) string {
  function GetUint (line 128) | func GetUint(u *uint) uint {
  function GetUint8 (line 136) | func GetUint8(u *uint8) uint8 {
  function GetUint16 (line 144) | func GetUint16(u *uint16) uint16 {
  function GetUint32 (line 152) | func GetUint32(u *uint32) uint32 {
  function GetUint64 (line 160) | func GetUint64(u *uint64) uint64 {
  function GetUintptr (line 168) | func GetUintptr(u *uintptr) uintptr {
  function GetDuration (line 176) | func GetDuration(d *time.Duration) time.Duration {
  function GetTime (line 184) | func GetTime(t *time.Time) time.Time {
Condensed preview — 10 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (29K chars).
[
  {
    "path": ".github/workflows/go.yml",
    "chars": 688,
    "preview": "---\nname: Go\non:\n  push:\n    branches:\n      - main\n  pull_request:\n  schedule:\n    - cron: '42 3 * * 4'\n\njobs:\n  test:\n"
  },
  {
    "path": "LICENSE",
    "chars": 1086,
    "preview": "The MIT License (MIT)\n\nCopyright (c) 2015 Alexey Palazhchenko\n\nPermission is hereby granted, free of charge, to any pers"
  },
  {
    "path": "README.md",
    "chars": 1187,
    "preview": "# pointer\n\n[![Go Reference](https://pkg.go.dev/badge/github.com/AlekSi/pointer.svg)](https://pkg.go.dev/github.com/AlekS"
  },
  {
    "path": "generic.go",
    "chars": 742,
    "preview": "//go:build go1.18\n// +build go1.18\n\npackage pointer\n\n// To returns a pointer to the passed value.\nfunc To[T any](t T) *T"
  },
  {
    "path": "generic_test.go",
    "chars": 507,
    "preview": "//go:build go1.18\n// +build go1.18\n\npackage pointer\n\nimport (\n\t\"testing\"\n\t\"time\"\n)\n\nfunc TestGeneric(t *testing.T) {\n\tva"
  },
  {
    "path": "go.mod",
    "chars": 42,
    "preview": "module github.com/AlekSi/pointer\n\ngo 1.18\n"
  },
  {
    "path": "pointer.go",
    "chars": 2704,
    "preview": "// Package pointer provides helpers to convert between pointers and values of built-in (and, with generics, of any) type"
  },
  {
    "path": "pointer_or_nil.go",
    "chars": 4340,
    "preview": "package pointer\n\nimport (\n\t\"time\"\n)\n\n/*\nOrder as in spec:\n\tbool byte complex64 complex128 error float32 float64\n\tint int"
  },
  {
    "path": "pointer_test.go",
    "chars": 10470,
    "preview": "package pointer\n\nimport (\n\t\"encoding/json\"\n\t\"errors\"\n\t\"fmt\"\n\t\"testing\"\n\t\"time\"\n)\n\n/*\nOrder as in spec:\n\tbool byte comple"
  },
  {
    "path": "value.go",
    "chars": 3966,
    "preview": "package pointer\n\nimport (\n\t\"time\"\n)\n\n/*\nOrder as in spec:\n\tbool byte complex64 complex128 error float32 float64\n\tint int"
  }
]

About this extraction

This page contains the full source code of the AlekSi/pointer GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 10 files (25.1 KB), approximately 8.5k tokens, and a symbol index with 93 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.

Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.

Copied to clipboard!