From 739758cd2210a9159699e782dc8e9b422f707771 Mon Sep 17 00:00:00 2001 From: promix17 Date: Mon, 29 Aug 2016 17:52:41 +0300 Subject: [PATCH] conditions: add interface for adding condition factories (#33) Signed-off-by: Mikhail Pronyakin --- README.md | 16 ++++++++++++++++ condition.go | 5 +++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index dc90f52..77420de 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/condition.go b/condition.go index 593dcc0..c9cc6b0 100644 --- a/condition.go +++ b/condition.go @@ -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() @@ -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) },