pax_global_header00006660000000000000000000000064143300275410014511gustar00rootroot0000000000000052 comment=5c8fb1b0c6db07105bc9594163fb56e0b16dbf41 pointer-1.2.2/000077500000000000000000000000001433002754100131735ustar00rootroot00000000000000pointer-1.2.2/.editorconfig000066400000000000000000000003431433002754100156500ustar00rootroot00000000000000[*] charset = utf-8 end_of_line = lf insert_final_newline = true trim_trailing_whitespace = true indent_style = space indent_size = 4 [*.go] indent_style = tab [{*.yaml, *.yml}] indent_size = 2 [Makefile] indent_style = tab pointer-1.2.2/.github/000077500000000000000000000000001433002754100145335ustar00rootroot00000000000000pointer-1.2.2/.github/workflows/000077500000000000000000000000001433002754100165705ustar00rootroot00000000000000pointer-1.2.2/.github/workflows/go.yml000066400000000000000000000021221433002754100177150ustar00rootroot00000000000000name: Go on: push: branches: [ main ] pull_request: branches: [ main ] schedule: - cron: '17 17 * * 5' workflow_dispatch: { } jobs: basic: name: 'Basic health checks' runs-on: "${{ matrix.os }}" strategy: matrix: os: [ ubuntu-latest ] go: - '1.11' - '1.12' - '1.13' - '1.14' - '1.15' - '1.16' - '1.17' - '1.18' - '1.19' steps: - uses: actions/checkout@v3 - name: "Set up Go:${{ matrix.go }}" uses: actions/setup-go@v3 with: go-version: '${{ matrix.go }}' - name: Make build run: make build - name: Make test run: make test advanced: name: 'Full check code quality' runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: "Set up Go" uses: actions/setup-go@v3 with: go-version: '1.19' - name: Make check run: make check - uses: codecov/codecov-action@v2 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.out verbose: true pointer-1.2.2/.gitignore000066400000000000000000000000161433002754100151600ustar00rootroot00000000000000/coverage.out pointer-1.2.2/LICENSE000066400000000000000000000030101433002754100141720ustar00rootroot00000000000000BSD 3-Clause License Copyright © 2019-2020,2022 Vasiliy Vasilyuk All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. pointer-1.2.2/Makefile000066400000000000000000000031461433002754100146370ustar00rootroot00000000000000# Based on https://git.io/fjkGc # The full path to the main package is use in the # imports tool to format imports correctly. NAMESPACE = github.com/xorcare/pointer # The name of the file recommended in the standard # documentation go test -cover and used codecov.io # to check code coverage. COVER_FILE ?= coverage.out # Main targets. .DEFAULT_GOAL := help .PHONY: build build: ## Build the project binary @go build ./... .PHONY: ci ci: check ## Target for integration with ci pipeline .PHONY: check check: static test build ## Check project with static checks and unit tests .PHONY: help help: ## Print this help @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | \ awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' .PHONY: imports imports: tools ## Check and fix import section by import rules @test -z $$(for d in $$(go list -f {{.Dir}} ./...); do goimports -e -l -local $(NAMESPACE) -w $$d/*.go; done) .PHONY: lint lint: tools ## Check the project with lint @golint -set_exit_status ./... .PHONY: static static: imports vet lint ## Run static checks (lint, imports, vet, ...) all over the project .PHONY: test test: ## Run unit tests @go test ./... -count=1 -race @go test ./... -count=1 -coverprofile=$(COVER_FILE) -covermode=atomic $d @go tool cover -func=$(COVER_FILE) | grep ^total .PHONY: tools tools: ## Install all needed tools, e.g. for static checks @go install golang.org/x/lint/golint@v0.0.0-20210508222113-6edffad5e616 @go install golang.org/x/tools/cmd/goimports@v0.0.0-20200130002326-2f3ba24bd6e7 .PHONY: vet vet: ## Check the project with vet @go vet ./... pointer-1.2.2/README.md000066400000000000000000000112141433002754100144510ustar00rootroot00000000000000# pointer [![Go workflow status badge](https://github.com/xorcare/pointer/actions/workflows/go.yml/badge.svg?branch=main)](https://github.com/xorcare/pointer/actions/workflows/go.yml) [![codecov](https://codecov.io/gh/xorcare/pointer/badge.svg?branch=main)](https://codecov.io/gh/xorcare/pointer) [![Go Report Card](https://goreportcard.com/badge/github.com/xorcare/pointer)](https://goreportcard.com/report/github.com/xorcare/pointer) [![GoDoc](https://godoc.org/github.com/xorcare/pointer?status.svg)](https://pkg.go.dev/github.com/xorcare/pointer) Package pointer contains helper routines for simplifying the creation of optional fields of basic type. ## A little copying is better than a little dependency With the advent of generics in go 1.18, using this library in your code is likely to be unwarranted, I recommend that you make a small local copy of this library, for example: Put the code below in the file: `internal/pointer/pointer.go` and use it at your own pleasure. ```go package pointer // Of is a helper routine that allocates a new any value // to store v and returns a pointer to it. func Of[Value any](v Value) *Value { return &v } ``` ## Installation ```bash go get github.com/xorcare/pointer ``` ## Generics Starting with Go 18+, you can use the `Of` method to get pointers to values of any types. ## Examples Examples of using the library are presented on [godoc.org][GDE] and in the [source library code][SCE]. ## FAQ | Question | Source | |-----------------------------------------------------------------------|------------------------------------------------------| | How to set **bool** pointer in a struct literal or variable? | `var _ *bool = pointer.Bool(true)` | | How to set **byte** pointer in a struct literal or variable? | `var _ *byte = pointer.Byte(1)` | | How to set **complex64** pointer in a struct literal or variable? | `var _ *complex64 = pointer.Complex64(1.1)` | | How to set **complex128** pointer in a struct literal or variable? | `var _ *complex128 = pointer.Complex128(1.1)` | | How to set **float32** pointer in a struct literal or variable? | `var _ *float32 = pointer.Float32(1.1)` | | How to set **float64** pointer in a struct literal or variable? | `var _ *float64 = pointer.Float64(1.1)` | | How to set **int** pointer in a struct literal or variable? | `var _ *int = pointer.Int(1)` | | How to set **int8** pointer in a struct literal or variable? | `var _ *int8 = pointer.Int8(8)` | | How to set **int16** pointer in a struct literal or variable? | `var _ *int16 = pointer.Int16(16)` | | How to set **int32** pointer in a struct literal or variable? | `var _ *int32 = pointer.Int32(32)` | | How to set **int64** pointer in a struct literal or variable? | `var _ *int64 = pointer.Int64(64)` | | How to set **rune** pointer in a struct literal or variable? | `var _ *rune = pointer.Rune(1)` | | How to set **string** pointer in a struct literal or variable? | `var _ *string = pointer.String("ptr")` | | How to set **uint** pointer in a struct literal or variable? | `var _ *uint = pointer.Uint(1)` | | How to set **uint8** pointer in a struct literal or variable? | `var _ *uint8 = pointer.Uint8(8)` | | How to set **uint16** pointer in a struct literal or variable? | `var _ *uint16 = pointer.Uint16(16)` | | How to set **uint32** pointer in a struct literal or variable? | `var _ *uint32 = pointer.Uint32(32)` | | How to set **uint64** pointer in a struct literal or variable? | `var _ *uint64 = pointer.Uint64(64)` | | How to set **time.Time** pointer in a struct literal or variable? | `var _ *time.Time = pointer.Time(time.Now())` | | How to set **time.Duration** pointer in a struct literal or variable? | `var _ *time.Duration = pointer.Duration(time.Hour)` | ## License © 2019-2020,2022 Vasiliy Vasilyuk Released under the [BSD 3-Clause License]. [BSD 3-Clause License]: https://github.com/xorcare/pointer/blob/main/LICENSE 'BSD 3-Clause "New" or "Revised" License' [GDE]: https://godoc.org/github.com/xorcare/pointer#pkg-examples 'Examples of using package pointer' [SCE]: https://github.com/xorcare/pointer/blob/main/example_test.go 'Source code examples of using package pointer' pointer-1.2.2/example_118_test.go000066400000000000000000000010641433002754100166060ustar00rootroot00000000000000// Copyright © 2022 Vasiliy Vasilyuk. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. //go:build go1.18 // +build go1.18 package pointer_test import ( "github.com/xorcare/pointer" ) func ExampleOf() { // Examples for values that have default type casting. var _ *int = pointer.Of(0) var _ *bool = pointer.Of(true) var _ *string = pointer.Of("example") // Example of usage with non default type casting. var _ *int8 = pointer.Of(int8(0)) var _ *int8 = pointer.Of[int8](0) } pointer-1.2.2/example_test.go000066400000000000000000000315501433002754100162200ustar00rootroot00000000000000// Copyright © 2019-2020 Vasiliy Vasilyuk. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package pointer_test import ( "encoding/json" "fmt" "os" "time" "github.com/xorcare/pointer" ) func ExampleAny() { type example struct { Bool bool BoolPtr *bool Uint uint UintPtr *uint String string StringPtr *string } encoder := json.NewEncoder(os.Stdout) fmt.Print("Example use universal function: ") _ = encoder.Encode(example{ Bool: true, BoolPtr: pointer.Any(true).(*bool), Uint: 32, UintPtr: pointer.Any(uint(64)).(*uint), String: "xor", StringPtr: pointer.Any("care").(*string), }) fmt.Print("Example use specific functions: ") _ = encoder.Encode(example{ Bool: true, BoolPtr: pointer.Bool(true), Uint: 32, UintPtr: pointer.Uint(64), String: "xor", StringPtr: pointer.String("care"), }) fmt.Print("Example of using a non-standard hack: ") _ = encoder.Encode(example{ Bool: true, BoolPtr: &[]bool{true}[0], Uint: 32, UintPtr: &[]uint{64}[0], String: "xor", StringPtr: &[]string{"care"}[0], }) // Output: // Example use universal function: {"Bool":true,"BoolPtr":true,"Uint":32,"UintPtr":64,"String":"xor","StringPtr":"care"} // Example use specific functions: {"Bool":true,"BoolPtr":true,"Uint":32,"UintPtr":64,"String":"xor","StringPtr":"care"} // Example of using a non-standard hack: {"Bool":true,"BoolPtr":true,"Uint":32,"UintPtr":64,"String":"xor","StringPtr":"care"} } func ExampleBool() { s := struct{ Pointer *bool }{} fmt.Println(fmt.Sprintf("Zero pointer value: %T %v", s.Pointer, s.Pointer)) s.Pointer = pointer.Bool(true) fmt.Println(fmt.Sprintf("The value set by the library: %T %v", s.Pointer, *s.Pointer)) s.Pointer = &[]bool{true}[0] fmt.Println(fmt.Sprintf("The value set in elegant way: %T %v", s.Pointer, *s.Pointer)) // Output: // Zero pointer value: *bool // The value set by the library: *bool true // The value set in elegant way: *bool true } func ExampleByte() { s := struct{ Pointer *byte }{} fmt.Println(fmt.Sprintf("Zero pointer value: %T %v", s.Pointer, s.Pointer)) s.Pointer = pointer.Byte(127) fmt.Println(fmt.Sprintf("The value set by the library: %T %v", s.Pointer, *s.Pointer)) s.Pointer = &[]byte{255}[0] fmt.Println(fmt.Sprintf("The value set in elegant way: %T %v", s.Pointer, *s.Pointer)) // Output: // Zero pointer value: *uint8 // The value set by the library: *uint8 127 // The value set in elegant way: *uint8 255 } func ExampleComplex64() { s := struct{ Pointer *complex64 }{} fmt.Println(fmt.Sprintf("Zero pointer value: %T %v", s.Pointer, s.Pointer)) s.Pointer = pointer.Complex64(complex64(55.751878)) fmt.Println(fmt.Sprintf("The value set by the library: %T %v", s.Pointer, *s.Pointer)) s.Pointer = &[]complex64{complex64(48.752467)}[0] fmt.Println(fmt.Sprintf("The value set in elegant way: %T %v", s.Pointer, *s.Pointer)) // Output: // Zero pointer value: *complex64 // The value set by the library: *complex64 (55.751877+0i) // The value set in elegant way: *complex64 (48.75247+0i) } func ExampleComplex128() { s := struct{ Pointer *complex128 }{} fmt.Println(fmt.Sprintf("Zero pointer value: %T %v", s.Pointer, s.Pointer)) s.Pointer = pointer.Complex128(complex128(55.753184)) fmt.Println(fmt.Sprintf("The value set by the library: %T %v", s.Pointer, *s.Pointer)) s.Pointer = &[]complex128{complex128(48.743702)}[0] fmt.Println(fmt.Sprintf("The value set in elegant way: %T %v", s.Pointer, *s.Pointer)) // Output: // Zero pointer value: *complex128 // The value set by the library: *complex128 (55.753184+0i) // The value set in elegant way: *complex128 (48.743702+0i) } func ExampleFloat32() { s := struct{ Pointer *float32 }{} fmt.Println(fmt.Sprintf("Zero pointer value: %T %v", s.Pointer, s.Pointer)) s.Pointer = pointer.Float32(float32(55.753184)) fmt.Println(fmt.Sprintf("The value set by the library: %T %v", s.Pointer, *s.Pointer)) s.Pointer = &[]float32{float32(48.743702)}[0] fmt.Println(fmt.Sprintf("The value set in elegant way: %T %v", s.Pointer, *s.Pointer)) // Output: // Zero pointer value: *float32 // The value set by the library: *float32 55.753185 // The value set in elegant way: *float32 48.743702 } func ExampleFloat64() { s := struct{ Pointer *float64 }{} fmt.Println(fmt.Sprintf("Zero pointer value: %T %v", s.Pointer, s.Pointer)) s.Pointer = pointer.Float64(float64(55.753184)) fmt.Println(fmt.Sprintf("The value set by the library: %T %v", s.Pointer, *s.Pointer)) s.Pointer = &[]float64{float64(48.743702)}[0] fmt.Println(fmt.Sprintf("The value set in elegant way: %T %v", s.Pointer, *s.Pointer)) // Output: // Zero pointer value: *float64 // The value set by the library: *float64 55.753184 // The value set in elegant way: *float64 48.743702 } func ExampleInt() { s := struct{ Pointer *int }{} fmt.Println(fmt.Sprintf("Zero pointer value: %T %v", s.Pointer, s.Pointer)) s.Pointer = pointer.Int(1) fmt.Println(fmt.Sprintf("The value set by the library: %T %v", s.Pointer, *s.Pointer)) s.Pointer = &[]int{2}[0] fmt.Println(fmt.Sprintf("The value set in elegant way: %T %v", s.Pointer, *s.Pointer)) // Output: // Zero pointer value: *int // The value set by the library: *int 1 // The value set in elegant way: *int 2 } func ExampleInt8() { s := struct{ Pointer *int8 }{} fmt.Println(fmt.Sprintf("Zero pointer value: %T %v", s.Pointer, s.Pointer)) s.Pointer = pointer.Int8(63) fmt.Println(fmt.Sprintf("The value set by the library: %T %v", s.Pointer, *s.Pointer)) s.Pointer = &[]int8{127}[0] fmt.Println(fmt.Sprintf("The value set in elegant way: %T %v", s.Pointer, *s.Pointer)) // Output: // Zero pointer value: *int8 // The value set by the library: *int8 63 // The value set in elegant way: *int8 127 } func ExampleInt16() { s := struct{ Pointer *int16 }{} fmt.Println(fmt.Sprintf("Zero pointer value: %T %v", s.Pointer, s.Pointer)) s.Pointer = pointer.Int16(16383) fmt.Println(fmt.Sprintf("The value set by the library: %T %v", s.Pointer, *s.Pointer)) s.Pointer = &[]int16{32767}[0] fmt.Println(fmt.Sprintf("The value set in elegant way: %T %v", s.Pointer, *s.Pointer)) // Output: // Zero pointer value: *int16 // The value set by the library: *int16 16383 // The value set in elegant way: *int16 32767 } func ExampleInt32() { s := struct{ Pointer *int32 }{} fmt.Println(fmt.Sprintf("Zero pointer value: %T %v", s.Pointer, s.Pointer)) s.Pointer = pointer.Int32(1073741823) fmt.Println(fmt.Sprintf("The value set by the library: %T %v", s.Pointer, *s.Pointer)) s.Pointer = &[]int32{2147483647}[0] fmt.Println(fmt.Sprintf("The value set in elegant way: %T %v", s.Pointer, *s.Pointer)) // Output: // Zero pointer value: *int32 // The value set by the library: *int32 1073741823 // The value set in elegant way: *int32 2147483647 } func ExampleInt64() { s := struct{ Pointer *int64 }{} fmt.Println(fmt.Sprintf("Zero pointer value: %T %v", s.Pointer, s.Pointer)) s.Pointer = pointer.Int64(4611686018427387903) fmt.Println(fmt.Sprintf("The value set by the library: %T %v", s.Pointer, *s.Pointer)) s.Pointer = &[]int64{9223372036854775807}[0] fmt.Println(fmt.Sprintf("The value set in elegant way: %T %v", s.Pointer, *s.Pointer)) // Output: // Zero pointer value: *int64 // The value set by the library: *int64 4611686018427387903 // The value set in elegant way: *int64 9223372036854775807 } func ExampleRune() { s := struct{ Pointer *rune }{} fmt.Println(fmt.Sprintf("Zero pointer value: %T %v", s.Pointer, s.Pointer)) s.Pointer = pointer.Rune(120) fmt.Println(fmt.Sprintf("The value set by the library: %T %3[2]d %[2]q", s.Pointer, *s.Pointer)) s.Pointer = &[]rune{99}[0] fmt.Println(fmt.Sprintf("The value set in elegant way: %T %3[2]d %[2]q", s.Pointer, *s.Pointer)) // Output: // Zero pointer value: *int32 // The value set by the library: *int32 120 'x' // The value set in elegant way: *int32 99 'c' } func ExampleString() { s := struct{ Pointer *string }{} fmt.Println(fmt.Sprintf("Zero pointer value: %T %v", s.Pointer, s.Pointer)) s.Pointer = pointer.String("xor") fmt.Println(fmt.Sprintf("The value set by the library: %T %v", s.Pointer, *s.Pointer)) s.Pointer = &[]string{"care"}[0] fmt.Println(fmt.Sprintf("The value set in elegant way: %T %v", s.Pointer, *s.Pointer)) // Output: // Zero pointer value: *string // The value set by the library: *string xor // The value set in elegant way: *string care } func ExampleUint() { s := struct{ Pointer *uint }{} fmt.Println(fmt.Sprintf("Zero pointer value: %T %v", s.Pointer, s.Pointer)) s.Pointer = pointer.Uint(1) fmt.Println(fmt.Sprintf("The value set by the library: %T %v", s.Pointer, *s.Pointer)) s.Pointer = &[]uint{2}[0] fmt.Println(fmt.Sprintf("The value set in elegant way: %T %v", s.Pointer, *s.Pointer)) // Output: // Zero pointer value: *uint // The value set by the library: *uint 1 // The value set in elegant way: *uint 2 } func ExampleUint8() { s := struct{ Pointer *uint8 }{} fmt.Println(fmt.Sprintf("Zero pointer value: %T %v", s.Pointer, s.Pointer)) s.Pointer = pointer.Uint8(63) fmt.Println(fmt.Sprintf("The value set by the library: %T %v", s.Pointer, *s.Pointer)) s.Pointer = &[]uint8{127}[0] fmt.Println(fmt.Sprintf("The value set in elegant way: %T %v", s.Pointer, *s.Pointer)) // Output: // Zero pointer value: *uint8 // The value set by the library: *uint8 63 // The value set in elegant way: *uint8 127 } func ExampleUint16() { s := struct{ Pointer *uint16 }{} fmt.Println(fmt.Sprintf("Zero pointer value: %T %v", s.Pointer, s.Pointer)) s.Pointer = pointer.Uint16(16383) fmt.Println(fmt.Sprintf("The value set by the library: %T %v", s.Pointer, *s.Pointer)) s.Pointer = &[]uint16{32767}[0] fmt.Println(fmt.Sprintf("The value set in elegant way: %T %v", s.Pointer, *s.Pointer)) // Output: // Zero pointer value: *uint16 // The value set by the library: *uint16 16383 // The value set in elegant way: *uint16 32767 } func ExampleUint32() { s := struct{ Pointer *uint32 }{} fmt.Println(fmt.Sprintf("Zero pointer value: %T %v", s.Pointer, s.Pointer)) s.Pointer = pointer.Uint32(1073741823) fmt.Println(fmt.Sprintf("The value set by the library: %T %v", s.Pointer, *s.Pointer)) s.Pointer = &[]uint32{2147483647}[0] fmt.Println(fmt.Sprintf("The value set in elegant way: %T %v", s.Pointer, *s.Pointer)) // Output: // Zero pointer value: *uint32 // The value set by the library: *uint32 1073741823 // The value set in elegant way: *uint32 2147483647 } func ExampleUint64() { s := struct{ Pointer *uint64 }{} fmt.Println(fmt.Sprintf("Zero pointer value: %T %v", s.Pointer, s.Pointer)) s.Pointer = pointer.Uint64(4611686018427387903) fmt.Println(fmt.Sprintf("The value set by the library: %T %v", s.Pointer, *s.Pointer)) s.Pointer = &[]uint64{9223372036854775807}[0] fmt.Println(fmt.Sprintf("The value set in elegant way: %T %v", s.Pointer, *s.Pointer)) // Output: // Zero pointer value: *uint64 // The value set by the library: *uint64 4611686018427387903 // The value set in elegant way: *uint64 9223372036854775807 } func ExampleUintptr() { s := struct{ Pointer *uintptr }{} fmt.Println(fmt.Sprintf("Zero pointer value: %T %v", s.Pointer, s.Pointer)) s.Pointer = pointer.Uintptr(4611686018427387903) fmt.Println(fmt.Sprintf("The value set by the library: %T %v", s.Pointer, *s.Pointer)) s.Pointer = &[]uintptr{9223372036854775807}[0] fmt.Println(fmt.Sprintf("The value set in elegant way: %T %v", s.Pointer, *s.Pointer)) // Output: // Zero pointer value: *uintptr // The value set by the library: *uintptr 4611686018427387903 // The value set in elegant way: *uintptr 9223372036854775807 } func ExampleTime() { s := struct{ Pointer *time.Time }{} fmt.Println(fmt.Sprintf("Zero pointer value: %T %v", s.Pointer, s.Pointer)) s.Pointer = pointer.Time(time.Time{}.Add(time.Minute)) fmt.Println(fmt.Sprintf("The value set by the library: %T %v", s.Pointer, *s.Pointer)) s.Pointer = &[]time.Time{time.Time{}.Add(time.Hour)}[0] fmt.Println(fmt.Sprintf("The value set in elegant way: %T %v", s.Pointer, *s.Pointer)) // Output: // Zero pointer value: *time.Time // The value set by the library: *time.Time 0001-01-01 00:01:00 +0000 UTC // The value set in elegant way: *time.Time 0001-01-01 01:00:00 +0000 UTC } func ExampleDuration() { s := struct{ Pointer *time.Duration }{} fmt.Println(fmt.Sprintf("Zero pointer value: %T %v", s.Pointer, s.Pointer)) s.Pointer = pointer.Duration(time.Hour) fmt.Println(fmt.Sprintf("The value set by the library: %T %v", s.Pointer, *s.Pointer)) s.Pointer = &[]time.Duration{time.Minute}[0] fmt.Println(fmt.Sprintf("The value set in elegant way: %T %v", s.Pointer, *s.Pointer)) // Output: // Zero pointer value: *time.Duration // The value set by the library: *time.Duration 1h0m0s // The value set in elegant way: *time.Duration 1m0s } pointer-1.2.2/go.mod000066400000000000000000000000531433002754100142770ustar00rootroot00000000000000module github.com/xorcare/pointer go 1.18 pointer-1.2.2/go.sum000066400000000000000000000000001433002754100143140ustar00rootroot00000000000000pointer-1.2.2/pointer.go000066400000000000000000000113411433002754100152020ustar00rootroot00000000000000// Copyright © 2019-2020 Vasiliy Vasilyuk. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Package pointer contains helper routines for simplifying the creation // of optional fields of basic type. package pointer import ( "reflect" "time" ) // Any is a helper routine that allocates a new interface value // to store v and returns a pointer to it. // // // Usage: var _ *Type = pointer.Any(Type(value) | value).(*Type) // // var _ *bool = pointer.Any(true).(*bool) // var _ *byte = pointer.Any(byte(1)).(*byte) // var _ *complex64 = pointer.Any(complex64(1.1)).(*complex64) // var _ *complex128 = pointer.Any(complex128(1.1)).(*complex128) // var _ *float32 = pointer.Any(float32(1.1)).(*float32) // var _ *float64 = pointer.Any(float64(1.1)).(*float64) // var _ *int = pointer.Any(int(1)).(*int) // var _ *int8 = pointer.Any(int8(8)).(*int8) // var _ *int16 = pointer.Any(int16(16)).(*int16) // var _ *int32 = pointer.Any(int32(32)).(*int32) // var _ *int64 = pointer.Any(int64(64)).(*int64) // var _ *rune = pointer.Any(rune(1)).(*rune) // var _ *string = pointer.Any("ptr").(*string) // var _ *uint = pointer.Any(uint(1)).(*uint) // var _ *uint8 = pointer.Any(uint8(8)).(*uint8) // var _ *uint16 = pointer.Any(uint16(16)).(*uint16) // var _ *uint32 = pointer.Any(uint32(32)).(*uint32) // var _ *uint64 = pointer.Any(uint64(64)).(*uint64) // var _ *uintptr = pointer.Any(uintptr(64)).(*uintptr) func Any(v interface{}) interface{} { r := reflect.New(reflect.TypeOf(v)) reflect.ValueOf(r.Interface()).Elem().Set(reflect.ValueOf(v)) return r.Interface() } // Bool is a helper routine that allocates a new bool value // to store v and returns a pointer to it. func Bool(v bool) *bool { return &v } // Byte is a helper routine that allocates a new byte value // to store v and returns a pointer to it. func Byte(v byte) *byte { return &v } // Complex64 is a helper routine that allocates a new complex64 value // to store v and returns a pointer to it. func Complex64(v complex64) *complex64 { return &v } // Complex128 is a helper routine that allocates a new complex128 value // to store v and returns a pointer to it. func Complex128(v complex128) *complex128 { return &v } // Float32 is a helper routine that allocates a new float32 value // to store v and returns a pointer to it. func Float32(v float32) *float32 { return &v } // Float64 is a helper routine that allocates a new float64 value // to store v and returns a pointer to it. func Float64(v float64) *float64 { return &v } // Int is a helper routine that allocates a new int value // to store v and returns a pointer to it. func Int(v int) *int { return &v } // Int8 is a helper routine that allocates a new int8 value // to store v and returns a pointer to it. func Int8(v int8) *int8 { return &v } // Int16 is a helper routine that allocates a new int16 value // to store v and returns a pointer to it. func Int16(v int16) *int16 { return &v } // Int32 is a helper routine that allocates a new int32 value // to store v and returns a pointer to it. func Int32(v int32) *int32 { return &v } // Int64 is a helper routine that allocates a new int64 value // to store v and returns a pointer to it. func Int64(v int64) *int64 { return &v } // Rune is a helper routine that allocates a new rune value // to store v and returns a pointer to it. func Rune(v rune) *rune { return &v } // String is a helper routine that allocates a new string value // to store v and returns a pointer to it. func String(v string) *string { return &v } // Uint is a helper routine that allocates a new uint value // to store v and returns a pointer to it. func Uint(v uint) *uint { return &v } // Uint8 is a helper routine that allocates a new uint8 value // to store v and returns a pointer to it. func Uint8(v uint8) *uint8 { return &v } // Uint16 is a helper routine that allocates a new uint16 value // to store v and returns a pointer to it. func Uint16(v uint16) *uint16 { return &v } // Uint32 is a helper routine that allocates a new uint32 value // to store v and returns a pointer to it. func Uint32(v uint32) *uint32 { return &v } // Uint64 is a helper routine that allocates a new uint64 value // to store v and returns a pointer to it. func Uint64(v uint64) *uint64 { return &v } // Uintptr is a helper routine that allocates a new uintptr value // to store v and returns a pointer to it. func Uintptr(v uintptr) *uintptr { return &v } // Time is a helper routine that allocates a new time.Time value // to store v and returns a pointer to it. func Time(v time.Time) *time.Time { return &v } // Duration is a helper routine that allocates a new time.Duration value // to store v and returns a pointer to it. func Duration(v time.Duration) *time.Duration { return &v } pointer-1.2.2/pointer_118.go000066400000000000000000000005601433002754100155740ustar00rootroot00000000000000// Copyright © 2022 Vasiliy Vasilyuk. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. //go:build go1.18 // +build go1.18 package pointer // Of is a helper routine that allocates a new any value // to store v and returns a pointer to it. func Of[Value any](v Value) *Value { return &v } pointer-1.2.2/pointer_118_test.go000066400000000000000000000023031433002754100166300ustar00rootroot00000000000000// Copyright © 2022 Vasiliy Vasilyuk. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. //go:build go1.18 // +build go1.18 package pointer import ( "os" "testing" "time" ) func TestOf(t *testing.T) { equal(t, true, *Of(true)) equal(t, false, *Of(false)) equal(t, byte(1), *Of(byte(1))) equal(t, []byte{1}, *Of([]byte{1})) equal(t, complex64(1), *Of(complex64(1))) equal(t, complex128(1), *Of(complex128(1))) equal(t, float32(1.1), *Of(float32(1.1))) equal(t, float64(1.1), *Of(float64(1.1))) equal(t, int(1), *Of(int(1))) equal(t, int8(1), *Of(int8(1))) equal(t, int16(1), *Of(int16(1))) equal(t, int32(1), *Of(int32(1))) equal(t, int64(1), *Of(int64(1))) equal(t, rune(1), *Of(rune(1))) equal(t, string("pointer"), *Of(string("pointer"))) equal(t, uint(1), *Of(uint(1))) equal(t, uint8(1), *Of(uint8(1))) equal(t, uint16(1), *Of(uint16(1))) equal(t, uint32(1), *Of(uint32(1))) equal(t, uint64(1), *Of(uint64(1))) equal(t, uintptr(1), *Of(uintptr(1))) equal(t, time.Time{}.Add(time.Hour), *Of(time.Time{}.Add(time.Hour))) equal(t, 127*time.Hour, *Of(127 * time.Hour)) equal(t, os.Interrupt, *Of(os.Interrupt)) } pointer-1.2.2/pointer_test.go000066400000000000000000000114411433002754100162420ustar00rootroot00000000000000// Copyright © 2019-2020 Vasiliy Vasilyuk. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package pointer import ( "fmt" "reflect" "testing" "time" ) func equal(t testing.TB, expected, actual interface{}) { if h, ok := t.(interface{ Helper() }); ok { h.Helper() } t.Logf("expected: %q, actual: %q", fmt.Sprint(expected), fmt.Sprint(actual)) if !reflect.DeepEqual(expected, actual) { t.Fatalf("values obtained are not equal, expected: %v, actual: %v", expected, actual) } } func TestAny(t *testing.T) { var _ *bool = Any(true).(*bool) var _ *byte = Any(byte(1)).(*byte) var _ *complex64 = Any(complex64(1.1)).(*complex64) var _ *complex128 = Any(complex128(1.1)).(*complex128) var _ *float32 = Any(float32(1.1)).(*float32) var _ *float64 = Any(float64(1.1)).(*float64) var _ *int = Any(int(1)).(*int) var _ *int8 = Any(int8(8)).(*int8) var _ *int16 = Any(int16(16)).(*int16) var _ *int32 = Any(int32(32)).(*int32) var _ *int64 = Any(int64(64)).(*int64) var _ *rune = Any(rune(1)).(*rune) var _ *string = Any("ptr").(*string) var _ *uint = Any(uint(1)).(*uint) var _ *uint8 = Any(uint8(8)).(*uint8) var _ *uint16 = Any(uint16(16)).(*uint16) var _ *uint32 = Any(uint32(32)).(*uint32) var _ *uint64 = Any(uint64(64)).(*uint64) var _ *uintptr = Any(uintptr(64)).(*uintptr) equal(t, true, *Any(true).(*bool)) equal(t, false, *Any(false).(*bool)) equal(t, true, *(*Any(Bool(true)).(**bool))) equal(t, int64(1), *Any(int64(1)).(*int64)) equal(t, uint64(1), *Any(uint64(1)).(*uint64)) equal(t, struct{}{}, *Any(struct{}{}).(*struct{})) equal(t, string("pointer"), *Any(string("pointer")).(*string)) equal(t, reflect.Method{}, *Any(reflect.Method{}).(*reflect.Method)) equal(t, reflect.Method{Name: "name"}, *Any(reflect.Method{Name: "name"}).(*reflect.Method)) } func TestBool(t *testing.T) { equal(t, true, *Bool(true)) equal(t, false, *Bool(false)) } func TestByte(t *testing.T) { equal(t, byte(0), *Byte(byte(0))) equal(t, byte(1), *Byte(byte(1))) } func TestComplex64(t *testing.T) { equal(t, complex64(0), *Complex64(complex64(0))) equal(t, complex64(1), *Complex64(complex64(1))) } func TestComplex128(t *testing.T) { equal(t, complex128(0), *Complex128(complex128(0))) equal(t, complex128(1), *Complex128(complex128(1))) } func TestFloat32(t *testing.T) { equal(t, float32(-1.1), *Float32(float32(-1.1))) equal(t, float32(-1), *Float32(float32(-1))) equal(t, float32(0.0), *Float32(float32(0.0))) equal(t, float32(1), *Float32(float32(1))) equal(t, float32(1.1), *Float32(float32(1.1))) } func TestFloat64(t *testing.T) { equal(t, float64(-1.1), *Float64(float64(-1.1))) equal(t, float64(-1), *Float64(float64(-1))) equal(t, float64(0.0), *Float64(float64(0.0))) equal(t, float64(1), *Float64(float64(1))) equal(t, float64(1.1), *Float64(float64(1.1))) } func TestInt(t *testing.T) { equal(t, int(-1), *Int(int(-1))) equal(t, int(0), *Int(int(0))) equal(t, int(1), *Int(int(1))) } func TestInt8(t *testing.T) { equal(t, int8(-1), *Int8(int8(-1))) equal(t, int8(0), *Int8(int8(0))) equal(t, int8(1), *Int8(int8(1))) } func TestInt16(t *testing.T) { equal(t, int16(-1), *Int16(int16(-1))) equal(t, int16(0), *Int16(int16(0))) equal(t, int16(1), *Int16(int16(1))) } func TestInt32(t *testing.T) { equal(t, int32(-1), *Int32(int32(-1))) equal(t, int32(0), *Int32(int32(0))) equal(t, int32(1), *Int32(int32(1))) } func TestInt64(t *testing.T) { equal(t, int64(-1), *Int64(int64(-1))) equal(t, int64(0), *Int64(int64(0))) equal(t, int64(1), *Int64(int64(1))) } func TestRune(t *testing.T) { equal(t, rune(0), *Rune(rune(0))) equal(t, rune(1), *Rune(rune(1))) } func TestString(t *testing.T) { equal(t, string(""), *String(string(""))) equal(t, string("pointer"), *String(string("pointer"))) } func TestUint(t *testing.T) { equal(t, uint(0), *Uint(uint(0))) equal(t, uint(1), *Uint(uint(1))) } func TestUint8(t *testing.T) { equal(t, uint8(0), *Uint8(uint8(0))) equal(t, uint8(1), *Uint8(uint8(1))) } func TestUint16(t *testing.T) { equal(t, uint16(0), *Uint16(uint16(0))) equal(t, uint16(1), *Uint16(uint16(1))) } func TestUint32(t *testing.T) { equal(t, uint32(0), *Uint32(uint32(0))) equal(t, uint32(1), *Uint32(uint32(1))) } func TestUint64(t *testing.T) { equal(t, uint64(0), *Uint64(uint64(0))) equal(t, uint64(1), *Uint64(uint64(1))) } func TestUintptr(t *testing.T) { equal(t, uintptr(0), *Uintptr(uintptr(0))) equal(t, uintptr(1), *Uintptr(uintptr(1))) } func TestTime(t *testing.T) { equal(t, time.Time{}, *Time(time.Time{})) equal(t, time.Time{}.Add(time.Hour), *Time(time.Time{}.Add(time.Hour))) } func TestDuration(t *testing.T) { equal(t, -1*time.Hour, *Duration(-1 * time.Hour)) equal(t, time.Duration(0), *Duration(time.Duration(0))) equal(t, 127*time.Hour, *Duration(127 * time.Hour)) }