f490e57
Branches > main > f490e57 > files
Improve names and docs on reading and writing
Commit: f490e5768f1a4c9410964e2654279526b4fb87e2
Parent: 8155797
2 changed files:
src/net/bowmap/packet/read.clj view file
@@ -48,16 +48,18 @@ #_(defn read-array [codec len buf]) -(defn read-table! [buf table]- (reduce (fn [acc {:keys [field type]}]- (assoc acc field (type buf)))- {} table))+(defn read-table! [buf codec-fields]+ (reduce (fn [acc {:keys [field codec]}]+ (if-some [x (codec buf)]+ (assoc acc field x)+ acc))+ {} codec-fields)) (declare readers) (defn reader- "Returns a function of a ByteBuf that reads the respective type.- Will throw if no such type exists."+ "Returns a function of a ByteBuf that reads the respective type, as defined by 'readers'.+ Throws if no such type exists." [t] (if (vector? t) (let [[t & args] t]@@ -77,8 +79,7 @@ :uuid read-uuid! :table (fn [table] (let [table (->> (s/conform ::t/table table)- ;; TODO make this :codec- (mapv #(update % :type reader)))]+ (mapv #(assoc % :codec (reader (:type %)))))] (fn [buf] (read-table! buf table)))) :enum (fn [t m]src/net/bowmap/packet/write.clj view file
@@ -27,28 +27,30 @@ (defn write-json! [buf s] (write-string! buf (json/write-str s))) -(defn write-list! [buf t xs]+(defn write-list! [buf codec xs] ;; Reduce :p- (doseq [x xs]- (t buf x))- buf)+ (reduce (fn [acc x]+ (codec acc x))+ buf xs)) (defn write-uuid! [buf uuid]- (ByteBuf/.writeLong buf (UUID/.getMostSignificantBits uuid))- (ByteBuf/.writeLong buf (UUID/.getLeastSignificantBits uuid)))+ (-> buf+ (ByteBuf/.writeLong (UUID/.getMostSignificantBits uuid))+ (ByteBuf/.writeLong (UUID/.getLeastSignificantBits uuid)))) ;; TODO this is where optionality and nil checking should be, and the packet stuff should use this.-(defn write-table! [buf t? m]- (doseq [{:keys [field type]} t?]- (type buf (get m field)))- buf)+(defn write-table! [buf codec-fields m]+ (reduce (fn [acc {:keys [field codec]}]+ (codec acc (get m field)))+ buf codec-fields)) (declare writers) ;; I think this should have :required/:optional/:prefixed-optional, ;; and have it that non-optional fields are implicity :required and thus nil checked (defn writer- ""+ "Returns a function of a Bytebuf and a value and encodes/writes 't' to the buf, as defined by 'writers'.+ Throws if no such type exists." [t] (if (vector? t) (let [[t & args] t]@@ -92,9 +94,9 @@ (fn [buf x] (write-optional! buf codec x)))) :table (fn [t]- (let [t? (->> (s/conform ::t/table t)- (mapv #(update % :type writer)))]- (fn [buf m] (write-table! buf t? m))))+ (let [codec-fields (->> (s/conform ::t/table t)+ (mapv #(assoc % :codec (writer (:type %)))))]+ (fn [buf m] (write-table! buf codec-fields m)))) :list (fn [t] (let [codec (writer t)] (fn [buf xs] (write-list! buf codec xs))))})