iox

package
v0.0.0-...-6d088c4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 23, 2025 License: BSD-3-Clause Imports: 13 Imported by: 2

Documentation

Index

Examples

Constants

View Source
const (
	AcceptEncoding      = "Accept-Encoding"
	AcceptEncodingValue = "gzip, deflate, br"
	ContentEncoding     = "Content-Encoding"

	GzipEncoding     = "gzip"
	BrotliEncoding   = "br"
	DeflateEncoding  = "deflate"
	CompressEncoding = "compress"
	NoneEncoding     = "none"

	ApplicationGzip    = "application/x-gzip"
	ApplicationBrotli  = "application/x-br"
	ApplicationDeflate = "application/x-deflate"
)
View Source
const (
	CwdVariable = "[cwd]"
)

Variables

This section is empty.

Functions

func Decode

func Decode(buf []byte, h http.Header) ([]byte, error)

Decode - decode a []byte

func DirFS

func DirFS(dir string) fs.FS
Example
dir := "file:///c:/Users/markb/GitHub/core/iox/ioxtest"
fileSystem := DirFS(dir)
fs.WalkDir(fileSystem, ".", func(path string, d fs.DirEntry, err error) error {
	if err != nil {
		log.Fatal(err)
	}
	if path != "." {

		buf, err1 := fs.ReadFile(fileSystem, path)
		fmt.Printf("test: fs.ReadFile() -> [err:%v] [%v] %v\n", err1, path, buf != nil)
	}
	return nil
})
Output:

test: fs.ReadFile() -> [err:<nil>] [address1.json] true
test: fs.ReadFile() -> [err:<nil>] [address2.json] true
test: fs.ReadFile() -> [err:<nil>] [address3.json] true
test: fs.ReadFile() -> [err:<nil>] [hello-world.gz] true
test: fs.ReadFile() -> [err:<nil>] [hello-world.txt] true
test: fs.ReadFile() -> [err:<nil>] [status-504.json] true
test: fs.ReadFile() -> [err:<nil>] [test-response.gz] true
test: fs.ReadFile() -> [err:<nil>] [test-response.txt] true
test: fs.ReadFile() -> [err:<nil>] [test-response2.gz] true
test: fs.ReadFile() -> [err:<nil>] [test-response2.txt] true

func EncodeContent

func EncodeContent(h http.Header, content []byte) (buff []byte, encoding string, err error)
Example
package main

import (
	"encoding/json"
	"fmt"
	"net/http"
)

type echo struct {
	Method string      `json:"method"`
	Host   string      `json:"host"`
	Url    string      `json:"url"`
	Header http.Header `json:"header"`
}

func main() {
	h := make(http.Header)
	h.Add("x-request-id", "1234-56-7890")
	e := echo{
		Method: http.MethodGet,
		Host:   "localhost",
		Url:    "https://www.google.com/search?q=golang",
		Header: h,
	}
	content, err1 := json.Marshal(e)
	if err1 != nil {
		fmt.Printf("test: json.Marshal() -> [err:%v]\n", err1)
	}
	//buf, encoding, err := EncodeContent(nil, content)
	//fmt.Printf("test: EncodeContent-Nil-Header() -> [buf:%v] [encoding:%v] [err:%v]\n", len(buf), encoding, err)

	h2 := make(http.Header)
	buf, encoding, err := EncodeContent(h2, content)
	fmt.Printf("test: EncodeContent-No-Accept-Encoding() -> [buf:%v] [encoding:%v] [err:%v]\n", len(buf), encoding, err)

	h2.Add(AcceptEncoding, AcceptEncodingValue)
	buf, encoding, err = EncodeContent(h2, content)
	ct := http.DetectContentType(buf)
	fmt.Printf("test: EncodeContent() -> [buf:%v] [encoding:%v] [content-type:%v] [err:%v]\n", len(buf), encoding, ct, err)

}
Output:

test: EncodeContent-No-Accept-Encoding() -> [buf:0] [encoding:] [err:<nil>]
test: EncodeContent() -> [buf:144] [encoding:gzip] [content-type:application/x-gzip] [err:<nil>]

func FileName

func FileName(uri any) string

FileName - return the OS correct file name from a URI

func Mount

func Mount(fs embed.FS)

func ParseMap

func ParseMap(buf []byte) (map[string]string, error)

func ReadAll

func ReadAll(body io.Reader, h http.Header) ([]byte, error)

ReadAll - read the body with a Status

func ReadFile

func ReadFile(uri any) ([]byte, error)

ReadFile - read a file with a Status

Example
s := status504
buf, status := ReadFile(s)
fmt.Printf("test: ReadFile(%v) -> [type:%v] [buf:%v] [status:%v]\n", s, reflect.TypeOf(s), len(buf), status)

s = address1Url
buf, status = ReadFile(s)
fmt.Printf("test: ReadFile(%v) -> [type:%v] [buf:%v] [status:%v]\n", s, reflect.TypeOf(s), len(buf), status)

