Skip to content

Instantly share code, notes, and snippets.

@edbond
Created December 10, 2013 11:13
Show Gist options
  • Save edbond/7889075 to your computer and use it in GitHub Desktop.
Save edbond/7889075 to your computer and use it in GitHub Desktop.
snakeyaml
(ns snakeyaml-example.core
(:require [clojure.string :refer (split)])
(:import [org.yaml.snakeyaml.constructor SafeConstructor
AbstractConstruct Construct]
[org.yaml.snakeyaml.nodes Tag Node ScalarNode NodeId]
[snakeyaml-example.core Dice ConstructDice DiceConstructor]))
(set! *warn-on-reflection* true)
(gen-class
:name snakeyaml-example.core.Dice
:prefix dice-
:state state
:init init
:constructors {[Integer Integer] []})
(gen-class
:name snakeyaml-example.core.ConstructDice
:prefix construct-dice-
:extends org.yaml.snakeyaml.constructor.AbstractConstruct)
(gen-class
:name snakeyaml-example.core.DiceConstructor
:prefix dice-constructor-
:post-init initialize
:extends org.yaml.snakeyaml.constructor.SafeConstructor)
(defn dice-init
[a b]
[[] {:a a :b b}])
(defn construct-dice-construct
[^ScalarNode node]
(let [^String val (.constructScalar node)
[as bs] (split val #"d" 2)
a (Integer/parseInt as)
b (Integer/parseInt bs)]
(Dice. a b)))
(defn dice-constructor-initialize
[this]
;; (println "Initialize DiceConstructor!")
;; (throw (RuntimeException. "OK!"))
(let [yc (.yamlConstructors this)]
(.put yc (Tag. "!dice") (ConstructDice.))))
(defn -main [& args]
(Dice. (int 3) (int 5))
(DiceConstructor.)
(ConstructDice.))
;; class DiceConstructor extends SafeConstructor {
;; public DiceConstructor() {
;; this.yamlConstructors.put(new Tag("!dice"), new ConstructDice());
;; }
;; private class ConstructDice extends AbstractConstruct {
;; public Object construct(Node node) {
;; String val = (String) constructScalar((ScalarNode) node);
;; int position = val.indexOf('d');
;; Integer a = new Integer(val.substring(0, position));
;; Integer b = new Integer(val.substring(position + 1));
;; return new Dice(a, b);
;; }
;; }
;; }
(defproject snakeyaml-example "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.5.1"]
[org.yaml/snakeyaml "1.13"]]
:main snakeyaml-example.core
:aot [snakeyaml-example.core])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment