This repository has been archived by the owner on Aug 16, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor gql * refactor context * rename
- Loading branch information
Showing
31 changed files
with
372 additions
and
1,110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package adapter | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/reearth/reearth-backend/internal/usecase" | ||
"github.com/reearth/reearth-backend/internal/usecase/interfaces" | ||
"github.com/reearth/reearth-backend/pkg/user" | ||
) | ||
|
||
type ContextKey string | ||
|
||
const ( | ||
contextUser ContextKey = "user" | ||
contextOperator ContextKey = "operator" | ||
contextSub ContextKey = "sub" | ||
contextUsecases ContextKey = "usecases" | ||
) | ||
|
||
func AttachUser(ctx context.Context, u *user.User) context.Context { | ||
return context.WithValue(ctx, contextUser, u) | ||
} | ||
|
||
func AttachOperator(ctx context.Context, o *usecase.Operator) context.Context { | ||
return context.WithValue(ctx, contextOperator, o) | ||
} | ||
|
||
func AttachSub(ctx context.Context, sub string) context.Context { | ||
return context.WithValue(ctx, contextSub, sub) | ||
} | ||
|
||
func AttachUsecases(ctx context.Context, u *interfaces.Container) context.Context { | ||
ctx = context.WithValue(ctx, contextUsecases, u) | ||
return ctx | ||
} | ||
|
||
func User(ctx context.Context) *user.User { | ||
if v := ctx.Value(contextUser); v != nil { | ||
if u, ok := v.(*user.User); ok { | ||
return u | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func Lang(ctx context.Context, lang *string) string { | ||
if lang != nil && *lang != "" { | ||
return *lang | ||
} | ||
|
||
u := User(ctx) | ||
if u == nil { | ||
return "en" // default language | ||
} | ||
|
||
l := u.Lang() | ||
if l.IsRoot() { | ||
return "en" // default language | ||
} | ||
|
||
return l.String() | ||
} | ||
|
||
func Operator(ctx context.Context) *usecase.Operator { | ||
if v := ctx.Value(contextOperator); v != nil { | ||
if v2, ok := v.(*usecase.Operator); ok { | ||
return v2 | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func Sub(ctx context.Context) string { | ||
if v := ctx.Value(contextSub); v != nil { | ||
if v2, ok := v.(string); ok { | ||
return v2 | ||
} | ||
} | ||
return "" | ||
} | ||
|
||
func Usecases(ctx context.Context) *interfaces.Container { | ||
return ctx.Value(contextUsecases).(*interfaces.Container) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.