Skip to content
This repository has been archived by the owner on Apr 5, 2024. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'origin/feature/credentials-management' …
Browse files Browse the repository at this point in the history
…into feat/#20-multipart
  • Loading branch information
neo7337 committed Dec 21, 2022
2 parents c5b79fe + ace1666 commit 07a18e6
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 59 deletions.
48 changes: 36 additions & 12 deletions codec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ import (
)

const (
JSON = "application/json"
XML = "text/xml"
YAML = "text/yaml"
defaultValidateOnRead = false
defaultValidateBefWrite = false
ValidateOnRead = "ValidateOnRead"
ValidateBefWrite = "ValidateBefWrite"
JSON = "application/json"
XML = "text/xml"
XmlApplicationContentType = "application/xml"
YAML = "text/yaml"
defaultValidateOnRead = false
defaultValidateBefWrite = false
ValidateOnRead = "ValidateOnRead"
ValidateBefWrite = "ValidateBefWrite"
Charset = "charset"
JsonEscapeHTML = "JsonEscapeHTML"
PrettyPrint = "PrettyPrint"
)

// StringEncoder Interface
Expand Down Expand Up @@ -112,19 +116,39 @@ func Get(contentType string, options map[string]interface{}) (c Codec, err error
bc := &BaseCodec{
options: options,
}
switch contentType {
typ := contentType
if strings.Contains(contentType, textutils.SemiColonStr) {
values := strings.Split(contentType, textutils.SemiColonStr)
l := len(values)
for i := 0; i < l; i++ {
val := strings.TrimSpace(values[i])
if i == 0 {
typ = val
} else {
if strings.HasPrefix(val, Charset) {
charset := strings.Split(val, textutils.EqualStr)
if len(charset) == 2 {
//Charset is added to the options but not used by the known json,xml and yaml Read Writers
bc.SetOption(Charset, strings.TrimSpace(charset[1]))
}
}
}
}
}

switch typ {
case JSON:
{
bc.readerWriter = &jsonRW{}
bc.readerWriter = &jsonRW{options: options}

}
case XML:
case XML, XmlApplicationContentType:
{
bc.readerWriter = &xmlRW{}
bc.readerWriter = &xmlRW{options: options}
}
case YAML:
{
bc.readerWriter = &yamlRW{}
bc.readerWriter = &yamlRW{options: options}
}
default:
err = errutils.FmtError("Unsupported contentType %s", contentType)
Expand Down
35 changes: 30 additions & 5 deletions codec/json_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,46 @@ package codec

import (
"encoding/json"
"go.nandlabs.io/commons/codec/validator"
"io"

"go.nandlabs.io/commons/codec/validator"
)

const (
jsonPrettyPrintPrefix = ""
jsonPrettyPrintIndent = " "
)

var structValidator = validator.NewStructValidator()

type jsonRW struct {
options map[string]interface{}
}

func (c *jsonRW) Write(v interface{}, w io.Writer) error {
func (j *jsonRW) Write(v interface{}, w io.Writer) error {
//only utf-8 charset is supported
var escapeHtml = false
var prettyPrint = false
if j.options != nil {
if v, ok := j.options[JsonEscapeHTML]; ok {
escapeHtml = v.(bool)
}

if v, ok := j.options[PrettyPrint]; ok {
prettyPrint = v.(bool)
}

return json.NewEncoder(w).Encode(v)
}
encoder := json.NewEncoder(w)
if prettyPrint {
encoder.SetIndent(jsonPrettyPrintPrefix, jsonPrettyPrintIndent)
}
encoder.SetEscapeHTML(escapeHtml)
return encoder.Encode(v)

}

func (c *jsonRW) Read(r io.Reader, v interface{}) error {
return json.NewDecoder(r).Decode(v)
func (j *jsonRW) Read(r io.Reader, v interface{}) error {
decoder := json.NewDecoder(r)
return decoder.Decode(v)
}
36 changes: 18 additions & 18 deletions codec/xml_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,34 @@ package codec

import (
"encoding/xml"
"errors"
"fmt"
"io"
"io/ioutil"
)

const (
xmlPrettyPrintPrefix = ""
xmlPrettyPrintIndent = " "
)

type xmlRW struct {
options map[string]interface{}
}

func (x *xmlRW) Write(v interface{}, w io.Writer) error {
output, err := xml.Marshal(v)
if err != nil {
return errors.New(fmt.Sprintf("xml marshal error: %d", err))
encoder := xml.NewEncoder(w)
var prettyPrint = false
if x.options != nil {
if v, ok := x.options[PrettyPrint]; ok {
prettyPrint = v.(bool)
}
}
_, errW := w.Write(output)
if errW != nil {
return errW
if prettyPrint {
encoder.Indent(xmlPrettyPrintPrefix, xmlPrettyPrintIndent)
}
return nil
return encoder.Encode(v)

}

func (x *xmlRW) Read(r io.Reader, v interface{}) error {
b, err := ioutil.ReadAll(r)
if err != nil {
return errors.New(fmt.Sprintf("xml input error: %d", err))
}
if errU := xml.Unmarshal(b, v); err != nil {
return errors.New(fmt.Sprintf("xml unmarshal error: %d", errU))
}
return nil
decoder := xml.NewDecoder(r)
return decoder.Decode(v)
}
25 changes: 5 additions & 20 deletions codec/yaml_codec.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,21 @@
package codec

import (
"errors"
"fmt"
"io"
"io/ioutil"

"gopkg.in/yaml.v3"
)

type yamlRW struct {
options map[string]interface{}
}

func (y *yamlRW) Write(v interface{}, w io.Writer) error {
output, err := yaml.Marshal(v)
if err != nil {
return errors.New(fmt.Sprintf("xml marshal error: %d", err))
}
_, errW := w.Write(output)
if errW != nil {
return errW
}
return nil
encoder := yaml.NewEncoder(w)
return encoder.Encode(v)
}

func (y *yamlRW) Read(r io.Reader, v interface{}) error {
b, err := ioutil.ReadAll(r)
if err != nil {
return errors.New(fmt.Sprintf("xml input error: %d", err))
}
if errU := yaml.Unmarshal(b, v); err != nil {
return errors.New(fmt.Sprintf("xml unmarshal error: %d", errU))
}
return nil
decoder := yaml.NewDecoder(r)
return decoder.Decode(v)
}
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ module go.nandlabs.io/commons

go 1.18

require gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
require (
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM=
golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
Expand Down
3 changes: 2 additions & 1 deletion vfs/base_fs.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package vfs

import (
"go.nandlabs.io/commons/ioutils"
"io"
"net/url"

"go.nandlabs.io/commons/ioutils"
)

type BaseVFS struct {
Expand Down
2 changes: 1 addition & 1 deletion vfs/local_file.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package vfs

import (
"go.nandlabs.io/commons/errutils"
"io/fs"
"io/ioutil"
"net/url"
"os"

"go.nandlabs.io/commons/errutils"
"go.nandlabs.io/commons/fsutils"
"go.nandlabs.io/commons/textutils"
)
Expand Down
3 changes: 2 additions & 1 deletion vfs/vfs_manager.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package vfs

import (
"go.nandlabs.io/commons/errutils"
"net/url"
"sync"

"go.nandlabs.io/commons/errutils"
)

var manager Manager
Expand Down

0 comments on commit 07a18e6

Please sign in to comment.