From 96dd3cded9e443ac801d557b4e18670e6c1a372f Mon Sep 17 00:00:00 2001 From: Mark Phelps Date: Tue, 13 Nov 2018 20:25:38 -0500 Subject: [PATCH 1/2] Adding must that panics if value doesnt exit --- CONTRIBUTING.md | 12 ++++++------ Makefile | 24 ++++++++++++++---------- bool.go | 10 +++++++++- byte.go | 10 +++++++++- cmd/optional/main.go | 8 ++++++++ cmd/optional/testdata/optional_bar.go | 10 +++++++++- cmd/optional/testdata/optional_foo.go | 10 +++++++++- cmd/optional/testdata/string.go | 10 +++++++++- complex128.go | 10 +++++++++- complex64.go | 10 +++++++++- error.go | 10 +++++++++- float32.go | 10 +++++++++- float64.go | 10 +++++++++- int.go | 10 +++++++++- int16.go | 10 +++++++++- int32.go | 10 +++++++++- int64.go | 10 +++++++++- int8.go | 10 +++++++++- rune.go | 10 +++++++++- string.go | 10 +++++++++- uint.go | 10 +++++++++- uint16.go | 10 +++++++++- uint32.go | 10 +++++++++- uint64.go | 10 +++++++++- uint8.go | 10 +++++++++- uintptr.go | 10 +++++++++- 26 files changed, 235 insertions(+), 39 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 9c3b3a2..b48e3ae 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -2,19 +2,19 @@ ## Setup your machine -`example` is written in [Go](https://golang.org/). +`optional` is written in [Go](https://golang.org/). Prerequisites are: * Build: * `make` - * [Go 1.8+](http://golang.org/doc/install) + * [Go 1.10+](http://golang.org/doc/install) -Clone `example` from source into `$GOPATH`: +Clone `optional` from source into `$GOPATH`: ```sh -$ go get github.com/markphelps/example -$ cd $GOPATH/src/github.com/markphelps/example +$ go get github.com/markphelps/optional +$ cd $GOPATH/src/github.com/markphelps/optional ``` Install the build and lint dependencies: @@ -47,5 +47,5 @@ Which runs all the linters and tests. ## Submit a pull request -Push your branch to your `example` fork and open a pull request against the +Push your branch to your `optional` fork and open a pull request against the master branch. diff --git a/Makefile b/Makefile index 762ab69..08b30fe 100644 --- a/Makefile +++ b/Makefile @@ -1,26 +1,30 @@ GOTOOLS = \ github.com/golang/dep/cmd/dep \ github.com/golangci/golangci-lint/cmd/golangci-lint \ - golang.org/x/tools/cmd/cover \ + +SOURCE_FILES?=./... +TEST_PATTERN?=. +TEST_OPTIONS?= .PHONY: setup setup: ## Install all the build and lint dependencies @echo "--> Installing tools" - @go get -u $(GOTOOLS) + go get -u $(GOTOOLS) .PHONY: dep dep: ## Install all import dependencies @echo "--> Installing dependencies" - @dep ensure + dep ensure .PHONY: test test: ## Run all the tests @echo "--> Running tests" - @echo 'mode: atomic' > coverage.txt && go list ./... | grep -v /vendor/ | xargs -n1 -I{} sh -c 'go test -covermode=atomic -coverprofile=coverage.txt -v -race -timeout=30s {}' + go test $(TEST_OPTIONS) -failfast $(SOURCE_FILES) -run $(TEST_PATTERN) -timeout=1m -.PHONY: cover -cover: test ## Run all the tests and opens the coverage report - go tool cover -html=coverage.txt +.PHONY: golden +golden: ## Updates goldenfile + @echo "--> Updating goldenfiles" + go test ./cmd/optional/... -update .PHONY: fmt fmt: ## gofmt and goimports all go files @@ -30,7 +34,7 @@ fmt: ## gofmt and goimports all go files .PHONY: lint lint: ## Run all the linters @echo "--> Running linters" - @golangci-lint run + golangci-lint run .PHONY: ci ci: lint test ## Run all the tests and code checks @@ -38,12 +42,12 @@ ci: lint test ## Run all the tests and code checks .PHONY: generate generate: ## Run go generate @echo "--> Running go generate" - @go generate + go generate .PHONY: build build: ## Build @echo "--> Building ..." - @go build -o bin/optional ./cmd/optional/main.go + go build -o bin/optional ./cmd/optional/main.go # Absolutely awesome: http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html .PHONY: help diff --git a/bool.go b/bool.go index b0d0432..9832981 100644 --- a/bool.go +++ b/bool.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:52:25.812586 +0000 UTC +// This file was generated by robots at 2018-11-14 01:19:19.473472 +0000 UTC package optional @@ -32,6 +32,14 @@ func (b Bool) Get() (bool, error) { return *b.value, nil } +// Must returns the bool value or panics if not present. +func (b Bool) Must() bool { + if !b.Present() { + panic("value not present") + } + return *b.value +} + // Present returns whether or not the value is present. func (b Bool) Present() bool { return b.value != nil diff --git a/byte.go b/byte.go index d73e356..c8930f1 100644 --- a/byte.go +++ b/byte.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:52:26.274563 +0000 UTC +// This file was generated by robots at 2018-11-14 01:19:19.87742 +0000 UTC package optional @@ -32,6 +32,14 @@ func (b Byte) Get() (byte, error) { return *b.value, nil } +// Must returns the byte value or panics if not present. +func (b Byte) Must() byte { + if !b.Present() { + panic("value not present") + } + return *b.value +} + // Present returns whether or not the value is present. func (b Byte) Present() bool { return b.value != nil diff --git a/cmd/optional/main.go b/cmd/optional/main.go index 54b9dbe..a880b21 100644 --- a/cmd/optional/main.go +++ b/cmd/optional/main.go @@ -159,6 +159,14 @@ func ({{ .VariableName }} {{ .OutputName }}) Get() ({{ .TypeName }}, error) { return *{{ .VariableName }}.value, nil } +// Must returns the {{ .TypeName }} value or panics if not present. +func ({{ .VariableName }} {{ .OutputName }}) Must() {{ .TypeName }} { + if !{{ .VariableName }}.Present() { + panic("value not present") + } + return *{{ .VariableName }}.value +} + // Present returns whether or not the value is present. func ({{ .VariableName }} {{ .OutputName }}) Present() bool { return {{ .VariableName }}.value != nil diff --git a/cmd/optional/testdata/optional_bar.go b/cmd/optional/testdata/optional_bar.go index 20ff0c8..69cd9e1 100644 --- a/cmd/optional/testdata/optional_bar.go +++ b/cmd/optional/testdata/optional_bar.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:54:14.621363 +0000 UTC +// This file was generated by robots at 2018-11-14 01:24:01.194773 +0000 UTC package bar @@ -32,6 +32,14 @@ func (o optionalBar) Get() (bar, error) { return *o.value, nil } +// Must returns the bar value or panics if not present. +func (o optionalBar) Must() bar { + if !o.Present() { + panic("value not present") + } + return *o.value +} + // Present returns whether or not the value is present. func (o optionalBar) Present() bool { return o.value != nil diff --git a/cmd/optional/testdata/optional_foo.go b/cmd/optional/testdata/optional_foo.go index 667a74e..4370292 100644 --- a/cmd/optional/testdata/optional_foo.go +++ b/cmd/optional/testdata/optional_foo.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:54:14.61916 +0000 UTC +// This file was generated by robots at 2018-11-14 01:24:01.191566 +0000 UTC package foo @@ -32,6 +32,14 @@ func (o OptionalFoo) Get() (Foo, error) { return *o.value, nil } +// Must returns the Foo value or panics if not present. +func (o OptionalFoo) Must() Foo { + if !o.Present() { + panic("value not present") + } + return *o.value +} + // Present returns whether or not the value is present. func (o OptionalFoo) Present() bool { return o.value != nil diff --git a/cmd/optional/testdata/string.go b/cmd/optional/testdata/string.go index 44e2117..9175d44 100644 --- a/cmd/optional/testdata/string.go +++ b/cmd/optional/testdata/string.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:54:14.615858 +0000 UTC +// This file was generated by robots at 2018-11-14 01:24:01.189493 +0000 UTC package optional @@ -32,6 +32,14 @@ func (s String) Get() (string, error) { return *s.value, nil } +// Must returns the string value or panics if not present. +func (s String) Must() string { + if !s.Present() { + panic("value not present") + } + return *s.value +} + // Present returns whether or not the value is present. func (s String) Present() bool { return s.value != nil diff --git a/complex128.go b/complex128.go index e6954c3..ad456c9 100644 --- a/complex128.go +++ b/complex128.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:52:26.694747 +0000 UTC +// This file was generated by robots at 2018-11-14 01:19:20.322675 +0000 UTC package optional @@ -32,6 +32,14 @@ func (c Complex128) Get() (complex128, error) { return *c.value, nil } +// Must returns the complex128 value or panics if not present. +func (c Complex128) Must() complex128 { + if !c.Present() { + panic("value not present") + } + return *c.value +} + // Present returns whether or not the value is present. func (c Complex128) Present() bool { return c.value != nil diff --git a/complex64.go b/complex64.go index 4e011eb..31fc328 100644 --- a/complex64.go +++ b/complex64.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:52:27.082956 +0000 UTC +// This file was generated by robots at 2018-11-14 01:19:20.738084 +0000 UTC package optional @@ -32,6 +32,14 @@ func (c Complex64) Get() (complex64, error) { return *c.value, nil } +// Must returns the complex64 value or panics if not present. +func (c Complex64) Must() complex64 { + if !c.Present() { + panic("value not present") + } + return *c.value +} + // Present returns whether or not the value is present. func (c Complex64) Present() bool { return c.value != nil diff --git a/error.go b/error.go index 41d3b7a..f40d834 100644 --- a/error.go +++ b/error.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:52:27.535329 +0000 UTC +// This file was generated by robots at 2018-11-14 01:19:21.143852 +0000 UTC package optional @@ -32,6 +32,14 @@ func (e Error) Get() (error, error) { return *e.value, nil } +// Must returns the error value or panics if not present. +func (e Error) Must() error { + if !e.Present() { + panic("value not present") + } + return *e.value +} + // Present returns whether or not the value is present. func (e Error) Present() bool { return e.value != nil diff --git a/float32.go b/float32.go index eb78e2e..df96c7b 100644 --- a/float32.go +++ b/float32.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:52:27.939662 +0000 UTC +// This file was generated by robots at 2018-11-14 01:19:21.58314 +0000 UTC package optional @@ -32,6 +32,14 @@ func (f Float32) Get() (float32, error) { return *f.value, nil } +// Must returns the float32 value or panics if not present. +func (f Float32) Must() float32 { + if !f.Present() { + panic("value not present") + } + return *f.value +} + // Present returns whether or not the value is present. func (f Float32) Present() bool { return f.value != nil diff --git a/float64.go b/float64.go index 1125530..485f1a6 100644 --- a/float64.go +++ b/float64.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:52:28.345245 +0000 UTC +// This file was generated by robots at 2018-11-14 01:19:22.00215 +0000 UTC package optional @@ -32,6 +32,14 @@ func (f Float64) Get() (float64, error) { return *f.value, nil } +// Must returns the float64 value or panics if not present. +func (f Float64) Must() float64 { + if !f.Present() { + panic("value not present") + } + return *f.value +} + // Present returns whether or not the value is present. func (f Float64) Present() bool { return f.value != nil diff --git a/int.go b/int.go index f675e32..08d7336 100644 --- a/int.go +++ b/int.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:52:28.766753 +0000 UTC +// This file was generated by robots at 2018-11-14 01:19:22.40707 +0000 UTC package optional @@ -32,6 +32,14 @@ func (i Int) Get() (int, error) { return *i.value, nil } +// Must returns the int value or panics if not present. +func (i Int) Must() int { + if !i.Present() { + panic("value not present") + } + return *i.value +} + // Present returns whether or not the value is present. func (i Int) Present() bool { return i.value != nil diff --git a/int16.go b/int16.go index a6da247..91577d7 100644 --- a/int16.go +++ b/int16.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:52:29.175694 +0000 UTC +// This file was generated by robots at 2018-11-14 01:19:22.845542 +0000 UTC package optional @@ -32,6 +32,14 @@ func (i Int16) Get() (int16, error) { return *i.value, nil } +// Must returns the int16 value or panics if not present. +func (i Int16) Must() int16 { + if !i.Present() { + panic("value not present") + } + return *i.value +} + // Present returns whether or not the value is present. func (i Int16) Present() bool { return i.value != nil diff --git a/int32.go b/int32.go index f759790..8add3bd 100644 --- a/int32.go +++ b/int32.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:52:29.586464 +0000 UTC +// This file was generated by robots at 2018-11-14 01:19:23.271884 +0000 UTC package optional @@ -32,6 +32,14 @@ func (i Int32) Get() (int32, error) { return *i.value, nil } +// Must returns the int32 value or panics if not present. +func (i Int32) Must() int32 { + if !i.Present() { + panic("value not present") + } + return *i.value +} + // Present returns whether or not the value is present. func (i Int32) Present() bool { return i.value != nil diff --git a/int64.go b/int64.go index 0089cc3..c947bc3 100644 --- a/int64.go +++ b/int64.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:52:30.027992 +0000 UTC +// This file was generated by robots at 2018-11-14 01:19:23.691505 +0000 UTC package optional @@ -32,6 +32,14 @@ func (i Int64) Get() (int64, error) { return *i.value, nil } +// Must returns the int64 value or panics if not present. +func (i Int64) Must() int64 { + if !i.Present() { + panic("value not present") + } + return *i.value +} + // Present returns whether or not the value is present. func (i Int64) Present() bool { return i.value != nil diff --git a/int8.go b/int8.go index 4cdb349..fd219cf 100644 --- a/int8.go +++ b/int8.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:52:30.427813 +0000 UTC +// This file was generated by robots at 2018-11-14 01:19:24.127552 +0000 UTC package optional @@ -32,6 +32,14 @@ func (i Int8) Get() (int8, error) { return *i.value, nil } +// Must returns the int8 value or panics if not present. +func (i Int8) Must() int8 { + if !i.Present() { + panic("value not present") + } + return *i.value +} + // Present returns whether or not the value is present. func (i Int8) Present() bool { return i.value != nil diff --git a/rune.go b/rune.go index a68653a..3c6128a 100644 --- a/rune.go +++ b/rune.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:52:30.867604 +0000 UTC +// This file was generated by robots at 2018-11-14 01:19:24.541611 +0000 UTC package optional @@ -32,6 +32,14 @@ func (r Rune) Get() (rune, error) { return *r.value, nil } +// Must returns the rune value or panics if not present. +func (r Rune) Must() rune { + if !r.Present() { + panic("value not present") + } + return *r.value +} + // Present returns whether or not the value is present. func (r Rune) Present() bool { return r.value != nil diff --git a/string.go b/string.go index a346334..acc0e4a 100644 --- a/string.go +++ b/string.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:52:31.29776 +0000 UTC +// This file was generated by robots at 2018-11-14 01:19:24.977209 +0000 UTC package optional @@ -32,6 +32,14 @@ func (s String) Get() (string, error) { return *s.value, nil } +// Must returns the string value or panics if not present. +func (s String) Must() string { + if !s.Present() { + panic("value not present") + } + return *s.value +} + // Present returns whether or not the value is present. func (s String) Present() bool { return s.value != nil diff --git a/uint.go b/uint.go index c9e610e..519a859 100644 --- a/uint.go +++ b/uint.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:52:31.699785 +0000 UTC +// This file was generated by robots at 2018-11-14 01:19:25.409219 +0000 UTC package optional @@ -32,6 +32,14 @@ func (u Uint) Get() (uint, error) { return *u.value, nil } +// Must returns the uint value or panics if not present. +func (u Uint) Must() uint { + if !u.Present() { + panic("value not present") + } + return *u.value +} + // Present returns whether or not the value is present. func (u Uint) Present() bool { return u.value != nil diff --git a/uint16.go b/uint16.go index d7c7591..6c9c2d2 100644 --- a/uint16.go +++ b/uint16.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:52:32.12056 +0000 UTC +// This file was generated by robots at 2018-11-14 01:19:25.968716 +0000 UTC package optional @@ -32,6 +32,14 @@ func (u Uint16) Get() (uint16, error) { return *u.value, nil } +// Must returns the uint16 value or panics if not present. +func (u Uint16) Must() uint16 { + if !u.Present() { + panic("value not present") + } + return *u.value +} + // Present returns whether or not the value is present. func (u Uint16) Present() bool { return u.value != nil diff --git a/uint32.go b/uint32.go index 125b416..85c5de2 100644 --- a/uint32.go +++ b/uint32.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:52:32.56791 +0000 UTC +// This file was generated by robots at 2018-11-14 01:19:26.46976 +0000 UTC package optional @@ -32,6 +32,14 @@ func (u Uint32) Get() (uint32, error) { return *u.value, nil } +// Must returns the uint32 value or panics if not present. +func (u Uint32) Must() uint32 { + if !u.Present() { + panic("value not present") + } + return *u.value +} + // Present returns whether or not the value is present. func (u Uint32) Present() bool { return u.value != nil diff --git a/uint64.go b/uint64.go index 1252c06..91f51be 100644 --- a/uint64.go +++ b/uint64.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:52:32.98085 +0000 UTC +// This file was generated by robots at 2018-11-14 01:19:26.87282 +0000 UTC package optional @@ -32,6 +32,14 @@ func (u Uint64) Get() (uint64, error) { return *u.value, nil } +// Must returns the uint64 value or panics if not present. +func (u Uint64) Must() uint64 { + if !u.Present() { + panic("value not present") + } + return *u.value +} + // Present returns whether or not the value is present. func (u Uint64) Present() bool { return u.value != nil diff --git a/uint8.go b/uint8.go index 920cb84..9f48f04 100644 --- a/uint8.go +++ b/uint8.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:52:33.373985 +0000 UTC +// This file was generated by robots at 2018-11-14 01:19:27.292368 +0000 UTC package optional @@ -32,6 +32,14 @@ func (u Uint8) Get() (uint8, error) { return *u.value, nil } +// Must returns the uint8 value or panics if not present. +func (u Uint8) Must() uint8 { + if !u.Present() { + panic("value not present") + } + return *u.value +} + // Present returns whether or not the value is present. func (u Uint8) Present() bool { return u.value != nil diff --git a/uintptr.go b/uintptr.go index b947661..455067f 100644 --- a/uintptr.go +++ b/uintptr.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:52:33.811844 +0000 UTC +// This file was generated by robots at 2018-11-14 01:19:27.716273 +0000 UTC package optional @@ -32,6 +32,14 @@ func (u Uintptr) Get() (uintptr, error) { return *u.value, nil } +// Must returns the uintptr value or panics if not present. +func (u Uintptr) Must() uintptr { + if !u.Present() { + panic("value not present") + } + return *u.value +} + // Present returns whether or not the value is present. func (u Uintptr) Present() bool { return u.value != nil From 4793d22d8df204d4ba27869f944f61b5d34ea946 Mon Sep 17 00:00:00 2001 From: Mark Phelps Date: Tue, 13 Nov 2018 20:39:30 -0500 Subject: [PATCH 2/2] Golden comment --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 08b30fe..879fd62 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,7 @@ test: ## Run all the tests go test $(TEST_OPTIONS) -failfast $(SOURCE_FILES) -run $(TEST_PATTERN) -timeout=1m .PHONY: golden -golden: ## Updates goldenfile +golden: ## Updates goldenfiles and runs tests @echo "--> Updating goldenfiles" go test ./cmd/optional/... -update