csvreader

package module
v0.0.0-...-9abf819 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2023 License: MIT Imports: 12 Imported by: 0

README

csvreader

Jason idea_wj@163.com 简单的csv格式文件解析到struct工具

2022-01-27 注:为满足自身需要,在 原作者:zhnxin 项目 基础上做了改动:

  1. 当header中字段首尾存在空格时,去除空格
  2. value值,去除开头和结尾的空格
  3. header中的字段必须全为小写,结构体随意,但名称必须和header中字段名称保持一致

安装

go get github.com/bitxx/csvreader

简单用法

NOTE: 默认情况下,csv 文件的首行会被当作header处理。

hosname,ip
redis,172.17.0.2
mariadb,172.17.0.3
type Info struct{
    Hostname string
    IP string
}

//struct slice
infos := []Info{}
_ = csvreader.New().UnMarshalFile("file.csv",&infos)
body,_ := json.Marshal(infos)
fmt.Println(string(body))

//point slice
infos = []*Info{}
_ = csvreader.New().UnMarshalFile("file.csv",&infos)
body,_ := json.Marshal(infos)
fmt.Println(string(body))

NOTE: 如果 csv 文件首行不包含header,可以使用 WithHeader([]string) 来指定header。

_ = csvreader.New().WithHeader([]string{"hostname","ip"}).UnMarshalFile("file.csv",&infos)

自定义parster

就像枚举类型(enum),偶尔会遇到这种需要实现自定义转换过程的情况。例子如下

type NetProtocol uint32
const(
    NetProtocol_TCP NetProtocol = iota
    NetProtocol_UDP
    NetProtocol_DCCP
    NetProtocol_SCTP
)

type ServiceInfo struct{
    Host string
    Port string
    Protocol NetProtocol
}

直接使用原始的类型来编辑csv文件,十分不便。这时就需要实现自定义parser。

type CsvMarshal interface {
    FromString(string) error
}
func (p *NetProtocol)FromString(protocol string) error{
switch strings.ToLower(protocol){
    case "tcp":
        *p = NetProtocol_TCP
    case "udp":
        *p = NetProtocol_UDP
    case "dccp":
        *p = NetProtocol_DCCP
    case "sctp":
        *p = NetProtocol_SCTP
    default:
        return fmt.Errorf("unknown protocoal:%s",protocol)
    }
    return nil
}

另外一个例子 reader_test.go TestCustom

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToSnake

func ToSnake(s string, screaming bool) string

Types

type CsvMarshal

type CsvMarshal interface {
	FromString(string) error
}

type Decoder

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

func New

func New() *Decoder

func (*Decoder) TrimSpace

func (d *Decoder) TrimSpace(s string) string

func (*Decoder) UnMarshal

func (d *Decoder) UnMarshal(reader *csv.Reader, bean interface{}) error

func (*Decoder) UnMarshalBytes

func (d *Decoder) UnMarshalBytes(body []byte, bean interface{}) error

func (*Decoder) UnMarshalFile

func (d *Decoder) UnMarshalFile(path string, bean interface{}) error

func (*Decoder) WithCheck

func (d *Decoder) WithCheck(keys []string) *Decoder

func (*Decoder) WithHeader

func (d *Decoder) WithHeader(header []string) *Decoder

Jump to

Keyboard shortcuts

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