-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #127 from youyuanwu/feature/readonly-validate
WIP: feature/ReadOnly-Validation
- Loading branch information
Showing
6 changed files
with
190 additions
and
1 deletion.
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,56 @@ | ||
package validate | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
// validateCtxKey is the key type of context key in this pkg | ||
type validateCtxKey string | ||
|
||
const ( | ||
operationTypeKey validateCtxKey = "operationTypeKey" | ||
) | ||
|
||
type operationType string | ||
|
||
const ( | ||
request operationType = "request" | ||
response operationType = "response" | ||
none operationType = "none" // not specified in ctx | ||
) | ||
|
||
var operationTypeEnum []operationType = []operationType{request, response, none} | ||
|
||
// WithOperationRequest returns a new context with operationType request | ||
// in context value | ||
func WithOperationRequest(ctx context.Context) context.Context { | ||
return withOperation(ctx, request) | ||
} | ||
|
||
// WithOperationRequest returns a new context with operationType response | ||
// in context value | ||
func WithOperationResponse(ctx context.Context) context.Context { | ||
return withOperation(ctx, response) | ||
} | ||
|
||
func withOperation(ctx context.Context, operation operationType) context.Context { | ||
return context.WithValue(ctx, operationTypeKey, operation) | ||
} | ||
|
||
// extractOperationType extracts the operation type from ctx | ||
// if not specified or of unknown value, return none operation type | ||
func extractOperationType(ctx context.Context) operationType { | ||
v := ctx.Value(operationTypeKey) | ||
if v == nil { | ||
return none | ||
} | ||
res, ok := v.(operationType) | ||
if !ok { | ||
return none | ||
} | ||
// validate the value is in operation enum | ||
if err := Enum("", "", res, operationTypeEnum); err != nil { | ||
return none | ||
} | ||
return res | ||
} |
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,50 @@ | ||
package validate | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestContext_ExtractOperationType(t *testing.T) { | ||
|
||
var testCases = []struct { | ||
Ctx context.Context | ||
ExpectedOpType operationType | ||
}{ | ||
{ | ||
Ctx: WithOperationRequest(context.Background()), | ||
ExpectedOpType: request, | ||
}, | ||
{ | ||
Ctx: WithOperationResponse(context.Background()), | ||
ExpectedOpType: response, | ||
}, | ||
{ | ||
Ctx: context.Background(), | ||
ExpectedOpType: none, | ||
}, | ||
{ | ||
Ctx: context.WithValue(context.Background(), validateCtxKey("dummy"), "dummy val"), | ||
ExpectedOpType: none, | ||
}, | ||
{ | ||
Ctx: context.WithValue(context.Background(), operationTypeKey, "dummy val"), | ||
ExpectedOpType: none, | ||
}, | ||
{ | ||
Ctx: context.WithValue(context.Background(), operationTypeKey, operationType("dummy val")), | ||
ExpectedOpType: none, | ||
}, | ||
} | ||
|
||
for idx, tc := range testCases { | ||
t.Run(fmt.Sprintf("TestCase #%d", idx), func(t *testing.T) { | ||
op := extractOperationType(tc.Ctx) | ||
assert.Equal(t, tc.ExpectedOpType, op) | ||
}) | ||
} | ||
|
||
} |
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