Skip to content

Commit

Permalink
conditions: add interface for adding condition factories (#33)
Browse files Browse the repository at this point in the history
Signed-off-by: Mikhail Pronyakin <pronyakin@onsec.ru>
  • Loading branch information
promix17 authored and arekkas committed Aug 29, 2016
1 parent e38e0a2 commit 739758c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,22 @@ There are a couple of conditions available:
* [String Equal Condition](condition_string_equal.go): Matches two strings.
* [Subject Condition](condition_subject_equal.go): Matches when the condition field is equal to the subject field.

You can add custom conditions by using `ladon.ConditionFactories` (for more information see condition.go):

```go
import "github.com/ory-am/ladon"

func main() {
// ...

ladon.ConditionFactories[new(CustomCondition).GetName()] = func() Condition {
return new(CustomCondition)
}

// ...
}
```

### Examples

Let's assume that we are using the policy from above for the following requests.
Expand Down
5 changes: 3 additions & 2 deletions condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (cs Conditions) UnmarshalJSON(data []byte) error {
}

for k, jc := range jcs {
for name, c := range conditionFactories {
for name, c := range ConditionFactories {
if name == jc.Type {
dc = c()

Expand All @@ -82,7 +82,8 @@ type jsonCondition struct {
Options json.RawMessage `json:"options"`
}

var conditionFactories = map[string]func() Condition{
// You can add custom conditions to ConditionFactories
var ConditionFactories = map[string]func() Condition{
new(StringEqualCondition).GetName(): func() Condition {
return new(StringEqualCondition)
},
Expand Down

0 comments on commit 739758c

Please sign in to comment.