s = status504
u := parseRaw(s)
buf, status = ReadFile(u.String())
fmt.Printf("test: ReadFile(%v) -> [type:%v] [buf:%v] [status:%v]\n", s, reflect.TypeOf(u), len(buf), status)

s = address1Url
u = parseRaw(s)
buf, status = ReadFile(u.String())
fmt.Printf("test: ReadFile(%v) -> [type:%v] [buf:%v] [status:%v]\n", s, reflect.TypeOf(u), len(buf), status)
Output:

test: ReadFile(file://[cwd]/ioxtest/status-504.json) -> [type:string] [buf:82] [status:<nil>]
test: ReadFile(file://[cwd]/ioxtest/address1.json) -> [type:string] [buf:68] [status:<nil>]
test: ReadFile(file://[cwd]/ioxtest/status-504.json) -> [type:*url.URL] [buf:82] [status:<nil>]
test: ReadFile(file://[cwd]/ioxtest/address1.json) -> [type:*url.URL] [buf:68] [status:<nil>]

func ReadFileWithEncoding

func ReadFileWithEncoding(uri string, h http.Header) ([]byte, error)

ReadFileWithEncoding - read a file with a possible encoding and a Status

Example
buf, status := ReadFileWithEncoding(helloWorldGzip, nil)
fmt.Printf("test: ReadFileWithEncoding(\"%v\",nil) -> [buf:%v] [status:%v]\n", helloWorldGzip, string(buf), status)

h := make(http.Header)
h.Set(ContentEncoding, GzipEncoding)
buf, status = ReadFileWithEncoding(helloWorldGzip, h)
fmt.Printf("test: ReadFileWithEncoding(\"%v\",h) -> [buf:%v] [status:%v]\n", helloWorldGzip, string(buf), status)

buf, status = ReadFileWithEncoding(helloWorldTxt, nil)
fmt.Printf("test: ReadFileWithEncoding(\"%v\",nil) -> [buf:%v] [status:%v]\n", helloWorldTxt, string(buf), status)
Output:

test: ReadFileWithEncoding("file://[cwd]/ioxtest/hello-world.gz",nil) -> [buf:Hello World!!] [status:<nil>]
test: ReadFileWithEncoding("file://[cwd]/ioxtest/hello-world.gz",h) -> [buf:Hello World!!] [status:<nil>]
test: ReadFileWithEncoding("file://[cwd]/ioxtest/hello-world.txt",nil) -> [buf:Hello World!!] [status:<nil>]

func ReadMap

func ReadMap(uri any) (map[string]string, error)
Example
m, err := ReadMap(serviceConfigTxt)

fmt.Printf("test: ReadMap() -> [err:%v] %v\n", err, m)
Output:

test: ReadMap() -> [err:<nil>] map[ORIGIN-INSTANCE-ID:instance-id ORIGIN-REGION:region ORIGIN-SERVICE:service ORIGIN-SUB-ZONE:sub-zone ORIGIN-ZONE:zone RUNTIME_ENV_KEY:RT_ENV egress-listener-addr:0.0.0.0:8087 ingress-listener-addr:0.0.0.0:8086]

func ReadMapT

func ReadMapT[T any](uri any) (t T, err error)

func ValidateUri

func ValidateUri(uri string) error

func WriteMap

func WriteMap(m map[string]string) string
Example
m := make(map[string]string)
m["ORIGIN-INSTANCE-ID"] = "instance-id"
m["ORIGIN-REGION"] = "region"
m["ORIGIN-SERVICE"] = "service"
m["ORIGIN-SUB-ZONE"] = "sub-zone"
m["ORIGIN-ZONE"] = "zone"
fmt.Printf("test: WriteMap() -> %v\n", WriteMap(m))
Output:

test: WriteMap() -> ORIGIN-SUB-ZONE : sub-zone
ORIGIN-ZONE : zone
ORIGIN-INSTANCE-ID : instance-id
ORIGIN-REGION : region
ORIGIN-SERVICE : service

func ZipFile

func ZipFile(uri string) error
Example
status := ZipFile(helloWorldTxt)

fmt.Printf("test: ZipFile(\"\") -> [status:%v]\n", status)
Output:

test: ZipFile("") -> [status:<nil>]

Types

type EncodingReader

type EncodingReader interface {
	io.ReadCloser
}

func NewEncodingReader

func NewEncodingReader(r io.Reader, h http.Header) (EncodingReader, error)

func NewGzipReader

func NewGzipReader(r io.Reader) (EncodingReader, error)

func NewIdentityReader

func NewIdentityReader(r io.Reader) EncodingReader

NewIdentityReader - The default (identity) encoding; the use of no transformation whatsoever

type EncodingWriter

type EncodingWriter interface {
	io.WriteCloser
	ContentEncoding() string
}

func NewEncodingWriter

func NewEncodingWriter(w io.Writer, h http.Header) (EncodingWriter, error)

func NewGzipWriter

func NewGzipWriter(w io.Writer) EncodingWriter

func NewIdentityWriter

func NewIdentityWriter(w io.Writer) EncodingWriter

NewIdentityWriter - The default (identity) encoding; the use of no transformation whatsoever

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL