Fossil SCM
Added createIfNotExists bool option to /json/wiki/save.
Commit
73e28dd71873b3e0b75adc565eeb265b884ff50c
Parent
30e4ebaa199af33…
1 file changed
+23
-10
+23
-10
| --- src/json_wiki.c | ||
| +++ src/json_wiki.c | ||
| @@ -102,12 +102,16 @@ | ||
| 102 | 102 | ** exists is rather arbitrary - we could just as well create the entry |
| 103 | 103 | ** here if it doesn't already exist. With that, save/create would |
| 104 | 104 | ** become one operation. That said, i expect there are people who |
| 105 | 105 | ** would categorize such behaviour as "being too clever" or "doing too |
| 106 | 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. | |
| 107 | 110 | */ |
| 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){ | |
| 109 | 113 | Blob content = empty_blob; |
| 110 | 114 | cson_value * nameV; |
| 111 | 115 | cson_value * contentV; |
| 112 | 116 | cson_value * emptyContent = NULL; |
| 113 | 117 | cson_value * payV = NULL; |
| @@ -139,20 +143,18 @@ | ||
| 139 | 143 | if(rid){ |
| 140 | 144 | if(createMode){ |
| 141 | 145 | g.json.resultCode = FSL_JSON_E_RESOURCE_ALREADY_EXISTS; |
| 142 | 146 | goto error; |
| 143 | 147 | } |
| 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; | |
| 149 | 151 | } |
| 150 | 152 | |
| 151 | 153 | contentV = json_req_payload_get("content"); |
| 152 | 154 | if( !contentV ){ |
| 153 | - if( createMode ){ | |
| 155 | + if( createMode || (!rid && allowCreateIfExists) ){ | |
| 154 | 156 | contentV = emptyContent = cson_value_new_string("",0); |
| 155 | 157 | }else{ |
| 156 | 158 | g.json.resultCode = FSL_JSON_E_MISSING_ARGS; |
| 157 | 159 | goto error; |
| 158 | 160 | } |
| @@ -183,10 +185,15 @@ | ||
| 183 | 185 | payV = NULL; |
| 184 | 186 | ok: |
| 185 | 187 | if( emptyContent ){ |
| 186 | 188 | /* We have some potentially tricky memory ownership |
| 187 | 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... | |
| 188 | 195 | */ |
| 189 | 196 | cson_value_free(emptyContent); |
| 190 | 197 | } |
| 191 | 198 | return payV; |
| 192 | 199 | |
| @@ -194,19 +201,25 @@ | ||
| 194 | 201 | |
| 195 | 202 | /* |
| 196 | 203 | ** Implementation of /json/wiki/create. |
| 197 | 204 | */ |
| 198 | 205 | static cson_value * json_wiki_create(){ |
| 199 | - return json_wiki_create_or_save(1); | |
| 206 | + return json_wiki_create_or_save(1,0); | |
| 200 | 207 | } |
| 201 | 208 | |
| 202 | 209 | /* |
| 203 | 210 | ** Implementation of /json/wiki/save. |
| 204 | 211 | */ |
| 205 | 212 | 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 | |
| 208 | 221 | } |
| 209 | 222 | |
| 210 | 223 | /* |
| 211 | 224 | ** Implementation of /json/wiki/list. |
| 212 | 225 | */ |
| 213 | 226 |
| --- 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 |