<commit_before>(ns puppetlabs.trapperkeeper.services-internal
  (:import (clojure.lang IFn))
  (:require [clojure.walk :refer [postwalk]]
            [clojure.set :refer [difference union intersection]]
            [schema.core :as schema]
            [puppetlabs.kitchensink.core :as ks]))

(def optional-dep-default
  "This value is used in place of an optional service
   when the service is not included. In the future, it may be a more interesting or
   useful dummy service."
  nil)

(defn protocol?
  "A predicate to determine whether or not an object is a protocol definition"
  [p]
  ;; there might be a better way to do this, but it seems to work
  (and (map? p)
       (contains? p :on)
       (instance? Class (resolve (:on p)))))

(defn fn-sig?
  "A predicate to determine whether or not a form represents a valid function signature
  in the context of a service definition."
  [sig]
  (and (seq? sig)
       (> (count sig) 1)
       (symbol? (first sig))
       (vector? (second sig))))

(def Protocol
  "A schema to determine whether or not an object is a protocol definition."
  (schema/pred protocol?))

(def Symbol
  "Interal schema type for validating symbol values."
  (schema/pred symbol?))

(def Var
  "Interal schema type for validating var values."
  (schema/pred var?))

(def ServiceFnMap
  "Internal schema for a service's map of function references."
  {schema/Keyword IFn})

(def FnsMap
  "Internal schema for a map of a service's function body forms."
  {schema/Keyword [(schema/pred fn-sig?)]})

(def InternalServiceMap
  "Schema defining TK's internal representation of a service. This is used while
  processing the service macro."
  {:service-sym (schema/maybe Symbol)
   :service-protocol-sym (schema/maybe Symbol)
   :service-id schema/Keyword
   :service-fn-map ServiceFnMap
   :dependencies (schema/pred sequential?)
   :fns-map FnsMap})

(def ServiceMap
  "Schema defining the map used to represent a schema in the output service
   map."
  {schema/Keyword IFn})

(def DependencyMap
  "Schema for a map detailing optional vs. required dependencies for a
   service."
  {:optional (schema/pred vector?)
   :required (schema/pred vector?)})

(schema/defn ^:always-validate var->symbol :- Symbol
  "Returns a symbol for the var, including its namespace"
  [fn-var :- Var]
  (symbol (str (-> fn-var meta :ns .name))
          (str (-> fn-var meta :name))))

(schema/defn ^:always-validate transform-deps-map :- (schema/pred vector?)
  "Given a map of required and optional dependencies, return a vector that
  adheres to Prismatic Graph's binding syntax. Specifically, optional
  dependencies are transformed into {ServiceName nil}."
  [deps :- DependencyMap]
  (loop [optional (:optional deps) output (:required deps)]
    (if (empty? optional)
      output
      (let [dep (first optional)
            optional-form (array-map dep optional-dep-default)]
        (recur (rest optional) (conj output optional-form))))))

(schema/defn ^:always-validate find-prot-and-deps-forms!
  :- {:fns (schema/pred seq?) :dependencies (schema/pred vector?) :service-protocol-sym (schema/maybe Symbol)}
  "Given the forms passed to the service macro, find the service protocol
  (if one is provided), the dependency list, and the function definitions.
  Throws `IllegalArgumentException` if the forms do not represent a valid service.
  Returns a map containing the protocol, dependency list, and fn forms."
  [forms :- (schema/pred seq?)]
  (let [f (if (some true? ((juxt symbol? map? vector?) (first forms)))
            (first forms)
            (throw (IllegalArgumentException.<commit_msg>(FIXUP) create schema map for output and change if statement to be clearer<commit_after>(ns puppetlabs.trapperkeeper.services-internal
  (:import (clojure.lang IFn))
  (:require [clojure.walk :refer [postwalk]]
            [clojure.set :refer [difference union intersection]]
            [schema.core :as schema]
            [puppetlabs.kitchensink.core :as ks]))

(def optional-dep-default
  "This value is used in place of an optional service
   when the service is not included. In the future, it may be a more interesting or
   useful dummy service."
  nil)

(defn protocol?
  "A predicate to determine whether or not an object is a protocol definition"
  [p]
  ;; there might be a better way to do this, but it seems to work
  (and (map? p)
       (contains? p :on)
       (instance? Class (resolve (:on p)))))

(defn fn-sig?
  "A predicate to determine whether or not a form represents a valid function signature
  in the context of a service definition."
  [sig]
  (and (seq? sig)
       (> (count sig) 1)
       (symbol? (first sig))
       (vector? (second sig))))

(def Protocol
  "A schema to determine whether or not an object is a protocol definition."
  (schema/pred protocol?))

(def Symbol
  "Interal schema type for validating symbol values."
  (schema/pred symbol?))

(def Var
  "Interal schema type for validating var values."
  (schema/pred var?))

(def ServiceFnMap
  "Internal schema for a service's map of function references."
  {schema/Keyword IFn})

(def FnsMap
  "Internal schema for a map of a service's function body forms."
  {schema/Keyword [(schema/pred fn-sig?)]})

(def InternalServiceMap
  "Schema defining TK's internal representation of a service. This is used while
  processing the service macro."
  {:service-sym (schema/maybe Symbol)
   :service-protocol-sym (schema/maybe Symbol)
   :service-id schema/Keyword
   :service-fn-map ServiceFnMap
   :dependencies (schema/pred sequential?)
   :fns-map FnsMap})

(def ProtocolandDependenciesMap
  "Schema defining the map used to represent the protocol and dependencies
  of a service."
  {:fns (schema/pred seq?)
   :dependencies (schema/pred vector?)
   :service-protocol-sym (schema/maybe Symbol)})

(def ServiceMap
  "Schema defining the map used to represent a schema in the output service
   map."
  {schema/Keyword IFn})

(def DependencyMap
  "Schema for a map detailing optional vs. required dependencies for a
   service."
  {:optional (schema/pred vector?)
   :required (schema/pred vector?)})

(schema/defn ^:always-validate var->symbol :- Symbol
  "Returns a symbol for the var, including its namespace"
  [fn-var :- Var]
  (symbol (str (-> fn-var meta :ns .name))
          (str (-> fn-var meta :name))))

(schema/defn ^:always-validate transform-deps-map :- (schema/pred vector?)
  "Given a map of required and optional dependencies, return a vector that
  adheres to Prismatic Graph's binding syntax. Specifically, optional
  dependencies are transformed into {ServiceName nil}."
  [deps :- DependencyMap]
  (loop [optional (:optional deps) output (:required deps)]
    (if (empty? optional)
      output
      (let [dep (first optional)
            optional-form (array-map dep optional-dep-default)]
        (recur (rest optional) (conj output optional-form))))))

(schema/defn ^:always-validate find-prot-and-deps-forms! :- ProtocolandDependenciesMap
  "Given the forms passed to the service macro, find the service protocol
  (if one is provided), the dependency list, and the function definitions.
  Throws `IllegalArgumentException` if the forms do not represent a valid service.
  Returns a map containing the protocol, dependency list, and fn forms."
  [forms :- (schema/pred seq?)]
  (let [f (if ((some-fn symbol? map? vector?) (first forms))
            (first forms)
            (throw (IllegalArgumentException.
{'fraction_non_alphanumeric': 0.10392578650174779, 'fraction_numerical': 0.00026888948642108095, 'mean_word_length': 3.549847094801223, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7583798', 'n_tokens_mistral': 2195, 'n_tokens_neox': 2129, 'n_words': 878}