项目作者: rjeczalik

项目描述 :
Code generation tools for Go.
高级语言: Go
项目地址: git://github.com/rjeczalik/interfaces.git
创建时间: 2015-12-06T00:04:50Z
项目社区:https://github.com/rjeczalik/interfaces

开源协议:MIT License

下载


interfaces GoDoc Build Status Build status

Code generation tools for Go’s interfaces.

Tools available in this repository:

cmd/interfacer GoDoc

Generates an interface for a named type.

Installation

  1. ~ $ go install github.com/rjeczalik/interfaces/cmd/interfacer@latest

Usage

  1. ~ $ interfacer -help
  1. Usage of interfacer:
  2. -all
  3. Include also unexported methods.
  4. -as string
  5. Generated interface name. (default "main.Interface")
  6. -for string
  7. Type to generate an interface for.
  8. -o string
  9. Output file. (default "-")

Example

  • generate by manually
    1. ~ $ interfacer -for os.File -as mock.File
  • generate by go generate
    1. //go:generate interfacer -for os.File -as mock.File -o file_iface.go
    1. ~ $ go generate ./...
  • output
    ```go
    // Created by interfacer; DO NOT EDIT

package mock

import (
“os”
)

// File is an interface generated for “os”.File.
type File interface {
Chdir() error
Chmod(os.FileMode) error
Chown(int, int) error
Close() error
Fd() uintptr
Name() string
Read([]byte) (int, error)
ReadAt([]byte, int64) (int, error)
Readdir(int) ([]os.FileInfo, error)
Readdirnames(int) ([]string, error)
Seek(int64, int) (int64, error)
Stat() (os.FileInfo, error)
Sync() error
Truncate(int64) error
Write([]byte) (int, error)
WriteAt([]byte, int64) (int, error)
WriteString(string) (int, error)
}

  1. ### cmd/structer [![GoDoc](https://godoc.org/github.com/rjeczalik/interfaces/cmd/structer?status.png)](https://godoc.org/github.com/rjeczalik/interfaces/cmd/structer)
  2. Generates a struct for a formatted file. Currently supported formats are:
  3. - CSV
  4. *Installation*
  5. ```bash
  6. ~ $ go get github.com/rjeczalik/interfaces/cmd/structer

Usage

  1. ~ $ structer -help
  1. Usage of structer:
  2. -as string
  3. Generated struct name. (default "main.Struct")
  4. -f string
  5. Input file. (default "-")
  6. -o string
  7. Output file. (default "-")
  8. -tag string
  9. Name for a struct tag to add to each field.
  10. -type string
  11. Type of the input, overwrites inferred from file name.

Example

  1. ~ $ head -2 aws-billing.csv # first line is a CSV header, second - first line of values
  1. "InvoiceID","PayerAccountId","LinkedAccountId","RecordType","RecordID","BillingPeriodStartDate","BillingPeriodEndDate","InvoiceDate"
  2. "Estimated","123456","","PayerLineItem","5433212345","2016/01/01 00:00:00","2016/01/31 23:59:59","2016/01/21 19:19:06"
  1. ~ $ structer -f aws-billing.csv -tag json -as billing.Record
  1. // Created by structer; DO NOT EDIT
  2. package billing
  3. import (
  4. "strconv"
  5. "time"
  6. )
  7. // Record is a struct generated from "aws-billing.csv" file.
  8. type Record struct {
  9. InvoiceID string `json:"invoiceID"`
  10. PayerAccountID int64 `json:"payerAccountID"`
  11. LinkedAccountID string `json:"linkedAccountID"`
  12. RecordType string `json:"recordType"`
  13. RecordID int64 `json:"recordID"`
  14. BillingPeriodStartDate time.Time `json:"billingPeriodStartDate"`
  15. BillingPeriodEndDate time.Time `json:"billingPeriodEndDate"`
  16. InvoiceDate time.Time `json:"invoiceDate"`
  17. }
  18. // MarshalCSV encodes r as a single CSV record.
  19. func (r *Record) MarshalCSV() ([]string, error) {
  20. records := []string{
  21. r.InvoiceID,
  22. strconv.FormatInt(r.PayerAccountID, 10),
  23. r.LinkedAccountID,
  24. r.RecordType,
  25. strconv.FormatInt(r.RecordID, 10),
  26. time.Parse("2006/01/02 15:04:05", r.BillingPeriodStartDate),
  27. time.Parse("2006/01/02 15:04:05", r.BillingPeriodEndDate),
  28. time.Parse("2006/01/02 15:04:05", r.InvoiceDate),
  29. }
  30. return records, nil
  31. }
  32. // UnmarshalCSV decodes a single CSV record into r.
  33. func (r *Record) UnmarshalCSV(record []string) error {
  34. if len(record) != 8 {
  35. return fmt.Errorf("invalud number fields: want 8, got %d", len(record))
  36. }
  37. r.InvoiceID = record[0]
  38. if record[1] != "" {
  39. if val, err := strconv.ParseInt(record[1], 10, 64); err == nil {
  40. r.PayerAccountID = val
  41. } else {
  42. return err
  43. }
  44. }
  45. r.LinkedAccountID = record[2]
  46. r.RecordType = record[3]
  47. if record[4] != "" {
  48. if val, err := strconv.ParseInt(record[4], 10, 64); err == nil {
  49. r.RecordID = val
  50. } else {
  51. return err
  52. }
  53. }
  54. if record[5] != "" {
  55. if val, err := time.Parse("2006/01/02 15:04:05", record[5]); err == nil {
  56. r.BillingPeriodStartDate = val
  57. } else {
  58. return err
  59. }
  60. }
  61. if record[6] != "" {
  62. if val, err := time.Parse("2006/01/02 15:04:05", record[6]); err == nil {
  63. r.BillingPeriodEndDate = val
  64. } else {
  65. return err
  66. }
  67. }
  68. if record[7] != "" {
  69. if val, err := time.Parse("2006/01/02 15:04:05", record[7]); err == nil {
  70. r.InvoiceDate = val
  71. } else {
  72. return err
  73. }
  74. }
  75. return nil
  76. }