From 90b92d8c73446bb50eee14ca8d88c5224002001a Mon Sep 17 00:00:00 2001
From: Muh Muhten <muh.muhten@gmail.com>
Date: Sun, 15 Oct 2017 01:57:17 -0400
Subject: [PATCH 7/9] Add import metadata key "optional"

A library marked is imported if found, but silently skipped if missing.
This is the desired semantic for the auto-include at ~/.jq
---
 src/linker.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

--- a/src/linker.c
+++ b/src/linker.c
@@ -246,6 +246,9 @@ static int process_dependencies(jq_state
     jv v = jv_object_get(jv_copy(dep), jv_string("raw"));
     if (jv_get_kind(v) == JV_KIND_TRUE)
       raw = 1;
+    int optional = 0;
+    if (jv_get_kind(jv_object_get(jv_copy(dep), jv_string("optional"))) == JV_KIND_TRUE)
+      optional = 1;
     jv_free(v);
     jv relpath = validate_relpath(jv_object_get(jv_copy(dep), jv_string("relpath")));
     jv as = jv_object_get(jv_copy(dep), jv_string("as"));
@@ -259,10 +262,14 @@ static int process_dependencies(jq_state
     jv resolved = find_lib(jq, relpath, search, is_data ? ".json" : ".jq", jv_copy(jq_origin), jv_copy(lib_origin));
     // XXX ...move the rest of this into a callback.
     if (!jv_is_valid(resolved)) {
+      jv_free(as);
+      if (optional) {
+        jv_free(resolved);
+        continue;
+      }
       jv emsg = jv_invalid_get_msg(resolved);
       jq_report_error(jq, jv_string_fmt("jq: error: %s\n",jv_string_value(emsg)));
       jv_free(emsg);
-      jv_free(as);
       jv_free(deps);
       jv_free(jq_origin);
       jv_free(lib_origin);