From 0cc7e90c98d65a014604910df07fa5c1d2937159 Mon Sep 17 00:00:00 2001 From: k33g Date: Sat, 1 Jul 2023 07:49:23 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=A6=20updates=20MDK=20for=20v0.0.5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Taskfile.yml | 7 +++--- capsule.dk.go | 4 ++- docs/index.md | 1 + hostfunc.http.go | 8 ------ models.go | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 71 insertions(+), 12 deletions(-) diff --git a/Taskfile.yml b/Taskfile.yml index 5b1e85c..5a49cd4 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -22,9 +22,10 @@ tasks: env: #TAG: "v0.0.1" #TAG: "v0.0.2" - TAG: "v0.0.4" # current release - #TAG: "v0.0.5" # it will be the next release - + #TAG: "v0.0.4" + TAG: "v0.0.5" # current release + #TAG: "v0.0.6" # it will be the next release + cmds: - echo "📦 Generating release..." - git add . diff --git a/capsule.dk.go b/capsule.dk.go index ff7fadc..852df6b 100644 --- a/capsule.dk.go +++ b/capsule.dk.go @@ -1,7 +1,9 @@ // Package capsule SDK for WASM plugin package capsule -import "errors" +import ( + "errors" +) const isFailure = rune('F') const isSuccess = rune('S') diff --git a/docs/index.md b/docs/index.md index 04e8066..bae970e 100644 --- a/docs/index.md +++ b/docs/index.md @@ -1,6 +1,7 @@ # Capsule Module SDK !!! info "What's new?" + - `v0.0.5`: ✨ Add 2 new helpers: `GetHeaders` (transform a JSON string to a map[string]string) and `SetHeaders` (transform a map[string]string to a JSON string) - `v0.0.4`: ✨ Add the `Success` and `Failure` functions (public functions to call `success` and `failure`) and the `StringifyHTTPResponse` function - `v0.0.3`: ✨ Encode `retValue.TextBody` to avoid special characters in jsonString - `v0.0.2`: ✨ Redis support diff --git a/hostfunc.http.go b/hostfunc.http.go index 0a2980b..936836b 100644 --- a/hostfunc.http.go +++ b/hostfunc.http.go @@ -38,7 +38,6 @@ func HTTP(request HTTPRequest) (HTTPResponse, error) { body="" } - // TODO: we can only have one Body... var jsonHTTPRequest string /* @@ -57,8 +56,6 @@ func HTTP(request HTTPRequest) (HTTPResponse, error) { jsonHTTPRequest = `{"JSONBody":{},"TextBody":"` + body + `","URI":"` + request.URI + `","Headers":` + request.Headers + `,"Method":"` + request.Method + `"}` } - - jsonHTTPRequestPosition, jsonHTTPRequestSize := getBufferPosSize([]byte(jsonHTTPRequest)) // This will be use to get the response from the host @@ -126,8 +123,3 @@ func HTTP(request HTTPRequest) (HTTPResponse, error) { }, nil } -/* Documentation - - - - */ diff --git a/models.go b/models.go index 005f7cc..ab65aef 100644 --- a/models.go +++ b/models.go @@ -1,5 +1,7 @@ package capsule +import "strings" + // HTTPRequest is the data of the http request type HTTPRequest struct { Body string @@ -18,3 +20,64 @@ type HTTPResponse struct { Headers string StatusCode int } + +// strSeparator is use to create a string from a slice +// ex: "Content-Type":"application/json; charset=utf-8","PRIVATE-TOKEN":"glpat-mmysLDdMx532zxHgsgzF" +const strSeparator = "," +// fieldSeparator is used to create a slice from a map +// ex: "PRIVATE-TOKEN":"glpat-mmysLDdMx532zxHgsgzF" +const fieldSeparator = ":" +// quote is used to create a slice from a map +// to add double quote before and after a fieldname or a value +const quote = "\"" + + +// createStringFromSlice creates a string from a slice of strings +func createStringFromSlice(strSlice []string, separator string) string { + return strings.Join(strSlice[:], separator) +} + +// createSliceFromMap creates a slice of strings from a maps of strings +func createSliceFromMap(strMap map[string]string) []string { + var strSlice []string + for field, value := range strMap { + strSlice = append(strSlice, quote+field+quote+fieldSeparator+quote+value+quote) + } + return strSlice +} + +// createSliceFromString creates a slice of string from a string +func createSliceFromString(str string, separator string) []string { + return strings.Split(str, separator) +} +// createMapFromSlice creates a map from a slice of string +func createMapFromSlice(strSlice []string, separator string, remove string) map[string]string { + strMap := make(map[string]string) + for _, item := range strSlice { + + item = strings.ReplaceAll(item, remove, "") + res := strings.Split(item, separator) + + if len(res) >= 2 { // for example: "https://gitlab.com" and separator is ":" + strMap[res[0]] = strings.Join(res[1:], separator) + } else { + if len(res) > 1 { + strMap[res[0]] = res[1] + } + } + } + return strMap +} + +// GetHeaders return a map of headers from a string of headers +// transform a JSON string to a map[string]string +func GetHeaders(headers string) map[string]string { + return createMapFromSlice(createSliceFromString(headers, ","), ":", `"`) +} + +// SetHeaders return a string of headers from a map of headers +// transform a map[string]string to a JSON string +func SetHeaders(headers map[string]string) string { + return "{"+createStringFromSlice(createSliceFromMap(headers), strSeparator)+"}" +} +