Fork me on GitHub
#specter
<
2020-12-21
>
bruno.bonacci12:12:40

Hi, is there a way to distinguish between a path not found and a path-value with nil? like get-in with default value?

(get-in {"foo" nil} ["foo"] :not-found) ;;=> nil
(get-in {"bar" nil} ["foo"] :not-found) ;;=> :not-found
(select ["foo"] {"foo" nil}) ;;=> [nil]
(select ["bar"] {"foo" nil}) ;;=> [nil]

nathanmarz17:12:20

@bruno.bonacci use the must navigator

bruno.bonacci17:12:29

ok, however must uses contains? which fails if it not a map

bruno.bonacci17:12:29

so i have to put a guard predicate on every nesting to check that is a map

bruno.bonacci17:12:36

like [(pred map?) (must "foo") (pred map?) (must "bar") (pred map?) (must "baz")]

bruno.bonacci17:12:21

example: (get-in {"foo" 1} ["foo" "bar"] :not-found) ;;=> :not-found (select [(must "foo") (must "bar")] {"foo" 1}) throws: Execution error (IllegalArgumentException) at com.rpl.specter.navs$fn$reify__33786/select_STAR_ (navs.cljc:655). contains? not supported on type: java.lang.Long

nathanmarz18:12:34

personally I think it's a good thing specter throws an exception in that case

nathanmarz18:12:44

but you can always write your own navigator with the more lenient behavior

👍 3