Fossil SCM

Added createIfNotExists bool option to /json/wiki/save.

stephan 2011-09-28 22:04 UTC json
Commit 73e28dd71873b3e0b75adc565eeb265b884ff50c
1 file changed +23 -10
+23 -10
--- src/json_wiki.c
+++ src/json_wiki.c
@@ -102,12 +102,16 @@
102102
** exists is rather arbitrary - we could just as well create the entry
103103
** here if it doesn't already exist. With that, save/create would
104104
** become one operation. That said, i expect there are people who
105105
** would categorize such behaviour as "being too clever" or "doing too
106106
** much automatically" (and i would likely agree with them).
107
+**
108
+** If allowCreateIfExists is true then this function will allow a new
109
+** page to be created even if createMode is false.
107110
*/
108
-static cson_value * json_wiki_create_or_save(char createMode){
111
+static cson_value * json_wiki_create_or_save(char createMode,
112
+ char allowCreateIfExists){
109113
Blob content = empty_blob;
110114
cson_value * nameV;
111115
cson_value * contentV;
112116
cson_value * emptyContent = NULL;
113117
cson_value * payV = NULL;
@@ -139,20 +143,18 @@
139143
if(rid){
140144
if(createMode){
141145
g.json.resultCode = FSL_JSON_E_RESOURCE_ALREADY_EXISTS;
142146
goto error;
143147
}
144
- }else{
145
- if(!createMode){
146
- g.json.resultCode = FSL_JSON_E_RESOURCE_NOT_FOUND;
147
- goto error;
148
- }
148
+ }else if(!allowCreateIfExists){
149
+ g.json.resultCode = FSL_JSON_E_RESOURCE_NOT_FOUND;
150
+ goto error;
149151
}
150152
151153
contentV = json_req_payload_get("content");
152154
if( !contentV ){
153
- if( createMode ){
155
+ if( createMode || (!rid && allowCreateIfExists) ){
154156
contentV = emptyContent = cson_value_new_string("",0);
155157
}else{
156158
g.json.resultCode = FSL_JSON_E_MISSING_ARGS;
157159
goto error;
158160
}
@@ -183,10 +185,15 @@
183185
payV = NULL;
184186
ok:
185187
if( emptyContent ){
186188
/* We have some potentially tricky memory ownership
187189
here, which is why we handle emptyContent separately.
190
+
191
+ This is, in fact, overkill because cson_value_new_string("",0)
192
+ actually returns a shared singleton instance (i.e. doesn't
193
+ allocate), but that is a cson implementation detail which i
194
+ don't want leaking into this code...
188195
*/
189196
cson_value_free(emptyContent);
190197
}
191198
return payV;
192199
@@ -194,19 +201,25 @@
194201
195202
/*
196203
** Implementation of /json/wiki/create.
197204
*/
198205
static cson_value * json_wiki_create(){
199
- return json_wiki_create_or_save(1);
206
+ return json_wiki_create_or_save(1,0);
200207
}
201208
202209
/*
203210
** Implementation of /json/wiki/save.
204211
*/
205212
static cson_value * json_wiki_save(){
206
- /* FIXME: add GET/POST.payload bool option createIfNotExists. */
207
- return json_wiki_create_or_save(0);
213
+ char const createIfNotExists = json_getenv_bool("createIfNotExists",0);
214
+#if 0
215
+ return json_wiki_create_or_save(0,createIfNotExists);
216
+#else
217
+ cson_value * v = json_wiki_create_or_save(0,createIfNotExists);
218
+ cson_object * o = cson_value_get_object(v);
219
+ cson_object_set(o,"createIfNotExists", cson_value_new_bool(createIfNotExists));
220
+#endif
208221
}
209222
210223
/*
211224
** Implementation of /json/wiki/list.
212225
*/
213226
--- src/json_wiki.c
+++ src/json_wiki.c
@@ -102,12 +102,16 @@
102 ** exists is rather arbitrary - we could just as well create the entry
103 ** here if it doesn't already exist. With that, save/create would
104 ** become one operation. That said, i expect there are people who
105 ** would categorize such behaviour as "being too clever" or "doing too
106 ** much automatically" (and i would likely agree with them).
 
 
 
107 */
108 static cson_value * json_wiki_create_or_save(char createMode){
 
109 Blob content = empty_blob;
110 cson_value * nameV;
111 cson_value * contentV;
112 cson_value * emptyContent = NULL;
113 cson_value * payV = NULL;
@@ -139,20 +143,18 @@
139 if(rid){
140 if(createMode){
141 g.json.resultCode = FSL_JSON_E_RESOURCE_ALREADY_EXISTS;
142 goto error;
143 }
144 }else{
145 if(!createMode){
146 g.json.resultCode = FSL_JSON_E_RESOURCE_NOT_FOUND;
147 goto error;
148 }
149 }
150
151 contentV = json_req_payload_get("content");
152 if( !contentV ){
153 if( createMode ){
154 contentV = emptyContent = cson_value_new_string("",0);
155 }else{
156 g.json.resultCode = FSL_JSON_E_MISSING_ARGS;
157 goto error;
158 }
@@ -183,10 +185,15 @@
183 payV = NULL;
184 ok:
185 if( emptyContent ){
186 /* We have some potentially tricky memory ownership
187 here, which is why we handle emptyContent separately.
 
 
 
 
 
188 */
189 cson_value_free(emptyContent);
190 }
191 return payV;
192
@@ -194,19 +201,25 @@
194
195 /*
196 ** Implementation of /json/wiki/create.
197 */
198 static cson_value * json_wiki_create(){
199 return json_wiki_create_or_save(1);
200 }
201
202 /*
203 ** Implementation of /json/wiki/save.
204 */
205 static cson_value * json_wiki_save(){
206 /* FIXME: add GET/POST.payload bool option createIfNotExists. */
207 return json_wiki_create_or_save(0);
 
 
 
 
 
 
208 }
209
210 /*
211 ** Implementation of /json/wiki/list.
212 */
213
--- src/json_wiki.c
+++ src/json_wiki.c
@@ -102,12 +102,16 @@
102 ** exists is rather arbitrary - we could just as well create the entry
103 ** here if it doesn't already exist. With that, save/create would
104 ** become one operation. That said, i expect there are people who
105 ** would categorize such behaviour as "being too clever" or "doing too
106 ** much automatically" (and i would likely agree with them).
107 **
108 ** If allowCreateIfExists is true then this function will allow a new
109 ** page to be created even if createMode is false.
110 */
111 static cson_value * json_wiki_create_or_save(char createMode,
112 char allowCreateIfExists){
113 Blob content = empty_blob;
114 cson_value * nameV;
115 cson_value * contentV;
116 cson_value * emptyContent = NULL;
117 cson_value * payV = NULL;
@@ -139,20 +143,18 @@
143 if(rid){
144 if(createMode){
145 g.json.resultCode = FSL_JSON_E_RESOURCE_ALREADY_EXISTS;
146 goto error;
147 }
148 }else if(!allowCreateIfExists){
149 g.json.resultCode = FSL_JSON_E_RESOURCE_NOT_FOUND;
150 goto error;
 
 
151 }
152
153 contentV = json_req_payload_get("content");
154 if( !contentV ){
155 if( createMode || (!rid && allowCreateIfExists) ){
156 contentV = emptyContent = cson_value_new_string("",0);
157 }else{
158 g.json.resultCode = FSL_JSON_E_MISSING_ARGS;
159 goto error;
160 }
@@ -183,10 +185,15 @@
185 payV = NULL;
186 ok:
187 if( emptyContent ){
188 /* We have some potentially tricky memory ownership
189 here, which is why we handle emptyContent separately.
190
191 This is, in fact, overkill because cson_value_new_string("",0)
192 actually returns a shared singleton instance (i.e. doesn't
193 allocate), but that is a cson implementation detail which i
194 don't want leaking into this code...
195 */
196 cson_value_free(emptyContent);
197 }
198 return payV;
199
@@ -194,19 +201,25 @@
201
202 /*
203 ** Implementation of /json/wiki/create.
204 */
205 static cson_value * json_wiki_create(){
206 return json_wiki_create_or_save(1,0);
207 }
208
209 /*
210 ** Implementation of /json/wiki/save.
211 */
212 static cson_value * json_wiki_save(){
213 char const createIfNotExists = json_getenv_bool("createIfNotExists",0);
214 #if 0
215 return json_wiki_create_or_save(0,createIfNotExists);
216 #else
217 cson_value * v = json_wiki_create_or_save(0,createIfNotExists);
218 cson_object * o = cson_value_get_object(v);
219 cson_object_set(o,"createIfNotExists", cson_value_new_bool(createIfNotExists));
220 #endif
221 }
222
223 /*
224 ** Implementation of /json/wiki/list.
225 */
226

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button