• Stars
    star
    421
  • Rank 102,977 (Top 3 %)
  • Language
    Go
  • License
    MIT License
  • Created almost 9 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Code generation tools for Go.

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

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

Usage

~ $ interfacer -help
Usage of interfacer:
  -all
        Include also unexported methods.
  -as string
        Generated interface name. (default "main.Interface")
  -for string
        Type to generate an interface for.
  -o string
        Output file. (default "-")

Example

  • generate by manually
~ $ interfacer -for os.File -as mock.File
  • generate by go generate
//go:generate interfacer -for os.File -as mock.File -o file_iface.go
~ $ go generate  ./...
  • output
// 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)
}

cmd/structer GoDoc

Generates a struct for a formatted file. Currently supported formats are:

  • CSV

Installation

~ $ go get github.com/rjeczalik/interfaces/cmd/structer

Usage

~ $ structer -help
Usage of structer:
  -as string
        Generated struct name. (default "main.Struct")
  -f string
        Input file. (default "-")
  -o string
        Output file. (default "-")
  -tag string
        Name for a struct tag to add to each field.
  -type string
        Type of the input, overwrites inferred from file name.

Example

~ $ head -2 aws-billing.csv         # first line is a CSV header, second - first line of values
"InvoiceID","PayerAccountId","LinkedAccountId","RecordType","RecordID","BillingPeriodStartDate","BillingPeriodEndDate","InvoiceDate"
"Estimated","123456","","PayerLineItem","5433212345","2016/01/01 00:00:00","2016/01/31 23:59:59","2016/01/21 19:19:06"
~ $ structer -f aws-billing.csv -tag json -as billing.Record
// Created by structer; DO NOT EDIT

package billing

import (
        "strconv"
        "time"
)

// Record is a struct generated from "aws-billing.csv" file.
type Record struct {
        InvoiceID              string    `json:"invoiceID"`
        PayerAccountID         int64     `json:"payerAccountID"`
        LinkedAccountID        string    `json:"linkedAccountID"`
        RecordType             string    `json:"recordType"`
        RecordID               int64     `json:"recordID"`
        BillingPeriodStartDate time.Time `json:"billingPeriodStartDate"`
        BillingPeriodEndDate   time.Time `json:"billingPeriodEndDate"`
        InvoiceDate            time.Time `json:"invoiceDate"`
}

// MarshalCSV encodes r as a single CSV record.
func (r *Record) MarshalCSV() ([]string, error) {
        records := []string{
                r.InvoiceID,
                strconv.FormatInt(r.PayerAccountID, 10),
                r.LinkedAccountID,
                r.RecordType,
                strconv.FormatInt(r.RecordID, 10),
                time.Parse("2006/01/02 15:04:05", r.BillingPeriodStartDate),
                time.Parse("2006/01/02 15:04:05", r.BillingPeriodEndDate),
                time.Parse("2006/01/02 15:04:05", r.InvoiceDate),
        }
        return records, nil
}

// UnmarshalCSV decodes a single CSV record into r.
func (r *Record) UnmarshalCSV(record []string) error {
        if len(record) != 8 {
                return fmt.Errorf("invalud number fields: want 8, got %d", len(record))
        }
        r.InvoiceID = record[0]
        if record[1] != "" {
                if val, err := strconv.ParseInt(record[1], 10, 64); err == nil {
                        r.PayerAccountID = val
                } else {
                        return err
                }
        }
        r.LinkedAccountID = record[2]
        r.RecordType = record[3]
        if record[4] != "" {
                if val, err := strconv.ParseInt(record[4], 10, 64); err == nil {
                        r.RecordID = val
                } else {
                        return err
                }
        }
        if record[5] != "" {
                if val, err := time.Parse("2006/01/02 15:04:05", record[5]); err == nil {
                        r.BillingPeriodStartDate = val
                } else {
                        return err
                }
        }
        if record[6] != "" {
                if val, err := time.Parse("2006/01/02 15:04:05", record[6]); err == nil {
                        r.BillingPeriodEndDate = val
                } else {
                        return err
                }
        }
        if record[7] != "" {
                if val, err := time.Parse("2006/01/02 15:04:05", record[7]); err == nil {
                        r.InvoiceDate = val
                } else {
                        return err
                }
        }
        return nil
}

More Repositories

1

notify

File system event notification library on steroids.
Go
906
star
2

gh

Scriptable server and net/http middleware for GitHub Webhooks.
Go
82
star
3

bin

Looks for Golang executables in $PATH, guesses their origin and updates them or creates $GOPATH workspaces.
Go
45
star
4

refmt

Reformat HCL ⇄ JSON ⇄ YAML.
Go
25
star
5

pkgconfig

Go-centric and GOPATH-aware pkg-config replacement for a use with the cgo tool.
Go
21
star
6

cmd

Handmade tools for day-to-day plumbing.
Go
21
star
7

terraform-aws-scylla

Terraform module for creating Scylla clusters on AWS
HCL
21
star
8

which

Reads the import path and target platform strings from Golang executables.
Go
9
star
9

rapidjson

Yet another rapidjson svn fork, this time with CMake and maintenance patches.
C++
7
star
10

fakerpc

A fake server for recording and mocking HTTP-based RPC services.
Go
6
star
11

fs

Interface, mocks and codegen for the os package.
Go
5
star
12

gsh

SSH for Go on steroids
Go
4
star
13

terraform-aws-scylla-bench

Terraform module for load testing a Scylla cluster with scylla-bench.
HCL
4
star
14

ciexec

A command line tool for executing any CI configuration file (.travis.yml, .pulse.xml) like it was a shell script.
Go
4
star
15

cross

Go cross-compilation unchained.
Shell
3
star
16

wayback

Client for Wayback Availability JSON API.
Go
2
star
17

rw

General purpose io.Reader / io.Writer utility functions.
Go
1
star
18

stat

Statistics and time series toys for the command line.
Go
1
star
19

alpine-peerjs

1
star
20

bigstruct

Go
1
star
21

powermux

Go
1
star
22

zeitdb

Key/value db library for storing log files, backed by a git.
Go
1
star
23

dsn2mycnf

Converts MySQL DSN connection string to a my.cnf configuration file
Go
1
star