-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove toml, yaml marshler and unmarshaler
- Loading branch information
Showing
8 changed files
with
38 additions
and
243 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,100 +1,17 @@ | ||
// Package encoding provides interfaces and utilities for encoding and decoding data. | ||
package encoding | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"strconv" | ||
"strings" | ||
"unicode/utf8" | ||
) | ||
|
||
// ErrInvalidString is returned when the input string is invalid. | ||
var ErrInvalidString = errors.New("invalid string") | ||
|
||
// Encoder is a function type that encodes a value into bytes. | ||
type Encoder func(any) ([]byte, error) | ||
|
||
// Decoder is a function type that decodes bytes into a provided value. | ||
type Decoder func([]byte, any) error | ||
|
||
// JSONMarshaler is an interface for types that can marshal themselves to JSON. | ||
type JSONMarshaler interface { | ||
MarshalJSON() ([]byte, error) | ||
} | ||
|
||
// JSONUnmarshaler is an interface for types that can unmarshal themselves from JSON. | ||
type JSONUnmarshaler interface { | ||
UnmarshalJSON([]byte) error | ||
} | ||
|
||
// TOMLMarshaler is an interface for types that can marshal themselves to TOML. | ||
type TOMLMarshaler interface { | ||
MarshalTOML() ([]byte, error) | ||
} | ||
|
||
// TOMLUnmarshaler is an interface for types that can unmarshal themselves from TOML. | ||
type TOMLUnmarshaler interface { | ||
UnmarshalTOML([]byte) error | ||
} | ||
|
||
// YAMLMarshaler is an interface for types that can marshal themselves to YAML. | ||
type YAMLMarshaler interface { | ||
MarshalYAML() ([]byte, error) | ||
} | ||
|
||
// YAMLUnmarshaler is an interface for types that can unmarshal themselves from YAML. | ||
type YAMLUnmarshaler interface { | ||
UnmarshalYAML([]byte) error | ||
} | ||
|
||
// UnmarshalString decodes a string from byte slice data. | ||
// It supports both quoted strings and literal strings. | ||
// | ||
// Parameters: | ||
// - data: The byte slice containing the string to unmarshal. | ||
// - literalChar: The character used for literal strings. Use 0 for no literal strings. | ||
// - allowNewline: Whether newlines are allowed in literal strings. | ||
// | ||
// Returns: | ||
// - The unmarshaled string and nil error if successful. | ||
// - An empty string and an error if unmarshaling fails. | ||
func UnmarshalString(data []byte, literalChar byte, allowNewline bool) (string, error) { | ||
data = bytes.TrimSpace(data) | ||
if len(data) < 2 { | ||
return "", errors.New("string too short") | ||
} | ||
|
||
switch data[0] { | ||
case '"': | ||
return unquoteString(data) | ||
case literalChar: | ||
if literalChar == 0 || literalChar == '"' { | ||
break | ||
} | ||
return extractLiteralString(data, literalChar, allowNewline) | ||
} | ||
|
||
return "", ErrInvalidString | ||
} | ||
|
||
func unquoteString(data []byte) (string, error) { | ||
if data[len(data)-1] != '"' { | ||
return "", errors.New("mismatched quotes") | ||
} | ||
return strconv.Unquote(string(data)) | ||
} | ||
|
||
func extractLiteralString(data []byte, literalChar byte, allowNewline bool) (string, error) { | ||
if data[len(data)-1] != literalChar { | ||
return "", errors.New("mismatched quotes") | ||
} | ||
str := string(data[1 : len(data)-1]) | ||
if !allowNewline && strings.ContainsRune(str, '\n') { | ||
return "", errors.New("newlines not allowed in literal string") | ||
} | ||
if !utf8.ValidString(str) { | ||
return "", errors.New("invalid UTF-8 in string") | ||
// Transform decodes data using the provided decoder, then encodes it using the provided encoder. | ||
func Transform(data []byte, encoder Encoder, decoder Decoder) ([]byte, error) { | ||
var v = make(map[string]any) | ||
if err := decoder(data, &v); err != nil { | ||
return nil, err | ||
} | ||
return str, nil | ||
return encoder(v) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.