Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #148 from duck8823/rearchitecture
Browse files Browse the repository at this point in the history
RE: ARCHITECTURE
  • Loading branch information
duck8823 authored Jan 5, 2019
2 parents 3b54e0a + c116414 commit 97d6737
Show file tree
Hide file tree
Showing 163 changed files with 10,153 additions and 5,900 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ vendor
# app
/config.yml
release-notes.txt
duci
/duci
duci.exe
dist
29 changes: 0 additions & 29 deletions application/cmd/health.go

This file was deleted.

5 changes: 5 additions & 0 deletions application/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ func (s maskString) MarshalJSON() ([]byte, error) {
return []byte(`"***"`), nil
}

// ToString returns unmasked string.
func (s maskString) String() string {
return string(s)
}

// Configuration of application.
type Configuration struct {
Server *Server `yaml:"server" json:"server"`
Expand Down
18 changes: 17 additions & 1 deletion application/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestConfiguration_Set(t *testing.T) {
expected := "hello world"

// and
os.Setenv("TEST_CONF_ENV", expected)
_ = os.Setenv("TEST_CONF_ENV", expected)

//
err := application.Config.Set("testdata/config_with_env.yml")
Expand Down Expand Up @@ -131,3 +131,19 @@ func TestMaskString_MarshalJSON(t *testing.T) {
t.Errorf("wont masked string, but got '%s'", actual)
}
}

func TestMaskString_String(t *testing.T) {
// given
want := "hoge"

// and
sut := application.MaskString(want)

// when
got := sut.String()

// then
if !cmp.Equal(got, want) {
t.Errorf("must be equal, but %+v", cmp.Diff(got, want))
}
}
37 changes: 37 additions & 0 deletions application/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package application

import (
"context"
"fmt"
"github.com/duck8823/duci/domain/model/job"
"github.com/duck8823/duci/domain/model/job/target/github"
"net/url"
)

var ctxKey = "duci_job"

// BuildJob represents once of job
type BuildJob struct {
ID job.ID
TargetSource *github.TargetSource
TaskName string
TargetURL *url.URL
}

// ContextWithJob set parent context BuildJob and returns it.
func ContextWithJob(parent context.Context, job *BuildJob) context.Context {
return context.WithValue(parent, &ctxKey, job)
}

// BuildJobFromContext extract BuildJob from context
func BuildJobFromContext(ctx context.Context) (*BuildJob, error) {
val := ctx.Value(&ctxKey)
if val == nil {
return nil, fmt.Errorf("context value '%s' should not be null", ctxKey)
}
buildJob, ok := val.(*BuildJob)
if !ok {
return nil, fmt.Errorf("invalid type in context '%s'", ctxKey)
}
return buildJob, nil
}
59 changes: 0 additions & 59 deletions application/context/context.go

This file was deleted.

58 changes: 0 additions & 58 deletions application/context/context_test.go

This file was deleted.

70 changes: 70 additions & 0 deletions application/context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package application_test

import (
"context"
"github.com/duck8823/duci/application"
"github.com/duck8823/duci/domain/model/job"
"github.com/google/go-cmp/cmp"
"github.com/google/uuid"
"testing"
)

func TestContextWithJob(t *testing.T) {
// given
want := &application.BuildJob{
ID: job.ID(uuid.New()),
}

// and
ctx := application.ContextWithJob(context.Background(), want)

// when
got := ctx.Value(application.GetCtxKey())

// then
if !cmp.Equal(got, want) {
t.Errorf("must be equal, but %+v", cmp.Diff(got, want))
}
}

func TestBuildJobFromContext(t *testing.T) {
t.Run("with value", func(t *testing.T) {
// given
want := &application.BuildJob{
ID: job.ID(uuid.New()),
}

sut := context.WithValue(context.Background(), application.GetCtxKey(), want)

// when
got, err := application.BuildJobFromContext(sut)

// then
if err != nil {
t.Errorf("error must be nil, but got %+v", err)
}

// and
if !cmp.Equal(got, want) {
t.Errorf("must be equal, but %+v", cmp.Diff(got, want))
}
})

t.Run("without value", func(t *testing.T) {
sut := context.Background()

// when
got, err := application.BuildJobFromContext(sut)

// then
if err == nil {
t.Error("error must not be nil")
}

// and
if got != nil {
t.Errorf("must be nil, but got %+v", got)
}
})

}
Loading

0 comments on commit 97d6737

Please sign in to comment.