Merge nvval rework v4.5-rc1
authorJosef 'Jeff' Sipek <jeffpc@josefsipek.net>
Thu, 12 Apr 2018 11:40:47 -0400
changeset 953 1dc65e2b39e7
parent 949 fe2d403b8be7 (current diff)
parent 952 d13f51a2d239 (diff)
child 954 2ff3f1268927
Merge nvval rework
--- a/comment.c	Mon Apr 02 15:19:41 2018 -0400
+++ b/comment.c	Thu Apr 12 11:40:47 2018 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009-2017 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
+ * Copyright (c) 2009-2018 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
@@ -81,9 +81,9 @@
 #define COMMENT_EMPTY		"x"
 
 static const struct nvl_convert_info comment_convert[] = {
-	{ .name = COMMENT_DATE,		.tgt_type = NVT_INT, },
-	{ .name = COMMENT_ID,		.tgt_type = NVT_INT, },
-	{ .name = COMMENT_CAPTCHA,	.tgt_type = NVT_INT, },
+	{ .name = COMMENT_DATE,		.tgt_type = VT_INT, },
+	{ .name = COMMENT_ID,		.tgt_type = VT_INT, },
+	{ .name = COMMENT_CAPTCHA,	.tgt_type = VT_INT, },
 	{ .name = NULL, },
 };
 
@@ -416,13 +416,13 @@
 	int id;
 
 	if (nvl_exists_type(req->scgi->request.headers, SCGI_HTTP_USER_AGENT,
-			    NVT_STR)) {
+			    VT_STR)) {
 		DBG("Missing user agent...");
 		return USERAGENT_MISSING;
 	}
 
 	if (nvl_exists_type(req->scgi->request.headers, SCGI_REMOTE_ADDR,
-			    NVT_STR)) {
+			    VT_STR)) {
 		DBG("Missing remote addr...");
 		return INTERNAL_ERR;
 	}
--- a/pipeline.c	Mon Apr 02 15:19:41 2018 -0400
+++ b/pipeline.c	Thu Apr 12 11:40:47 2018 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013-2017 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
+ * Copyright (c) 2013-2018 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
@@ -143,6 +143,8 @@
 		case VT_CONS:
 		case VT_BOOL:
 		case VT_CHAR:
+		case VT_ARRAY:
+		case VT_NVL:
 			panic("%s called with value type %d", __func__,
 			      val->type);
 	}
--- a/post_nv.c	Mon Apr 02 15:19:41 2018 -0400
+++ b/post_nv.c	Thu Apr 12 11:40:47 2018 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009-2017 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
+ * Copyright (c) 2009-2018 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
@@ -31,38 +31,27 @@
 static int __tag_val(struct nvlist *post, avl_tree_t *list)
 {
 	struct post_tag *cur;
-	struct nvval *tags;
+	struct val **tags;
 	size_t ntags;
 	size_t i;
-	int ret;
 
 	ntags = avl_numnodes(list);
 
-	tags = mem_reallocarray(NULL, ntags, sizeof(struct nvval));
+	tags = mem_reallocarray(NULL, ntags, sizeof(struct val *));
 	if (!tags)
 		return -ENOMEM;
 
 	i = 0;
-	avl_for_each(list, cur) {
-		struct nvval *tag = &tags[i++];
-
-		tag->type = NVT_STR;
-		tag->str = str_getref(cur->tag);
-	}
+	avl_for_each(list, cur)
+		tags[i++] = str_getref_val(cur->tag);
 
-	ret = nvl_set_array(post, "tags", tags, ntags);
-	if (ret) {
-		nvval_release_array(tags, ntags);
-		free(tags);
-	}
-
-	return ret;
+	return nvl_set_array(post, "tags", tags, ntags);
 }
 
 static int __com_val(struct nvlist *post, struct list *list)
 {
 	struct comment *cur;
-	struct nvval *comments;
+	struct val **comments;
 	size_t ncomments;
 	int ret;
 	int i;
@@ -76,13 +65,12 @@
 	if (!ncomments)
 		return 0;
 
-	comments = mem_reallocarray(NULL, ncomments, sizeof(struct nvval));
+	comments = mem_reallocarray(NULL, ncomments, sizeof(struct val *));
 	if (!comments)
 		return -ENOMEM;
 
 	i = 0;
 	list_for_each(cur, list) {
-		struct nvval *comment = &comments[i++];
 		struct nvlist *c;
 
 		c = nvl_alloc();
@@ -91,8 +79,7 @@
 			goto err;
 		}
 
-		comment->type = NVT_NVL;
-		comment->nvl = c;
+		comments[i++] = nvl_cast_to_val(c);
 
 		if ((ret = nvl_set_int(c, "commid", cur->id)))
 			goto err;
@@ -110,14 +97,12 @@
 			goto err;
 	}
 
