Skip to content

Commit

Permalink
📦 updates MDK for v0.0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
k33g committed Jul 1, 2023
1 parent 9f5e76f commit 0cc7e90
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 12 deletions.
7 changes: 4 additions & 3 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 .
Expand Down
4 changes: 3 additions & 1 deletion capsule.dk.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Package capsule SDK for WASM plugin
package capsule

import "errors"
import (
"errors"
)

const isFailure = rune('F')
const isSuccess = rune('S')
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 0 additions & 8 deletions hostfunc.http.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ func HTTP(request HTTPRequest) (HTTPResponse, error) {
body=""
}


// TODO: we can only have one Body...
var jsonHTTPRequest string
/*
Expand All @@ -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
Expand Down Expand Up @@ -126,8 +123,3 @@ func HTTP(request HTTPRequest) (HTTPResponse, error) {
}, nil
}

/* Documentation
*/
63 changes: 63 additions & 0 deletions models.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package capsule

import "strings"

// HTTPRequest is the data of the http request
type HTTPRequest struct {
Body string
Expand All @@ -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)+"}"
}

0 comments on commit 0cc7e90

Please sign in to comment.