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

from edn to google.protobuf.struct #70

Open
naomijub opened this issue Jun 21, 2023 · 0 comments
Open

from edn to google.protobuf.struct #70

naomijub opened this issue Jun 21, 2023 · 0 comments

Comments

@naomijub
Copy link

Hello, is there anyway to parse and edn like {#xtdb/id \"1073c72f6e04ee66347a627efd9096910b790e66\" {:map {:k2 \"v2\"}, :bool? true, :number 3.0, :listing [1.0 2.0 3.0 4.0], :key \"ARROZ\", :crux.db/id :id1}}to google.protobuf.struct ?

Generated code:

;-----------------------------------------------------------------------------
; SpeculativeTxResponse
;-----------------------------------------------------------------------------
(defrecord SpeculativeTxResponse-record [valid-time tx-time tx-id entity-cache-size batch-size edn-document]
  pb/Writer
  (serialize [this os]
    (serdes.core/write-String 1  {:optimize true} (:valid-time this) os)
    (serdes.core/write-String 2  {:optimize true} (:tx-time this) os)
    (serdes.core/write-Int64 3  {:optimize true} (:tx-id this) os)
    (serdes.core/write-Int32 4  {:optimize true} (:entity-cache-size this) os)
    (serdes.core/write-Int32 5  {:optimize true} (:batch-size this) os)
    (serdes.core/write-embedded 6 (:edn-document this) os))
  pb/TypeReflection
  (gettype [this]
    "com.xtdb.protos.SpeculativeTxResponse"))

(s/def :com.xtdb.protos.SpeculativeTxResponse/valid-time string?)
(s/def :com.xtdb.protos.SpeculativeTxResponse/tx-time string?)
(s/def :com.xtdb.protos.SpeculativeTxResponse/tx-id int?)
(s/def :com.xtdb.protos.SpeculativeTxResponse/entity-cache-size int?)
(s/def :com.xtdb.protos.SpeculativeTxResponse/batch-size int?)

(s/def ::SpeculativeTxResponse-spec (s/keys :opt-un [:com.xtdb.protos.SpeculativeTxResponse/valid-time :com.xtdb.protos.SpeculativeTxResponse/tx-time :com.xtdb.protos.SpeculativeTxResponse/tx-id :com.xtdb.protos.SpeculativeTxResponse/entity-cache-size :com.xtdb.protos.SpeculativeTxResponse/batch-size]))
(def SpeculativeTxResponse-defaults {:valid-time "" :tx-time "" :tx-id 0 :entity-cache-size 0 :batch-size 0})

(defn cis->SpeculativeTxResponse
  "CodedInputStream to SpeculativeTxResponse"
  [is]
  (map->SpeculativeTxResponse-record (tag-map SpeculativeTxResponse-defaults (fn [tag index] (case index 1 [:valid-time (serdes.core/cis->String is)] 2 [:tx-time (serdes.core/cis->String is)] 3 [:tx-id (serdes.core/cis->Int64 is)] 4 [:entity-cache-size (serdes.core/cis->Int32 is)] 5 [:batch-size (serdes.core/cis->Int32 is)] 6 [:edn-document (com.google.protobuf/ecis->Struct is)] [index (serdes.core/cis->undefined tag is)])) is)))

(defn ecis->SpeculativeTxResponse
  "Embedded CodedInputStream to SpeculativeTxResponse"
  [is]
  (serdes.core/cis->embedded cis->SpeculativeTxResponse is))

(defn new-SpeculativeTxResponse
  "Creates a new instance from a map, similar to map->SpeculativeTxResponse except that
  it properly accounts for nested messages, when applicable.
  "
  [init]
  {:pre [(if (s/valid? ::SpeculativeTxResponse-spec init) true (throw (ex-info "Invalid input" (s/explain-data ::SpeculativeTxResponse-spec init))))]}
  (-> (merge SpeculativeTxResponse-defaults init)
      (cond-> (some? (get init :edn-document)) (update :edn-document com.google.protobuf/new-Struct))
      (map->SpeculativeTxResponse-record)))

(defn pb->SpeculativeTxResponse
  "Protobuf to SpeculativeTxResponse"
  [input]
  (cis->SpeculativeTxResponse (serdes.stream/new-cis input)))

(def ^:protojure.protobuf.any/record SpeculativeTxResponse-meta {:type "com.xtdb.protos.SpeculativeTxResponse" :decoder pb->SpeculativeTxResponse})

Google code: com.google.protobuf/ecis->Struct

;-----------------------------------------------------------------------------
; Struct
;-----------------------------------------------------------------------------
(defrecord Struct-type [fields]
  pb/Writer

  (serialize [this os]
    (serdes.complex/write-map new-Struct-FieldsEntry 1 (:fields this) os)))

(s/def ::Struct-spec (s/keys :opt-un []))
(def Struct-defaults {:fields [] })

(defn cis->Struct
  "CodedInputStream to Struct"
  [is]
  (->> (tag-map Struct-defaults
         (fn [tag index]
             (case index
               1 [:fields (serdes.complex/cis->map ecis->Struct-FieldsEntry is)]

               [index (serdes.core/cis->undefined tag is)]))
         is)
        (map->Struct-type)))

(defn ecis->Struct
  "Embedded CodedInputStream to Struct"
  [is]
  (serdes.core/cis->embedded cis->Struct is))

(defn new-Struct
  "Creates a new instance from a map, similar to map->Struct except that
  it properly accounts for nested messages, when applicable.
  "
  [init]
  {:pre [(if (s/valid? ::Struct-spec init) true (throw (ex-info "Invalid input" (s/explain-data ::Struct-spec init))))]}
  (-> (merge Struct-defaults init)
      (map->Struct-type)))

(defn pb->Struct
  "Protobuf to Struct"
  [input]
  (cis->Struct (serdes.stream/new-cis input)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant