Skip to content

Commit

Permalink
Merge pull request #28 from sol-sh/optionaly
Browse files Browse the repository at this point in the history
optionally support
  • Loading branch information
idantavor authored Apr 25, 2024
2 parents fc58c9e + b7e5540 commit 445d8c4
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 11 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
## This library follows [Semantic Versioning](https://semver.org).
## This CHANGELOG follows [keepachangelog](https://keepachangelog.com/en/1.0.0/).

### VERSION [3.0.0]:
#### Changed
* Minimal supported protobuf version 3.15.0.
* when a getting an optional field that was not set, a nil will be returned instead of the deafault value of this field.
* Allow assoc a nil to optional fields - It will clear the field.
* Allow checking if an optional field was set or not with `p/has-field?`.

### VERSION [2.1.2]:
#### Changed
* return `:unrecognized` for unknown enum values in order to support forward compatibility.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ ignored completely.
## Installation
Add a dependency to your `project.clj` file:

[com.appsflyer/pronto "2.1.2"]
[com.appsflyer/pronto "3.0.0"]

Note that the library comes with no Java protobuf dependencies of its own and they are expected to be provided by consumers of the library, with a minimal version of `3.9.0`.
Note that the library comes with no Java protobuf dependencies of its own and they are expected to be provided by consumers of the library, with a minimal version of `3.15.0`.

## How does it work?

Expand Down
4 changes: 2 additions & 2 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(def protobuf-version "3.9.0")
(def protobuf-version "3.15.0")


(defproject com.appsflyer/pronto "2.1.2"
(defproject com.appsflyer/pronto "3.0.0"
:description "clojure support for protocol buffers"
:url "https://github.com/AppsFlyer/pronto"
:license {:name "Eclipse Public License"
Expand Down
2 changes: 1 addition & 1 deletion resources/proto/people.proto
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ enum Level {

message Like {
string desc = 1;
Level level = 2;
optional Level level = 2;
}

message House {
Expand Down
2 changes: 1 addition & 1 deletion src/clj/pronto/emitters.clj
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@
fields k true
(fn [field]
(let [^Descriptors$FieldDescriptor fd (:fd field)]
(if (u/struct? fd)
(if (or (u/struct? fd) (u/optional? fd))
(let [has-method (symbol (str ".has" (u/field->camel-case (:fd field))))]
`(~has-method ~o))
`(throw (IllegalArgumentException. (str "field " ~k " cannot be checked for field existence"))))))))
Expand Down
4 changes: 2 additions & 2 deletions src/clj/pronto/type_gen.clj
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
res
(u/with-type-hint (gensym 'res) field-type))
clear-method (symbol (str ".clear" (u/field->camel-case fd)))]
(if (u/message? fd)
(if (or (u/message? fd) (u/optional? fd))
`(if (nil? ~v)
(~clear-method ~builder)
(let [~res ~(w/unwrap wrapper v)]
Expand All @@ -73,7 +73,7 @@
has-method (symbol (str ".has" (u/field->camel-case fd)))
get-form `(let [~v (~getter ~o)]
~(w/wrap wrapper v))]
(if-not (u/message? fd)
(if-not (or (u/message? fd) (u/optional? fd))
get-form
`(when (~has-method ~o)
~get-form)))))))
Expand Down
3 changes: 3 additions & 0 deletions src/clj/pronto/utils.clj
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@
(not (.isMapField fd))
(not (.isRepeated fd))))

(defn optional? [^Descriptors$FieldDescriptor fd]
(.hasOptionalKeyword fd))

(defn enum? [^Descriptors$FieldDescriptor fd]
(= (.getType fd)
Descriptors$FieldDescriptor$Type/ENUM))
Expand Down
29 changes: 26 additions & 3 deletions test/pronto/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -812,10 +812,33 @@ an error in the generated code"
(is (= ["DESC1" "DESC2" "DESC3"] (map :desc (:likes new-p)))))))


(deftest hint-validity []
(deftest hint-validity
(testing "hinting should only work within with-hints or p->"
(is (thrown? Exception
(macroexpand '(pronto.lens/hint (p/proto-map mapper People$Person)
People$Person
mapper)))))
)
mapper))))))

(deftest optional-field
(testing "usage of optional fields"
(let [p1 (p/proto-map mapper People$Like :level :LOW)
p2 (p/proto-map mapper People$Like)]
(is (= (:level p1) :LOW))
(is (true? (p/has-field? p1 :level)))
(-> (p/clear-field p1 :level)
(p/has-field? :level)
false?
is)
(is (= (p/clear-field p1 :level) p2))

(-> (assoc p1 :level nil)
(p/has-field? :level)
false?
is)
(is (= (assoc p1 :level nil) p2))

(is (false? (p/has-field? p2 :level)))
(is (= (:level p2) nil))

(is (= (assoc p2 :level :LOW) p1))
(is (p/has-field? (assoc p2 :level :LOW) :level)))))

0 comments on commit 445d8c4

Please sign in to comment.