struct2csv

package module
v0.0.0-...-76379c8 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2023 License: MIT Imports: 12 Imported by: 0

README

struct2csv

a lib convert go struct to csv file

the struct like this

type testStruct struct {
    A1 int
    A2 bool
    B1 []int
    B2 []struct {
        B21 int
        B22 string
    }
    B3 []*struct {
        B31 []*int
    }
}

after the transformation, the header like follow

"/A1"
"/A2"
"/B1/0"
"/B1/1"
"/B2/0/B21"
"/B2/0/B22"
"/B3/0/B31/0"
"/B3/0/B31/1"

speciality

  • support a struct or map slice convert to csv
  • supports header mapping to custom types
  • support protobuf struct

how to use

type struct User {
    ID   string
    Name string
}

func Converter(input []User) error {
    conv, err := struct2csv.NewStructConverter(struct2csv.NewHeaderOriginalStringConv(), struct2csv.WithResultCap(len(input)))
    if err != nil {
        return err
    }

    results, err := conv.Convert(input)
    if err != nil {
        return error
    }
    defer results.Reset()

    f, err := os.Create("test.csv")
    if err != nil {
        return err
    }
    defer f.Close()
	
    return struct2csv.NewCSVWriter(f).WriteCSV(results)
}

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CSVWriter

type CSVWriter struct {
	*csv.Writer
	// contains filtered or unexported fields
}

CSVWriter writes CSV data.

func NewCSVWriter

func NewCSVWriter(w io.Writer) *CSVWriter

NewCSVWriter returns new CSVWriter

func (*CSVWriter) WriteCSV

func (w *CSVWriter) WriteCSV(results *KVs) error

WriteCSV writes CSV data.

func (*CSVWriter) WriteMapping

func (w *CSVWriter) WriteMapping(kvs *KVs) error

WriteMapping write the csv head mapping file

type HeaderAutoIncrementConv

type HeaderAutoIncrementConv struct {
	// contains filtered or unexported fields
}

func NewHeaderAutoIncrementConv

func NewHeaderAutoIncrementConv() *HeaderAutoIncrementConv

func (*HeaderAutoIncrementConv) ConvertHeader

func (h *HeaderAutoIncrementConv) ConvertHeader(s string) KeyType

type HeaderConverter

type HeaderConverter interface {
	ConvertHeader(string) KeyType
}

HeaderConverter will convert the path string of the original struct field to the custom of KeyType

type HeaderOriginalStringConv

type HeaderOriginalStringConv struct {
}

func NewHeaderOriginalStringConv

func NewHeaderOriginalStringConv() *HeaderOriginalStringConv

func (*HeaderOriginalStringConv) ConvertHeader

func (h *HeaderOriginalStringConv) ConvertHeader(s string) KeyType

type KVs

type KVs struct {
	// contains filtered or unexported fields
}

func NewKVs

func NewKVs(size, preMappingSize int) *KVs

func (*KVs) GetEncodedSortHeader

func (kvs *KVs) GetEncodedSortHeader() []string

func (*KVs) GetMapping

func (kvs *KVs) GetMapping() map[string]KeyType

func (*KVs) GetSortMappingValues

func (kvs *KVs) GetSortMappingValues() []KeyType

func (*KVs) GetUnEncodedSortHeader

func (kvs *KVs) GetUnEncodedSortHeader() []string

func (*KVs) Reset

func (kvs *KVs) Reset()

func (*KVs) SetEncodedSortHeader

func (kvs *KVs) SetEncodedSortHeader(header []string)

type KeyAutoIncrementID

type KeyAutoIncrementID uint64

func (KeyAutoIncrementID) Int

func (k KeyAutoIncrementID) Int() uint64

func (KeyAutoIncrementID) String

func (k KeyAutoIncrementID) String() string

type KeyString

type KeyString string

func (KeyString) Int

func (k KeyString) Int() uint64

func (KeyString) String

func (k KeyString) String() string

type KeyType

type KeyType interface {
	String() string // get encode string
	Int() uint64    // get the key map to uint64
}

type KeyValue

type KeyValue struct {
	// contains filtered or unexported fields
}

KeyValue records the struct's key(containing the path) and its value consider the performance problems caused by map keys of the interface type uint64 is used as the map key, and wrapped by KeyValue. call Set and Get of map's read and write. KeyType can be extended to different types, then both performance and code extensibility are considered

func (*KeyValue) Get

func (t *KeyValue) Get(k KeyType) (interface{}, bool)

func (*KeyValue) Len

func (t *KeyValue) Len() int

func (*KeyValue) Set

func (t *KeyValue) Set(k KeyType, v interface{})

type Option

type Option func(opts *Options)

func WithIsObjArray

func WithIsObjArray(p bool) Option

func WithOptions

func WithOptions(options Options) Option

func WithResultCap

func WithResultCap(p int) Option

func WithRowSize

func WithRowSize(p int) Option

func WithStrBuilderCap

func WithStrBuilderCap(p int) Option

type Options

type Options struct {
	// contains filtered or unexported fields
}

type PathBuilder

type PathBuilder struct {
	// contains filtered or unexported fields
}

PathBuilder is a sequence of Token.

func NewPathBuilder

func NewPathBuilder(growSize int) PathBuilder

func (PathBuilder) AppendString

func (p PathBuilder) AppendString(token string) PathBuilder

AppendString appends the token.

func (PathBuilder) Clone

func (p PathBuilder) Clone(growSize int) PathBuilder

Clone returns a duplicate of the PathBuilder.

func (PathBuilder) String

func (p PathBuilder) String() string

String returns full path string.

type StructConverter

type StructConverter struct {
	// contains filtered or unexported fields
}

func NewStructConverter

func NewStructConverter(headerConv HeaderConverter, opts ...Option) (*StructConverter, error)

NewStructConverter a converter can convert struct to csv kv

func (*StructConverter) Convert

func (s *StructConverter) Convert(data interface{}) (*KVs, error)

Convert converts Struct to CSV key value

type WrapperValue

type WrapperValue struct {
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

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