-
Notifications
You must be signed in to change notification settings - Fork 2
Matchers
Matchers are a important part of the clj-sandbox, they are used in the process of deciding which code can be run and which will be refused. While the matchers don’t actually say that is allowed but they are used to build lists. The basic testers are already included in the clj-sandbox package they are:
This is the most basic matcher, it takes one or more symbols and matches them against the names of functions. it does not distinguish between functions with the same name in different namespaces. It also matches special forms.
Example:
(function-matcher 'subs 'str)
The namespace-matcher matches against the namespace of functions and classes. It will match any function within a namespace or every class in a package. Both namespaces and packages are passed as symbols. Packages are defined as everything of a class name up until the last dot, so java.lang.String will have the namespace java.lang
Example:
(function-matcher 'clojure.core 'java.lang)
The class-matcher is made for the java interop. It is made to match against certain classes, this is helpful if you want to match for certain classes to be created or to forbid so.
Example:
(function-matcher 'java.lang.String 'java.lang.Integer)
If the matchers included does not rock your boat it’s pretty easy to create your own matcher, the matcher functions have to return a function that:
- takes a form, which is either
- a var as #’clojure.core/map
- a symbole, when it is a special form as ’let*
- a java class as java.lang.String
- it has to return:
- a list of true’s or false’s that indicate if it matches the passed form
- a empty list if it does not apply to this form
Lets look at a matcher that matches all functions that start with a certain letter as their first letter:
(defn silly-matcher
[& letters]
(fn [form]
(if (= (type form) clojure.lang.Var)
(map (partial = (first (str (:name (meta form))))) letters)
'())))
((silly-matcher \m \r) (resolve ’reduce)) ;; => (false true)
((silly-matcher \m \r) (resolve ’map)) ;; => (true false)
((silly-matcher \m \r) (resolve ’partition)) ;; => (false false)