Categorygithub.com/thecodedproject/gopkg
modulepackage
0.0.0-20240510153301-3b3435a6ae27
Repository: https://github.com/thecodedproject/gopkg.git
Documentation: pkg.go.dev

# README

gopkg

A toolkit for writing code generators in Golang.

Overview

gopkg provides a simple, AST-like structure for representing code declarations - FileContents.

It provides two main methods for interacting with this structure:

  • Parse takes a path to a package and returns the contents of each .go file as a slice of FileContents objects.

  • Generate takes a slice of FileContents objects and writes each object to .go file.

E.g.

  // pckContents has type `[]gopkg.FileContents`
  pkgContents, err := gopkg.Parse("./path/to/my/package")
  // check err

  err = gopkg.Generate(pkgContents)
  // check err

FileContents structure

Contains all package level declaration contained within a .go file, as well as information about the path to the file and it's Golang import path.

type FileContents struct {
	Filepath string
	PackageName string
	PackageImportPath string
	Imports []ImportAndAlias
	Consts []DeclVar
	Vars []DeclVar
	Types []DeclType
	Functions []DeclFunc
}

Examples:

The example_generators folder contains several toy examples of generator implementations.

See also:

servicegen - A generator for creating service interfaces and tests. resourcegen - For generating interfaces required for dependency injection.

TODO:

  • Make linting method to sort FileContents

  • Add DeclFunc.AdditionalImports field, which can be used to add imports at the function level

  • Add DeclFunc.DocString field

  • Add error checks for generation, e.g:

    • Return error when generating a func if:
      • Func name not set
      • Func args unnamed
        • Note; TypeFunc args may be unnamed... should also allow unnamed types on DeclFunc in interfaces
      • Func arg or ret arg is missing a type
    • Return error when generation a file if:
      • Filepath not set
      • PackageName not set
    • Error for TypeDecl when:
      • Name not set
      • Type not set
    • etc...
  • Add linter to sanatize literals which will strip leading and trailing whitespace from all literals:

    • Remove leading whitespace from DeclFunc.BodyTmpl
    • Remove leading and trailing whitespace from all strings in FileContents
    • Maybe remove newlines from things that shouldnt have new lines? (e.g. Decl names?)
  • Consider removing Import field from declaration types - it doesn't seem that this is used at all for generating; It seems like a convenience field but I'm not sure there is a scenario where this is useful

# Packages

No description provided by the author
No description provided by the author
No description provided by the author

# Functions

No description provided by the author
No description provided by the author
CreatePathAndOpen creats all contains directors in filepath if they do not exist and opens a file for writing at filepath.
No description provided by the author
No description provided by the author
GroupStdImportsFirst will move all std imports in all files to their own group at the start of the import list.
No description provided by the author
No description provided by the author
No description provided by the author
PackageImportPath returns the import path of a package given its path in the filesystem `path` may be either a relative or absoute path to the package directory or a file withing the package directory.
Parse parses the file or package at `inputPath` and returns its `FileContents` representation `inputPath` may either be a single golang source file or a directory of golang source files (i.e.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Constants

No description provided by the author

# Structs

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
TODO rename to something more approriate - maybe TypeNamed (or TypeAlias).
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Interfaces

No description provided by the author

# Type aliases

No description provided by the author