Created
October 18, 2011 22:49
-
-
Save sritchie/1296982 to your computer and use it in GitHub Desktop.
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
(ns backtype.schema.bootstrap | |
(:import [backtype.schema GenderType] | |
[backtype.schematools SchemaTools SchemaClasses])) | |
(defn schema-classes [] | |
(seq SchemaClasses/CLASSES)) | |
(defn schema-class-names [] | |
(map (memfn getName) | |
(schema-classes))) | |
(defmacro define-schema-constants [klass prefix] | |
(let [defs (for [e (eval `(. ~klass metaDataMap))] | |
(let [id (.. e getKey getThriftFieldId) | |
fieldname (.. e getValue fieldName)] | |
`(def ~(symbol (.toUpperCase (str prefix "-" fieldname))) ~id)))] | |
`(do ~@defs))) | |
;; deprecated, use import-schema | |
(defmacro import-schema-classes [] | |
(cons 'do | |
(cons '(import backtype.schematools.SchemaTools) | |
(for [r (schema-class-names)] | |
`(import ~(symbol r)))))) | |
(defmacro import-schema [] | |
(cons 'do (list '(backtype.schema.bootstrap/import-schema-classes)))) | |
;; TODO: automate constant generation for enums | |
(def GENDER-MALE GenderType/MALE) | |
(def GENDER-FEMALE GenderType/FEMALE) | |
(doseq [c (schema-classes)] | |
(let [n (.getSimpleName c) | |
n (if (.endsWith n "PropertyValue") | |
(.substring n 0 (- (count n) (count "PropertyValue"))) | |
n)] | |
(when (.isAssignableFrom org.apache.thrift.TUnion c) | |
(eval `(define-schema-constants ~(symbol (.getName c)) ~(.toUpperCase n)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment