Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New feature: Add schema metadata to deserialized avro records #174

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 18 additions & 15 deletions src/jackdaw/serdes/avro.clj
Expand Up @@ -399,21 +399,24 @@

(avro->clj [_ avro-record]
(when avro-record
(into {}
(comp (map first)
(map (fn [^Schema$Field field]
(let [field-name (.name field)
field-key (keyword (unmangle field-name))
[_ field-coercion :as entry] (get field->schema+coercion field-key)
value (.get ^GenericData$Record avro-record field-name)]
(when-not field-coercion
(throw (ex-info "Unable to deserialize field"
{:field field
:field-name field-name
:field-key field-key
:entry entry})))
[field-key (avro->clj field-coercion value)]))))
(vals field->schema+coercion))))
(with-meta
(into {}
(comp (map first)
(map (fn [^Schema$Field field]
(let [field-name (.name field)
field-key (keyword (unmangle field-name))
[_ field-coercion :as entry] (get field->schema+coercion field-key)
value (.get ^GenericData$Record avro-record field-name)]
(when-not field-coercion
(throw (ex-info "Unable to deserialize field"
{:field field
:field-name field-name
:field-key field-key
:entry entry})))
[field-key (avro->clj field-coercion value)]))))
(vals field->schema+coercion))
{:name (.getName schema)
:fullname (.getFullName schema)})))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think when hyphenating the FullName symbol I'd expect to see full-name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just pushed a commit with the hyphenated name.


(clj->avro [_ clj-map path]
(when-not (map? clj-map)
Expand Down
10 changes: 10 additions & 0 deletions test/jackdaw/serdes/avro_test.clj
Expand Up @@ -512,6 +512,16 @@
"topic"
(uuid/to-string uuid/+null+))))))))

(deftest record-metadata
(let [schema {:type "record"
:name "MyRecord"
:namespace "com.fundingcircle"
:fields [{:name "field1" :type "string"}]}
serde (->serde (json/write-str schema))]

(is (= {:name "MyRecord" :fullname "com.fundingcircle.MyRecord"}
(meta (round-trip serde "whatever" {:field1 "foo"}))))))

(deftest schemaless-test
(let [serde (->serde nil)]
(is (= (round-trip serde "bananas" "hello")
Expand Down