package codegen

import "github.com/gohugoio/hugo/codegen"

Package codegen contains helpers for code generation.

Index

Types

type Inspector

type Inspector struct {
	ProjectRootDir string
	// contains filtered or unexported fields
}

Inspector provides methods to help code generation. It uses a combination of reflection and source code AST to do the heavy lifting.

func NewInspector

func NewInspector(root string) *Inspector

NewInspector creates a new Inspector given a source root.

func (*Inspector) MethodsFromTypes

func (c *Inspector) MethodsFromTypes(include []reflect.Type, exclude []reflect.Type) Methods

MethodsFromTypes create a method set from the include slice, excluding any method in exclude.

type Method

type Method struct {
	// The interface we extracted this method from.
	Owner reflect.Type

	// String version of the above, on the form PACKAGE.NAME, e.g.
	// page.Page
	OwnerName string

	// Method name.
	Name string

	// Imports needed to satisfy the method signature.
	Imports []string

	// Argument types, including any package prefix, e.g. string, int, interface{},
	// net.Url
	In []string

	// Return types.
	Out []string
}

Method holds enough information about it to recreate it.

func (Method) Declaration

func (m Method) Declaration(receiver string) string

Declaration creates a method declaration (without any body) for the given receiver.

func (Method) DeclarationNamed

func (m Method) DeclarationNamed(receiver string) string

DeclarationNamed creates a method declaration (without any body) for the given receiver with named return values.

func (Method) Delegate

func (m Method) Delegate(receiver, delegate string) string

Delegate creates a delegate call string.

func (Method) String

func (m Method) String() string

type Methods

type Methods []Method

Methods represents a list of methods for one or more interfaces. The order matches the defined order in their source file(s).

func (Methods) Imports

func (m Methods) Imports() []string

Imports returns a sorted list of package imports needed to satisfy the signatures of all methods.

func (Methods) ToMarshalJSON

func (m Methods) ToMarshalJSON(receiver, pkgPath string, excludes ...string) (string, []string)

ToMarshalJSON creates a MarshalJSON method for these methods. Any method name matching any of the regexps in excludes will be ignored.

Source Files

methods.go

Version
v0.144.2 (latest)
Published
Feb 19, 2025
Platform
linux/amd64
Imports
12 packages
Last checked
13 hours ago

Tools for package owners.