Dever v0.0.2
This commit is contained in:
commit
e6e175ebcf
816 files changed
+120349
No files matched your search
@@ -0,0 +1,44 @@
|
||||
#include "dever_ffi.h"
|
||||
#include <yyjson.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
static const char contract[] =
|
||||
#include "manifest.h"
|
||||
;
|
||||
|
||||
static int open_worker(void *context, dever_ffi_slice setting) {
|
||||
(void)context; (void)setting;
|
||||
return DEVER_FFI_OK;
|
||||
}
|
||||
static int invoke(void *context, dever_ffi_slice operation, dever_ffi_slice payload, dever_ffi_output *output) {
|
||||
(void)context;
|
||||
if (operation.size == 7 && memcmp(operation.data,"invalid",7) == 0) {
|
||||
output->size = output->capacity + 1;
|
||||
return DEVER_FFI_OK;
|
||||
}
|
||||
if (operation.size == 4 && memcmp(operation.data,"fail",4) == 0) {
|
||||
const char *error = "{\"error\":\"demo.service.port.Error.Rejected\",\"payload\":{\"message\":\"business rejection\"}}";
|
||||
output->size = strlen(error); memcpy(output->data,error,output->size);
|
||||
return DEVER_FFI_ERROR;
|
||||
}
|
||||
yyjson_doc *input = yyjson_read(payload.data,payload.size,0);
|
||||
if (input == NULL) return DEVER_FFI_FAULT;
|
||||
yyjson_val *root = yyjson_doc_get_root(input);
|
||||
yyjson_mut_doc *result = yyjson_mut_doc_new(NULL);
|
||||
yyjson_mut_val *record = yyjson_mut_obj(result);
|
||||
yyjson_mut_doc_set_root(result,record);
|
||||
yyjson_mut_obj_add_val(result,record,"out",yyjson_val_mut_copy(result,yyjson_obj_get(root,"value")));
|
||||
yyjson_mut_obj_add_val(result,record,"name",yyjson_val_mut_copy(result,yyjson_obj_get(root,"label")));
|
||||
size_t size; char *text = yyjson_mut_write(result,0,&size);
|
||||
int okay = text != NULL && size <= output->capacity;
|
||||
if (okay) { memcpy(output->data,text,size); output->size = size; }
|
||||
free(text); yyjson_mut_doc_free(result); yyjson_doc_free(input);
|
||||
return okay ? DEVER_FFI_OK : DEVER_FFI_FAULT;
|
||||
}
|
||||
static void close_worker(void *context) { (void)context; fputs("closed\n",stderr); }
|
||||
int main(void) {
|
||||
dever_ffi_handler handler = {DEVER_FFI_ABI_V1,NULL,open_worker,invoke,close_worker};
|
||||
dever_ffi_slice manifest = {contract,sizeof(contract)-1};
|
||||
return dever_ffi_serve(&handler,manifest);
|
||||
}
|
||||
Reference in new issue
Block a user