packagemainimport("errors""github.com/extism/go-pdk""github.com/valyala/fastjson")//export hostPrintlnfunchostPrintln(offsetuint64)uint64funcPrintln(textstring){memoryText:=pdk.AllocateString(text)hostPrintln(memoryText.Offset())}varparser=fastjson.Parser{}//export hostMemorySetfunchostMemorySet(offsetuint64)uint64funcMemorySet(keystring,valuestring)(string,error){// Prepare the arguments for the host function// with a JSON string:// {// "key": "name",// "value": "Bob Morane"// }jsonStr:=`{"key":"`+key+`","value":"`+value+`"}`// Copy the string value to the shared memorykeyAndValue:=pdk.AllocateString(jsonStr)// Call host function with the offset of the argumentsoffset:=hostMemorySet(keyAndValue.Offset())// Get result from the shared memory// The host function (hostMemorySet) returns a JSON buffer:// {// "success": "the value associated to the key",// "failure": "error message if error, else empty"// }memoryResult:=pdk.FindMemory(offset)buffResult:=make([]byte,memoryResult.Length())memoryResult.Load(buffResult)JSONData,err:=parser.ParseBytes(buffResult)iferr!=nil{return"",err}iflen(JSONData.GetStringBytes("failure"))==0{returnstring(JSONData.GetStringBytes("success")),nil}else{return"",errors.New(string(JSONData.GetStringBytes("failure")))}}//export hellofunchello()uint64{_,err:=MemorySet("bob","Bob Morane")return0}funcmain(){}
useextism_pdk::*;useserde::{Serialize,Deserialize};usethiserror::Error;extern"C"{fnhostPrintln(ptr: u64)-> u64;}pubfnprintln(text: String){letmutmemory_text: Memory=extism_pdk::Memory::new(text.len());memory_text.store(text);unsafe{hostPrintln(memory_text.offset)};}#[derive(Serialize, Deserialize, Debug)]structMemArguments{pubkey: String,pubvalue: String,}#[derive(Serialize, Deserialize, Debug)]structStringResult{pubsuccess: String,pubfailure: String,}#[derive(Error, Debug)]pubenumMemError{#[error("Store issue")]StoreFailure,#[error("Not found")]NotFound,}extern"C"{fnhostMemorySet(offset: u64)-> u64;}pubfnmemory_set(key: String,value: String)-> Result<String,Error>{// Prepare the arguments for the host function// with a JSON string:// {// "key": "name",// "value": "Bob Morane"// }letrecord=MemArguments{key: key,value: value,};letjson_str: String=serde_json::to_string(&record).unwrap();// Copy the string value to the shared memoryletmutmemory_json_str: Memory=extism_pdk::Memory::new(json_str.len());memory_json_str.store(json_str);// Call host function with the offset of the argumentsletoffset: u64=unsafe{hostMemorySet(memory_json_str.offset)};// Get result from the shared memory// The host function (hostMemorySet) returns a JSON buffer:// {// "success": "the value associated to the key",// "failure": "error message if error, else empty"// }letmemory_result: Memory=extism_pdk::Memory::find(offset).unwrap();letjson_string:String=memory_result.to_string().unwrap();letresult: StringResult=serde_json::from_str(&json_string).unwrap();ifresult.failure.is_empty(){returnOk(result.success);}else{returnErr(MemError::StoreFailure.into());}}#[plugin_fn]pubfnhello(_: String)-> FnResult<u64>{matchmemory_set("bob".to_string(),"Bob Morane".to_string()){Ok(value)=>println("๐ฆ saved value: ".to_string()+&value),Err(error)=>println("๐ก error: ".to_string()+&error.to_string()),}Ok(0)}
hostMemoryGet
hostMemoryGet: get value with a key from a memory map.
packagemainimport("errors""github.com/extism/go-pdk""github.com/valyala/fastjson")//export hostPrintlnfunchostPrintln(offsetuint64)uint64funcPrintln(textstring){memoryText:=pdk.AllocateString(text)hostPrintln(memoryText.Offset())}varparser=fastjson.Parser{}//export hostMemoryGetfunchostMemoryGet(offsetuint64)uint64funcMemoryGet(keystring)(string,error){// Copy argument to memorymemoryKey:=pdk.AllocateString(key)// Call the host functionoffset:=hostMemoryGet(memoryKey.Offset())// Get result (the value associated to the key) from shared memory// The host function (hostMemoryGet) returns a JSON buffer:// {// "success": "the value associated to the key",// "failure": "error message if error, else empty"// }memoryValue:=pdk.FindMemory(offset)buffer:=make([]byte,memoryValue.Length())memoryValue.Load(buffer)JSONData,err:=parser.ParseBytes(buffer)iferr!=nil{return"",err}iflen(JSONData.GetStringBytes("failure"))==0{returnstring(JSONData.GetStringBytes("success")),nil}else{return"",errors.New(string(JSONData.GetStringBytes("failure")))}}//export hellofunchello()uint64{value,err:=MemoryGet("bob")iferr!=nil{Println("๐ก ouch! "+err.Error())}else{Println("value: "+value)}return0}funcmain(){}
useextism_pdk::*;useserde::{Serialize,Deserialize};usethiserror::Error;extern"C"{fnhostPrintln(ptr: u64)-> u64;}pubfnprintln(text: String){letmutmemory_text: Memory=extism_pdk::Memory::new(text.len());memory_text.store(text);unsafe{hostPrintln(memory_text.offset)};}#[derive(Serialize, Deserialize, Debug)]structStringResult{pubsuccess: String,pubfailure: String,}#[derive(Error, Debug)]pubenumMemError{#[error("Store issue")]StoreFailure,#[error("Not found")]NotFound,}extern"C"{fnhostMemoryGet(offset: u64)-> u64;}pubfnmemory_get(key: String)-> Result<String,Error>{// Copy argument to memoryletmutmemory_key: Memory=extism_pdk::Memory::new(key.len());memory_key.store(key);// Call the host functionletoffset: u64=unsafe{hostMemoryGet(memory_key.offset)};// Get result (the value associated to the key) from shared memory// The host function (hostMemoryGet) returns a JSON buffer:// {// "success": "the value associated to the key",// "failure": "error message if error, else empty"// }letmemory_result: Memory=extism_pdk::Memory::find(offset).unwrap();letjson_string:String=memory_result.to_string().unwrap();letresult: StringResult=serde_json::from_str(&json_string).unwrap();ifresult.failure.is_empty(){returnOk(result.success);}else{returnErr(MemError::NotFound.into());}}#[plugin_fn]pubfnhello(_: String)-> FnResult<u64>{matchmemory_get("bob".to_string()){Ok(value)=>println("๐ฆ value: ".to_string()+&value),Err(error)=>println("๐ก error: ".to_string()+&error.to_string()),}matchmemory_get("sam".to_string()){Ok(value)=>println("๐ฆ value: ".to_string()+&value),Err(error)=>println("๐ก error: ".to_string()+&error.to_string()),}Ok(0)}