-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
287 additions
and
171 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,72 @@ | ||
package owl | ||
|
||
import ( | ||
"reflect" | ||
"strings" | ||
) | ||
|
||
type context struct { | ||
rule string | ||
schema map[string]string | ||
root reflect.Value | ||
parent reflect.Value | ||
value reflect.Value | ||
field reflect.StructField | ||
hasFormat func(string) bool | ||
format func(string, string) error | ||
} | ||
|
||
func (self context) Root() reflect.Value { | ||
return self.root | ||
} | ||
|
||
func (self context) Parent() reflect.Value { | ||
return self.parent | ||
} | ||
|
||
func (self context) Rule() string { | ||
return self.rule | ||
} | ||
|
||
func (self context) Param() string { | ||
return self.schema[self.rule] | ||
} | ||
|
||
func (self context) RuleParam(name string) (string, bool) { | ||
v, ok := self.schema[name] | ||
return v, ok | ||
} | ||
|
||
func (self context) Name() string { | ||
name := self.field.Name | ||
|
||
if tag := self.field.Tag.Get("json"); tag != "" { | ||
parts := strings.Split(tag, ",") | ||
|
||
if parts[0] != "-" { | ||
name = parts[0] | ||
} | ||
} | ||
|
||
return name | ||
} | ||
|
||
func (self context) FieldName() string { | ||
return self.field.Name | ||
} | ||
|
||
func (self context) Value() reflect.Value { | ||
return self.value | ||
} | ||
|
||
func (self context) CoerceValue() reflect.Value { | ||
return reflect.Indirect(self.value) | ||
} | ||
|
||
func (self context) HasFormat(name string) bool { | ||
return self.hasFormat(name) | ||
} | ||
|
||
func (self context) Format(name string, text string) error { | ||
return self.format(name, text) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package rules | ||
|
||
import "reflect" | ||
|
||
type Context interface { | ||
// get the root struct | ||
Root() reflect.Value | ||
|
||
// get the parent struct | ||
Parent() reflect.Value | ||
|
||
// get the field value | ||
Value() reflect.Value | ||
|
||
// get coerced field value, indirecting pointers and interfaces | ||
CoerceValue() reflect.Value | ||
|
||
// get the field "json" name if exists, otherwise get the struct field name | ||
Name() string | ||
|
||
// get the struct field name | ||
FieldName() string | ||
|
||
// get the current rule name | ||
Rule() string | ||
|
||
// get the current rules parameter | ||
Param() string | ||
|
||
// get a rules parameter | ||
RuleParam(name string) (string, bool) | ||
|
||
// check if instance has formatter | ||
HasFormat(name string) bool | ||
|
||
// format text using formatter | ||
Format(name string, text string) error | ||
} |
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.