Code generation tools for Go.

Overview

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 get github.com/rjeczalik/interfaces/cmd/interfacer

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
}
Issues
  • Marking comment should be formed

    Marking comment should be formed "Code generated by xxx"

    ref: https://github.com/github/linguist/blob/master/lib/linguist/generated.rb#L299-L304

    Many of tools to generate go code are marking code with "Code generated by xxx". It's de facto standard.

    opened by kyoh86 5
  • `interfacer` doesn't preserve variadic arguments

    `interfacer` doesn't preserve variadic arguments

    Input struct: "github.com/gocql/gocql".Session

    Output:

    // Session is an interface generated for "github.com/gocql/gocql".Session.
    type Session interface {
    // ...
    	Query(string, []interface{}) *gocql.Query
    // ...
    }
    

    Corresponding function signature (see github.com/gocql/gocql/session.go:

    func (s *Session) Query(stmt string, values ...interface{}) *Query
    
    bug 
    opened by jinnovation 5
  • Panic when generating interface for redis client

    Panic when generating interface for redis client

    I love the idea of this project! Unfortunately this is what I hit when I tried it out:

    > interfacer -for \"github.com/go-redis/redis\".Client -as mock.redis

    panic: internal: t=*types.Signature, orig=*types.Signature
    
    goroutine 1 [running]:
    panic(0x217c40, 0x429cf5550)
    	$GOROOT/src/runtime/panic.go:500 +0x1a1
    github.com/rjeczalik/interfaces.(*Type).setFromType(0x430661878, 0x367400, 0x425b33290, 0x0, 0x367400, 0x425b33290)
    	$GOPATH/src/github.com/rjeczalik/interfaces/type.go:65 +0x350
    github.com/rjeczalik/interfaces.newType(0x42596d1d0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
    	$GOPATH/src/github.com/rjeczalik/interfaces/type.go:29 +0x8b
    github.com/rjeczalik/interfaces.buildInterfaceForPkg(0x4204b20b0, 0x430661eb0, 0x7fff5fbfe4e1, 0x19, 0x42582d818, 0x1, 0x0)
    	$GOPATH/src/github.com/rjeczalik/interfaces/interface.go:140 +0x41c
    github.com/rjeczalik/interfaces.buildInterface(0x430661eb0, 0x420433dd8, 0x95de4, 0x7fff5fbfe4e1, 0x21, 0x266bba)
    	$GOPATH/src/github.com/rjeczalik/interfaces/interface.go:84 +0x2c0
    github.com/rjeczalik/interfaces.NewWithOptions(0x430661eb0, 0x21, 0x4204a8060, 0x0, 0x0, 0x4204a2000)
    	$GOPATH/src/github.com/rjeczalik/interfaces/interface.go:45 +0x156
    main.main()
    	$GOPATH/src/github.com/rjeczalik/interfaces/cmd/interfacer/main.go:73 +0xf0
    

    Am I using it wrong or is there a bug here?

    bug 
    opened by jenspinney 3
  • Fix fixup function for case that import path of package has different…

    Fix fixup function for case that import path of package has different…

    Initial problem: Parameter of struct function is imported. This imported package folder is different to its package name.

    Example: interfacer -for github.com/minio/minio-go/v6.Client -as contentservice.MinioClient -o minio_client_interface.go

    opened by hanofzelbri 1
  • Invalid code generated for a method returning map

    Invalid code generated for a method returning map

    interfacer generates invalid response.

    $ cat bar.go
    package foo
    
    import (
            "net/url"
    )
    
    type Bar struct {
    }
    
    func (b *Bar) Map() map[string]url.URL {
            return nil
    }
    $ interfacer -as foo.Foo -for ..Bar
    // Created by interfacer; DO NOT EDIT
    
    package foo
    
    import (
            "net/url"
    )
    
    // Foo is an interface generated for "..Bar".
    type Foo interface {
            Map() map[string]net/url.URL  // This's invalid response
    }```
    opened by kyoh86 1
  • fix fixup when include other package struct

    fix fixup when include other package struct

    When I define method that contain other package struct slice, generated invalid Field interface

    eg)

    // github.com/rjeczalik/interfaces/test/sample
    package sample
    
    import (
        "github.com/rjeczalik/interfaces/test/hoge"
    )
    
    type Sample struct{}
    
    func (s *Sample) Hoges() []*hoge.Hoge { return []*hoge.Hoge{&hoge.Hoge{}} }
    
    interfacer -for 'github.com/rjeczalik/interfaces/test/hoge'.Sample -as hoge.Sampler -o sample.gen.go
    

    generated

    // github.com/rjeczalik/interfaces/test/sample.gen.go
    package sample
    
    type Sampler interface {
      Hoges() *[]github.com/rjeczalik/interfaces/test/hoge.Hoge
    }
    

    refs: https://github.com/rjeczalik/interfaces/issues/13


    so I fixed fixup method

    opened by Konboi 1
  • When method's response are other package struct slice, generated invalid interface.

    When method's response are other package struct slice, generated invalid interface.

    I use interfacer and it's very useful but I happen this case.

    I define struct like this

    package foo
    
    import (
       "github.com/example/hoge"
    )
    
    type Foo struct {}
    
    func (f *Foo) Hoge1() error { return nil }
    
    func (f *Foo) Hoge2() *hoge.Hoge { return &hoge.Hoge{} }
    
    func (f *Foo) Hoge3() []*hoge.Hoge { return []*hoge.Hoge{...} }
    

    I generate interface using by interfaces like this.

    interfacer -for github.com/Konboi/foo.Foo  -as foo.Fooer -o foo.gen.go
    

    but generated file is like this

    package foo
    
    import (
       "github.com/example/hoge"
    )
    
    type Fooer interface {
        Hoge1() error
        Hoge2() *hoge.Hoge
        Hoge3() []*github.com/example/hoge.Hoge
    }
    

    but I think it should be

    package foo
    
    import (
       "github.com/example/hoge"
    )
    
    type Fooer interface {
        Hoge1() error
        Hoge2() *hoge.Hoge
        Hoge3() []*hoge.Hoge
    }
    

    is it correct ??

    please check it.

    opened by Konboi 1
  • How do you install?

    How do you install?

    I tried the normal go get github.com/rjeczalik/interfaces and it downloads but it doesn't install anything. A go build in src/github.com/rjeczalik/interfaces doesn't seem to do anything either.

    opened by decibel 1
  • Update main.go

    Update main.go

    To convey to humans and machine tools that code is generated, generated source should have a line that matches the following regular expression (in Go syntax):

    ^// Code generated .* DO NOT EDIT\.$
    
    opened by plan97 0
  • Update main.go

    Update main.go

    To convey to humans and machine tools that code is generated, generated source should have a line that matches the following regular expression (in Go syntax):

    ^// Code generated .* DO NOT EDIT\.$
    
    opened by plan97 0
  • Update main.go

    Update main.go

    To convey to humans and machine tools that code is generated, generated source should have a line that matches the following regular expression (in Go syntax):

    ^// Code generated .* DO NOT EDIT\.$
    
    opened by plan97 0
  • command not found: interfacer

    command not found: interfacer

    interfacer zsh: command not found: interface

    I am getting this error after installing go get github.com/rjeczalik/interfaces/cmd/interfacer

    Also I am not seeing any binary in my go bin directory /Users/khanakia/go/bin

    opened by khanakia 0
  • Interfacer generates invalid code when package name contains hyphen

    Interfacer generates invalid code when package name contains hyphen

    We've tried to run interfacer against github.com/PagerDuty/go-pagerduty.Client. When one of the function arguments is a package name, it generates:

    	ListAuditRecordsPaginated(context.Context, pagerduty.ListAuditRecordsOptions, func(go-pagerduty.AuditRecord) bool) ([]pagerduty.AuditRecord, error)
    

    however, go-pagerduty isn't a valid package name. Would it be possible to pass args to tell interfacer which package name to use in this scenario?

    opened by paprikati 0
  • Is interfacer supposed to be slow?

    Is interfacer supposed to be slow?

    We have noticed that go generate takes awfully long since we started using interfacer. Compared to mockgen, interfacer seems to take several orders of magnitude longer to run per file.

    Is this by design/unavoidable or is this a bug?

    performance 
    opened by OscarVanL 1
  • feature request: Generate multiple interfaces into the same file

    feature request: Generate multiple interfaces into the same file

    I want to generate interfaces over the GCP Datastore library. This has four structs: Client, Transaction, Iterator, and Commit.

    Ideally I'd like to use interfacer to generate these all into the same target file, perhaps -for and -as could take multiple arguments to a single -o output file.

    Unless I am mistaken, I believe this is not currently possible. It would be a nice feature :)

    enhancement 
    opened by OscarVanL 0
  • Is it possible to include parameter names when generating an interface using `interfacer`

    Is it possible to include parameter names when generating an interface using `interfacer`

    Is it possible to include parameter names when generating an interface using interfacer?

    Currently, all the function parameter names are stripped so

     FooBar(ctx context.Context, eventContext string, includePrivate bool, includeGeo bool)
    

    becomes

    FooBar(ctx, string, bool, bool)
    

    which is much harder to work with.

    enhancement 
    opened by paprikati 1
Releases(v0.1.1)
  • v0.1.0(May 1, 2019)

    This is the first release of cmd/interfacer and cmd/structer tools.

    In particular it contains newly added support for variadic functions (#7)

    Full Changelog

    f267b5c Bump Go versions and use '.x' to always get latest patch versions 531c3d1 Compatible with go:generate 7c0d99a Initial commit b25e70e Merge pull request #11 from wweir/master d3085a4 Merge pull request #3 from rjeczalik/codegen-fix 7521fc0 Merge pull request #5 from rjeczalik/collect-fix 3c33efd Merge pull request #6 from rjeczalik/redis-mock 185b87d Merge pull request #8 from DenLilleMand/AddInstallationToReadme 60bd913 Merge pull request #9 from HaraldNordgren/go_versions fb4d0dd Update README.md 9464624 Update README.md d39b221 Update README.md f997e44 Update README.md c245cb4 Update README.md 628d589 Update README.md 7b997aa Update README.md 5d238bb Update README.md 9c00e9e add AppVeyor configuration 91cf4b7 add Travis CI configuration 0dfcbf2 cmd/interfacer: add inline comment for generated interface 3c1dcdd cmd/interfacer: fix typo 73581fb cmd/interfacers: codegen an interface from named type 73fbf6e cmd/structer: add fmt as dep 7ebe384 cmd/structer: add structer tool f3f3741 cmd/structer: add time.Time support 57e06fa cmd/structer: fix csv codegen 7e9e3ba cmd/structer: generate MarshalCSV and UnmarshalCSV methods 74e631e fixes for go vet 3a7f84a interfaces: add build test for commands 4eb1478 interfaces: add func member support 83fe0b7 interfaces: add goreleaser configuration 7b5da75 interfaces: add installation section to readme 4dd7b3e interfaces: add module files 0ae4d6d interfaces: add possibility to filter out unexported methods f88ea5b interfaces: bump appveyor version 6df3dd8 interfaces: bump travis version 2726d2e interfaces: disable building for tip 073b0dc interfaces: do not sync on output writers 1894ccb interfaces: first working pkg version c36d88e interfaces: fix Travis CI badge link 2a41193 interfaces: fix for go vet aa1146e interfaces: fix for go vet 7c0f171 interfaces: fix net.Interface test 1cce18e interfaces: fixes for goreportcard c7e9451 interfaces: fixup import paths as type definitions 23f0df0 interfaces: inline doc 4231c16 interfaces: list deps limitation 5315a69 interfaces: rework build test to support modules 8824828 interfaces: scan for methods embedded fields only 566c0f8 interfaces: support variadic arguments in func signatures cb05eaf interfaces: update Go version 3b6a9b0 interfaces: update go/types dep 61d2318 interfaces: use vet for go toolchain 6ae9366 remove coverage tools from Travis CI configuration 2167a4c update Travis CI configuration

    Source code(tar.gz)
    Source code(zip)
    checksums.txt(408 bytes)
    interfaces_0.1.0_Darwin_386.tar.gz(3.30 MB)
    interfaces_0.1.0_Darwin_x86_64.tar.gz(3.43 MB)
    interfaces_0.1.0_Linux_386.tar.gz(3.17 MB)
    interfaces_0.1.0_Linux_x86_64.tar.gz(3.30 MB)
Owner
Rafal Jeczalik
Rafal Jeczalik
Type-driven code generation for Go

What’s this? gen is a code-generation tool for Go. It’s intended to offer generics-like functionality on your types. Out of the box, it offers offers

Matt Sherman 1.4k Aug 7, 2022
Code Generation for Functional Programming, Concurrency and Generics in Golang

goderive goderive derives mundane golang functions that you do not want to maintain and keeps them up to date. It does this by parsing your go code fo

Walter Schulze 997 Jul 31, 2022
Source code of Liteloader Tools

LiteLoader Tools This repository store the source code of some LiteLoader Tools Prebuilt Binary see /bin folder Image2Binary [Golang] convert Image(jp

LiteLoaderBDS Developers 3 Apr 26, 2022
Singlestore event analytics - Evaluation of sortable ID generation schemes

Singlestore event analytics - Evaluation of sortable ID generation schemes

Carl Sverre 1 Jan 25, 2022
Clean-Swift source and test code auto-generator. It can save you time typing 500-600 lines of code.

Clean-Swift source & test code auto generator Overview Run Output Basic Usage make config.yaml target_project_name: Miro // target project name copyri

David Ha 20 Apr 13, 2022
Code generator that generates boilerplate code for a go http server

http-bootstrapper This is a code generator that uses go templates to generate a bootstrap code for a go http server. Usage Generate go http server cod

Jijo Thomas John 1 Nov 20, 2021
A directory of hardware related libs, tools, and tutorials for Go

Go + hardware This repo is a directory of tools, packages and tutorials to let you introduce Go in your hardware projects. Why Go? Go can target platf

Jaana Dogan 1.3k Jul 24, 2022
gqlanalysis makes easy to develop static analysis tools for GraphQL in Go.

gqlanalysis gqlanalysis defines the interface between a modular static analysis for GraphQL in Go. gqlanalysis is inspired by go/analysis. gqlanalysis

null 34 Apr 11, 2022
gopkg is a universal utility collection for Go, it complements offerings such as Boost, Better std, Cloud tools.

gopkg is a universal utility collection for Go, it complements offerings such as Boost, Better std, Cloud tools. Table of Contents Introduction

Bytedance Inc. 738 Aug 2, 2022
Little Bug Bounty & Hacking Tools⚔️

Little Bug Bounty & Hacking Tools ⚔️

gilfoyle97 150 Jul 30, 2022
common tools for golang

utils common tools for golang package main

daqNext 0 Dec 27, 2021
Go tools sourcecode read and customize

Go Tools This subrepository holds the source for various packages and tools that support the Go programming language. Some of the tools, godoc and vet

fumeboy 0 Oct 24, 2021
A fully Go userland with Linux bootloaders! u-root can create a one-binary root file system (initramfs) containing a busybox-like set of tools written in Go.

u-root Description u-root embodies four different projects. Go versions of many standard Linux tools, such as ls, cp, or shutdown. See cmds/core for m

null 2k Aug 1, 2022
Mackerel SLI/SLO tools

shimesaba For SRE to operate and monitor services using Mackerel. Description shimesaba is a tool for tracking SLO/ErrorBudget using Mackerel as an SL

ikeda-masashi 9 Jul 5, 2022
Like tools/cmd/stringer with bitmask features

Bitmasker Bitmasker is a tool used to automate the creation of helper methods when dealing with bitmask-type constant flags. Given the name of an unsi

Go Curses 0 Nov 25, 2021
Functional tools in Go 1.18 using newly introduced generics

functools functools is a simple Go library that brings you your favourite functi

Rakeeb Hossain 176 Jul 29, 2022
Generic-based collection tools

go-collection go collection is a tool implemented using generic, it can help you process slice/map data quickly and easily convert between them. Note:

王杨 10 Jun 21, 2022
Versatile Go code generator.

Generis Versatile Go code generator. Description Generis is a lightweight code preprocessor adding the following features to the Go language : Generic

SenseLogic 34 May 20, 2022
Golang source code parsing, usage like reflect package

gotype Golang source code parsing, usage like reflect package English 简体中文 Usage API Documentation Examples License Pouch is licensed under the MIT Li

null 44 Jul 14, 2022