-	ret = nvl_set_array(post, "comments", comments, ncomments);
-	if (ret)
-		goto err;
-
-	return 0;
+	return nvl_set_array(post, "comments", comments, ncomments);
 
 err:
-	nvval_release_array(comments, i);
+	while (i-- > 0)
+		val_putref(comments[i]);
+
 	free(comments);
 
 	return ret;
@@ -202,24 +187,20 @@
 }
 
 /*
- * Fill in the `posts' array with all posts matching the prepared and bound
- * statement.
- *
- * `stmt' should be all ready to execute and it should output two columns:
- *     post id
- *     post time
+ * Set "posts", "lastupdate", and "moreposts" vars based on the array of
+ * posts passed in as @posts.
  */
 void load_posts(struct req *req, struct post **posts, int nposts,
 		bool moreposts)
 {
-	struct nvval *nvposts;
+	struct val **nvposts;
 	size_t nnvposts;
 	time_t maxtime;
 	size_t i;
 
 	maxtime = 0;
 
-	nvposts = mem_reallocarray(NULL, nposts, sizeof(struct nvval));
+	nvposts = mem_reallocarray(NULL, nposts, sizeof(struct val *));
 	ASSERT(nvposts);
 
 	nnvposts = 0;
@@ -229,9 +210,8 @@
 
 		post_lock(post, true);
 
-		nvposts[nnvposts].type = NVT_NVL;
-		nvposts[nnvposts].nvl = __store_vars(req, post, NULL);
-		if (IS_ERR(nvposts[nnvposts].nvl)) {
+		nvposts[nnvposts] = nvl_cast_to_val(__store_vars(req, post, NULL));
+		if (IS_ERR(nvposts[nnvposts])) {
 			post_unlock(post);
 			post_putref(post);
 			continue;
--- a/render.c	Mon Apr 02 15:19:41 2018 -0400
+++ b/render.c	Thu Apr 12 11:40:47 2018 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013-2017 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
+ * Copyright (c) 2013-2018 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
@@ -68,7 +68,7 @@
 		 tmpl);
 
 	raw = file_cache_get_cb(path, revalidate_all_posts, NULL);
-	if (!raw)
+	if (IS_ERR(raw))
 		return NULL;
 
 	out = render_page(req, str_cstr(raw));
--- a/req.c	Mon Apr 02 15:19:41 2018 -0400
+++ b/req.c	Thu Apr 12 11:40:47 2018 -0400
@@ -197,7 +197,7 @@
 	nvl_set_nvl(logentry, "options", tmp);
 
 	/* serialize */
-	buf = nvl_pack(logentry, NVF_JSON);
+	buf = nvl_pack(logentry, VF_JSON);
 	if (IS_ERR(buf))
 		goto err_free;
 
@@ -241,10 +241,10 @@
 }
 
 static const struct nvl_convert_info info[] = {
-	{ .name = "p",       .tgt_type = NVT_INT, },
-	{ .name = "paged",   .tgt_type = NVT_INT, },
-	{ .name = "m",       .tgt_type = NVT_INT, },
-	{ .name = "preview", .tgt_type = NVT_INT, },
+	{ .name = "p",       .tgt_type = VT_INT, },
+	{ .name = "paged",   .tgt_type = VT_INT, },
+	{ .name = "m",       .tgt_type = VT_INT, },
+	{ .name = "preview", .tgt_type = VT_INT, },
 	{ .name = NULL, },
 };
 
--- a/sidebar.c	Mon Apr 02 15:19:41 2018 -0400
+++ b/sidebar.c	Thu Apr 12 11:40:47 2018 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013-2017 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
+ * Copyright (c) 2013-2018 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
@@ -36,7 +36,7 @@
 
 struct tagcloud_state {
 	unsigned long ntags;
-	struct nvval *cloud;
+	struct val **cloud;
 };
 
 static int __tag_size(int count, int cmin, int cmax)
@@ -57,7 +57,7 @@
 {
 	struct tagcloud_state *state = arg;
 
-	state->cloud = mem_reallocarray(NULL, ntags, sizeof(struct nvval));
+	state->cloud = mem_reallocarray(NULL, ntags, sizeof(struct val *));
 	state->ntags = 0;
 
 	return state->cloud ? 0 : -ENOMEM;
@@ -87,8 +87,7 @@
 	if ((ret = nvl_set_int(tmp, "size", __tag_size(count, cmin, cmax))))
 		goto err;
 
-	state->cloud[state->ntags].type = NVT_NVL;
-	state->cloud[state->ntags].nvl = tmp;
+	state->cloud[state->ntags] = nvl_cast_to_val(tmp);
 	state->ntags++;
 
 	return;
--- a/story.c	Mon Apr 02 15:19:41 2018 -0400
+++ b/story.c	Thu Apr 12 11:40:47 2018 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009-2017 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
+ * Copyright (c) 2009-2018 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
@@ -37,7 +37,7 @@
 static int __load_post(struct req *req, int p, bool preview)
 {
 	struct nvlist *post;
-	struct nvval *val;
+	struct val **val;
 
 	post = get_post(req, p, "title", preview);
 	if (!post) {
@@ -49,12 +49,16 @@
 		return -ENOENT;
 	}
 
-	val = malloc(sizeof(struct nvval));
-	if (!val)
+	val = malloc(sizeof(struct val *));
+	if (!val) {
+		DBG("failed to allocate val array");
+
+		nvl_putref(post);
+
 		return -ENOMEM;
+	}
 
-	val->type = NVT_NVL;
-	val->nvl = post;
+	*val = nvl_cast_to_val(post);
 
 	vars_set_array(&req->vars, "posts", val, 1);
 
--- a/template.y	Mon Apr 02 15:19:41 2018 -0400
+++ b/template.y	Thu Apr 12 11:40:47 2018 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012-2017 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
+ * Copyright (c) 2012-2018 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
@@ -77,7 +77,7 @@
 
 static char *__foreach(struct req *req, const struct nvpair *var, char *tmpl)
 {
-	const struct nvval *items;
+	struct val **items;
 	size_t nitems;
 	size_t i;
 	char *out;
@@ -91,13 +91,14 @@
 	for (i = 0; i < nitems; i++) {
 		vars_scope_push(&req->vars);
 
-		switch (items[i].type) {
-			case NVT_NVL:
-				vars_merge(&req->vars, items[i].nvl);
+		switch (items[i]->type) {
+			case VT_NVL:
+				vars_merge(&req->vars,
+					   val_cast_to_nvl(items[i]));
 				break;
-			case NVT_STR:
+			case VT_STR:
 				vars_set_str(&req->vars, nvpair_name(var),
-					     str_getref(items[i].str));
+					     val_getref_str(items[i]));
 				break;
 			default:
 				//vars_dump(&req->vars);
@@ -150,6 +151,8 @@
 		case VT_CONS:
 		case VT_BOOL:
 		case VT_CHAR:
+		case VT_ARRAY:
+		case VT_NVL:
 			panic("%s called with value of type %d", __func__,
 			      val->type);
 	}
@@ -164,12 +167,12 @@
 	char *ret;
 
 	switch (nvpair_type(var)) {
-		case NVT_STR:
+		case VT_STR:
 			str = nvpair_value_str(var);
 			ret = xstrdup(str_cstr(str));
 			str_putref(str);
 			break;
-		case NVT_INT:
+		case VT_INT:
 			snprintf(buf, sizeof(buf), "%"PRIu64, pair2int(var));
 			ret = xstrdup(buf);
 			break;
@@ -202,10 +205,10 @@
 	}
 
 	switch (nvpair_type(var)) {
-		case NVT_STR:
+		case VT_STR:
 			val = str_cast_to_val(nvpair_value_str(var));
 			break;
-		case NVT_INT:
+		case VT_INT:
 			val = VAL_ALLOC_INT(pair2int(var));
 			break;
 		default:
@@ -261,7 +264,7 @@
 		return 0;
 
 	switch (nvpair_type(var)) {
-		case NVT_INT:
+		case VT_INT:
 			return pair2int(var);
 		default:
 			panic("unexpected nvpair type: %d",
--- a/vars.c	Mon Apr 02 15:19:41 2018 -0400
+++ b/vars.c	Thu Apr 12 11:40:47 2018 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013-2017 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
+ * Copyright (c) 2013-2018 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
@@ -95,7 +95,7 @@
 
 WRAP_SET1(vars_set_str, nvl_set_str, struct str *);
 WRAP_SET1(vars_set_int, nvl_set_int, uint64_t);
-WRAP_SET2(vars_set_array, nvl_set_array, struct nvval *);
+WRAP_SET2(vars_set_array, nvl_set_array, struct val **);
 
 const struct nvpair *vars_lookup(struct vars *vars, const char *name)
 {
--- a/vars.h	Mon Apr 02 15:19:41 2018 -0400
+++ b/vars.h	Thu Apr 12 11:40:47 2018 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013-2017 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
+ * Copyright (c) 2013-2018 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
@@ -38,7 +38,7 @@
 extern void vars_set_str(struct vars *vars, const char *name, struct str *val);
 extern void vars_set_int(struct vars *vars, const char *name, uint64_t val);
 extern void vars_set_array(struct vars *vars, const char *name,
-		struct nvval *val, size_t nval);
+		struct val **vals, size_t nval);
 extern const struct nvpair *vars_lookup(struct vars *vars, const char *name);
 extern struct str *vars_lookup_str(struct vars *vars, const char *name);
 extern uint64_t vars_lookup_int(struct vars *vars, const char *name);