项目作者: ianlopshire

项目描述 :
Encoding and decoding for fixed-width formatted data
高级语言: Go
项目地址: git://github.com/ianlopshire/go-fixedwidth.git
创建时间: 2017-11-15T21:05:44Z
项目社区:https://github.com/ianlopshire/go-fixedwidth

开源协议:MIT License

下载


fixedwidth GoDoc Report card Go Cover

Package fixedwidth provides encoding and decoding for fixed-width formatted Data.

go get github.com/ianlopshire/go-fixedwidth

Usage

Struct Tags

The struct tag schema schema used by fixedwidth is: fixed:"{startPos},{endPos},[{alignment},[{padChar}]]"1.

The startPos and endPos arguments control the position within a line. startPos and endPos must both be positive integers greater than 0. Positions start at 1. The interval is inclusive.

The alignment argument controls the alignment of the value within it’s interval. The valid options are default2, right, left, and none. The alignment is optional and can be omitted.

The padChar argument controls the character that will be used to pad any empty characters in the interval after writing the value. The default padding character is a space. The padChar is optional and can be omitted.

Fields without tags are ignored.

Encode

  1. // define some data to encode
  2. people := []struct {
  3. ID int `fixed:"1,5"`
  4. FirstName string `fixed:"6,15"`
  5. LastName string `fixed:"16,25"`
  6. Grade float64 `fixed:"26,30"`
  7. Age uint `fixed:"31,33"`
  8. Alive bool `fixed:"34,39"`
  9. }{
  10. {1, "Ian", "Lopshire", 99.5, 20, true},
  11. }
  12. data, err := Marshal(people)
  13. if err != nil {
  14. log.Fatal(err)
  15. }
  16. fmt.Printf("%s", data)
  17. // Output:
  18. // 1 Ian Lopshire 99.5020 true

Decode

  1. // define the format
  2. var people []struct {
  3. ID int `fixed:"1,5"`
  4. FirstName string `fixed:"6,15"`
  5. LastName string `fixed:"16,25"`
  6. Grade float64 `fixed:"26,30"`
  7. Age uint `fixed:"31,33"`
  8. Alive bool `fixed:"34,39"`
  9. Github bool `fixed:"40,41"`
  10. }
  11. // define some fixed-with data to parse
  12. data := []byte("" +
  13. "1 Ian Lopshire 99.50 20 false f" + "\n" +
  14. "2 John Doe 89.50 21 true t" + "\n" +
  15. "3 Jane Doe 79.50 22 false F" + "\n" +
  16. "4 Ann Carraway 79.59 23 false T" + "\n")
  17. err := Unmarshal(data, &people)
  18. if err != nil {
  19. log.Fatal(err)
  20. }
  21. fmt.Printf("%+v\n", people[0])
  22. fmt.Printf("%+v\n", people[1])
  23. fmt.Printf("%+v\n", people[2])
  24. fmt.Printf("%+v\n", people[3])
  25. // Output:
  26. //{ID:1 FirstName:Ian LastName:Lopshire Grade:99.5 Age:20 Alive:false Github:false}
  27. //{ID:2 FirstName:John LastName:Doe Grade:89.5 Age:21 Alive:true Github:true}
  28. //{ID:3 FirstName:Jane LastName:Doe Grade:79.5 Age:22 Alive:false Github:false}
  29. //{ID:4 FirstName:Ann LastName:Carraway Grade:79.59 Age:23 Alive:false Github:true}

It is also possible to read data incrementally

  1. decoder := fixedwidth.NewDecoder(bytes.NewReader(data))
  2. for {
  3. var element myStruct
  4. err := decoder.Decode(&element)
  5. if err == io.EOF {
  6. break
  7. }
  8. if err != nil {
  9. log.Fatal(err)
  10. }
  11. handle(element)
  12. }

UTF-8, Codepoints, and Multibyte Characters

fixedwidth supports encoding and decoding fixed-width data where indices are expressed in
unicode codepoints and not raw bytes. The data must be UTF-8 encoded.

  1. decoder := fixedwidth.NewDecoder(strings.NewReader(data))
  2. decoder.SetUseCodepointIndices(true)
  3. // Decode as usual now
  1. buff := new(bytes.Buffer)
  2. encoder := fixedwidth.NewEncoder(buff)
  3. encoder.SetUseCodepointIndices(true)
  4. // Encode as usual now

Alignment Behavior

Alignment Encoding Decoding
default Field is left aligned The padding character is trimmed from both right and left of value
left Field is left aligned The padding character is trimmed from right of value
right Field is right aligned The padding character is trimmed from left of value
none Field is left aligned The padding character is not trimmed from value. Useful for nested structs.

Notes

  1. {} indicates an argument. [] indicates and optional segment ^
  2. The default alignment is similar to left but has slightly different behavior required to maintain backwards compatibility ^

Licence

MIT