Giter Club home page Giter Club logo

Comments (7)

pkpkpk avatar pkpkpk commented on August 27, 2024

Try master. should be fn<rdr,tag, field-count,name->map-ctor>

from fress.

pkpkpk avatar pkpkpk commented on August 27, 2024

can probably get rid of the name->map-ctor arg

from fress.

FerdiKuehne avatar FerdiKuehne commented on August 27, 2024

yeah without name->map-ctor arg would be awesome. And also if the record is not match could it be possible to return an custom object like your taggedObject?

from fress.

pkpkpk avatar pkpkpk commented on August 27, 2024

Can you explain your use case alittle bit for me? Are you trying to instantiate stuff before calling the record constructor? If so it may be better to read/write as custom types rather than records

from fress.

FerdiKuehne avatar FerdiKuehne commented on August 27, 2024

Good morning!
Sorry for the delayed response.
Best way to explain the case is with the following code:

  (defrecord IncognitoTaggedLiteral [tag value])

  (defn incognito-write-handlers [write-handlers]
    {"record" (fn [w rec]
                (let [{:keys [tag] :as r} (if (isa? (type rec) incognito.base.IncognitoTaggedLiteral)
                                            (into {} rec)
                                            (incognito-writer @write-handlers rec))]
                  (write-tag w "record" 2)
                  (write-object w (str tag))
                  (write-tag w "map" 1)
                  (beginClosedList w)
                  (doseq [[field value] r]
                    (write-object w field true)
                    (write-object w value))
                  (endList w)))})

  (defn incognito-reader [read-handlers m]
    (if (read-handlers (:tag m))
      ((read-handlers (:tag m)) (:value m))
      (map->IncognitoTaggedLiteral m)))

  (defn incognito-read-handlers [read-handlers]
    {"record" (fn [rdr tag _]
                (let [tag   (read-object rdr)
                      value (read-object rdr)]
                  (incognito-reader @read-handlers
                                    {:tag tag :value val})))})

  (defrecord SomeRecord [f1 f2])
  (def rec (SomeRecord. "field1" "field2"))
  (def buf (fress.api/byte-stream))
  (def writer (fress.api/create-writer buf :handlers (incognito-write-handlers (atom {'incognito.fressian.SomeRecord (fn [foo] (println "foos") (assoc foo :c "donkey"))}))))
  (fress.api/write-object writer rec)
  (fress.api/read buf :handlers (incognito-read-handlers (atom {})))
  ;read returns  => #fress.reader.TaggedObject{:tag "record", :value #js ["incognito.fressian/SomeRecord" {:value {:f1 "field1", :c "donkey", :f2 "field2"}, :tag incognito.fressian/SomeRecord}]}


  ;it should return => #incognito.base.IncognitoTaggedLiteral{:tag incognito.fressian/SomeRecord, :value {:f1 "field1", :c "donkey", :f2 "field2"}}

The custom read-handler for "record" is somehow ignored. Sorry for the confusion. I hope the answer can help.

Best Regards F

from fress.

pkpkpk avatar pkpkpk commented on August 27, 2024

I had it set to look for :record, you are right it should have been a string

from fress.

FerdiKuehne avatar FerdiKuehne commented on August 27, 2024

Big thanks! Well done!
It's working! I think the issue can be closed.

Best Regards F

from fress.

Related Issues (6)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.