create helper functions

This commit is contained in:
root
2020-07-25 20:25:10 +00:00
parent 077248a00d
commit 042368245d

View File

@@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"io/ioutil"
"log"
"math/rand"
"mime"
"net/http"
@@ -56,6 +57,31 @@ func getMeta(r *http.Request) string {
}
*/
func getStringValue(records map[string]interface{}, key string) string {
if value, ok := records[key]; ok {
if reflect.TypeOf(value) == reflect.TypeOf("string") {
return strings.TrimSpace(value.(string))
}
log.Printf("getStringValue unknown type %s", key)
} else {
log.Printf("getStringValue not found %s", key)
}
return ""
}
func getInt64Value(records map[string]interface{}, key string) int64 {
if value, ok := records[key]; ok {
switch value.(type) {
case int32:
return int64(value.(int32))
case int64:
return int64(value.(int64))
}
}
return 0
}
func hashString(hash []byte, src string) string {
stringToHash := append(hash, []byte(src)...)
hashed := sha256.Sum256(stringToHash)
@@ -427,10 +453,22 @@ func getJSONPostData(r *http.Request) (map[string]interface{}, error) {
} else if strings.HasPrefix(cType, "application/json") {
err = json.Unmarshal(body, &records)
if err != nil {
log.Printf("Error in json decode %s", err)
return nil, err
}
} else if strings.HasPrefix(cType, "application/xml") {
err = json.Unmarshal(body, &records)
if err != nil {
log.Printf("Error in xml/json decode %s", err)
return nil, err
}
} else {
fmt.Printf("ignoring wrong content type: %s", cType)
log.Printf("Ignoring wrong content type: %s", cType)
max_str_len := 200
if len(body) < max_str_len {
max_str_len = len(body)
}
log.Printf("Body[max 200 chars]: %s", body[0:max_str_len])
return nil, nil
}
return records, nil