Skip to content

Commit

Permalink
feat: added first version
Browse files Browse the repository at this point in the history
  • Loading branch information
Axel Etcheverry committed Oct 28, 2019
0 parents commit 7a2468d
Show file tree
Hide file tree
Showing 13 changed files with 462 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
root = true

[*]
charset = utf-8
end_of_line = LF
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = space
indent_size = 4

[Makefile]
indent_style = tab

[*.md]
trim_trailing_whitespace = false

[*.go]
indent_style = tab

[*.yml]
indent_size = 2

[*.yaml]
indent_size = 2

[*.yml.dist]
indent_size = 2
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.DS_Store
*.out

build/

*.swp
13 changes: 13 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
run:
concurrency: 4
deadline: 3m
issues-exit-code: 1
tests: false
skip-files:
- ".*_mock\\.go"
- "mock_.*\\.go"

output:
format: colored-line-number
print-issued-lines: true
print-linter-name: true
28 changes: 28 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
language: go

sudo: required
dist: xenial

env:
- GO111MODULE=on

go:
- 1.13.x
- 1.12.x
- master
- tip

matrix:
fast_finish: true
allow_failures:
- go: master
- go: tip

install:
- go get github.com/mattn/goveralls

script:
- make test

after_success:
- goveralls -coverprofile=coverage.out -service=travis-ci
19 changes: 19 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2019 Axel Etcheverry

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.
68 changes: 68 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
BUILD=build

.PHONY: release
release:
@echo "Release v$(version)"
@git pull
@git checkout master
@git pull
@git checkout develop
@git flow release start $(version)
@git flow release finish $(version) -p -m "Release v$(version)"
@git checkout develop
@echo "Release v$(version) finished."

.PHONY: all
all: coverage.out

coverage.out: $(shell find . -type f -print | grep -v vendor | grep "\.go")
@go test -race -cover -covermode=atomic -coverprofile ./coverage.out.tmp ./...
@cat ./coverage.out.tmp | grep -v '.pb.go' | grep -v 'mock_' > ./coverage.out
@rm ./coverage.out.tmp

.PHONY: test
test: coverage.out

.PHONY: cover
cover: coverage.out
@echo ""
@go tool cover -func ./coverage.out

.PHONY: cover-html
cover-html: coverage.out
@go tool cover -html=./coverage.out

.PHONY: benchmark
benchmark:
@go test -bench=. ./...

.PHONY: clean
clean:
@rm ./coverage.out
@go clean -i ./...

.PHONY: generate
generate:
@CGO_ENABLED=0 go generate ./...

.PHONY: lint
lint:
@CGO_ENABLED=0 golangci-lint run ./...


docs:
@mkdir -p /tmp/tmpgoroot/doc
@rm -rf /tmp/tmpgopath/src/github.com/euskadi31/go-eventemitter
@mkdir -p /tmp/tmpgopath/src/github.com/euskadi31/go-eventemitter
@tar -c --exclude='.git' --exclude='tmp' . | tar -x -C /tmp/tmpgopath/src/github.com/euskadi31/go-eventemitter
@echo -e "open http://localhost:6060/pkg/github.com/euskadi31/go-eventemitter\n"
@GOROOT=/tmp/tmpgoroot/ GOPATH=/tmp/tmpgopath/ godoc -http=localhost:6060

${BUILD}/demo-app: $(shell find . -type f -print | grep -v vendor | grep "\.go")
@echo "Building demo-app..."
@go generate ./cmd/demo-app/
@go build -o $@ ./cmd/demo-app/

run-demo-app: ${BUILD}/demo-app
@echo "Running demo-app..."
@./$<
84 changes: 84 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
Go Future [![Last release](https://img.shields.io/github/release/euskadi31/go-future.svg)](https://github.com/euskadi31/go-future/releases/latest) [![Documentation](https://godoc.org/github.com/euskadi31/go-future?status.svg)](https://godoc.org/github.com/euskadi31/go-future)
================

[![Go Report Card](https://goreportcard.com/badge/github.com/euskadi31/go-future)](https://goreportcard.com/report/github.com/euskadi31/go-future)

| Branch | Status | Coverage |
|---------|--------|----------|
| master | [![Build Status](https://img.shields.io/travis/euskadi31/go-future/master.svg)](https://travis-ci.org/euskadi31/go-future) | [![Coveralls](https://img.shields.io/coveralls/euskadi31/go-future/master.svg)](https://coveralls.io/github/euskadi31/go-future?branch=master) |

go-future is an implementation of future in Go.

Example
-------

```go
package main

import (
"fmt"

"github.com/euskadi31/go-future"
)

func asyncFunc() *future.Future {
f := future.New()

go func(f *future.Future) {
f.Value("my async value")
}(f)

return f
}

func main() {
f := asyncFunc();

v, err := f.Get()
if err != nil {
panic(err)
}

fmt.Println(v)

}
```

With `Fill`


```go
package main

import (
"fmt"

"github.com/euskadi31/go-future"
)

func asyncFunc() *future.Future {
f := future.New()

go func(f *future.Future) {
f.Value("my async value")
}(f)

return f
}

func main() {
f := asyncFunc();

var v string
if err := f.Fill(&v); err != nil {
panic(err)
}

fmt.Println(v)
}
```

License
-------

go-future is licensed under [the MIT license](LICENSE.md).
15 changes: 15 additions & 0 deletions default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2019 Axel Etcheverry. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.

package future

// Error return future error
func Error(err error) *Future {
return New().Error(err)
}

// Value return future value
func Value(value interface{}) *Future {
return New().Value(value)
}
28 changes: 28 additions & 0 deletions default_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2019 Axel Etcheverry. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.

package future

import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
)

func TestError(t *testing.T) {
f := Error(errors.New("fail"))

_, err := f.Get()
assert.Error(t, err)

}

func TestValue(t *testing.T) {
f := Value("foo")

v, err := f.Get()
assert.NoError(t, err)
assert.Equal(t, "foo", v)
}
91 changes: 91 additions & 0 deletions future.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright 2019 Axel Etcheverry. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.

package future

import (
"fmt"
"reflect"
)

// Future struct
type Future struct {
err error
value chan interface{}
closed bool
}

// New Future
func New() *Future {
return &Future{
value: make(chan interface{}, 1),
}
}

// Error set error to Future
func (f *Future) Error(err error) *Future {
f.err = err

f.Close()

return f
}

// Value set value to Future
func (f *Future) Value(value interface{}) *Future {
if !f.closed {
f.value <- value
}

return f
}

// Get result or error
func (f *Future) Get() (interface{}, error) {
value := <-f.value

f.Close()

if f.err != nil {
return nil, f.err
}

return value, nil
}

// Close future
func (f *Future) Close() {
if !f.closed {
f.closed = true

close(f.value)
}
}

// Fill dest var
func (f *Future) Fill(dest interface{}) error {
value := <-f.value

f.Close()

if f.err != nil {
return f.err
}

return fill(value, dest)
}

func fill(src, dest interface{}) (err error) {
defer func() {
if r := recover(); r != nil {
d := reflect.TypeOf(dest)
s := reflect.TypeOf(src)
err = fmt.Errorf("the fill destination should be a pointer to a `%s`, but you used a `%s`", s, d)
}
}()

reflect.ValueOf(dest).Elem().Set(reflect.ValueOf(src))

return err
}
Loading

0 comments on commit 7a2468d

Please sign in to comment.