2026-10-20 15:55:50 +02:00
2026-10-20 15:24:00 +02:00
2026-10-20 15:42:46 +02:00
2026-10-20 15:42:46 +02:00
2024-05-20 16:51:24 +02:00
2026-10-20 15:24:00 +02:00
2026-10-20 15:24:00 +02:00
2026-10-20 15:27:55 +02:00
2026-10-20 15:24:00 +02:00
2026-10-20 15:24:00 +02:00
2026-10-20 15:27:55 +02:00
2026-10-20 15:27:55 +02:00
2026-10-20 15:27:55 +02:00
2026-10-20 15:27:55 +02:00
2024-05-20 16:51:24 +02:00
2026-10-20 15:24:00 +02:00
2026-10-20 15:44:46 +02:00

CodeQL Coverage GoDoc GitHub Stars

gocontemplate

Go Contemplate

A code generation helper.

Wrapper library around golang.org/x/tools/go/packages that loads Go packages and narrows them down to the types you ask for, plus every type those types depend on — across package boundaries.

Typical use: a code generator that needs the full type graph behind a few root types (e.g. to emit TypeScript, JSON Schema or docs) without walking the AST and imports itself.

Installation

go get github.com/foomo/gocontemplate

Usage

package main

import (
  "context"
  "fmt"

  "github.com/foomo/gocontemplate/pkg/contemplate"
)

func main() {
  ctpl, err := contemplate.Load(context.Background(), &contemplate.Config{
    Packages: []*contemplate.PackageConfig{
      {
        Path:  "github.com/foomo/sesamy-go/event",
        Types: []string{"PageView"},
      },
    },
  })
  if err != nil {
    panic(err)
  }

  // Print every type that PageView depends on, grouped by package.
  for path, pkg := range ctpl.Packages {
    for name := range pkg.ScopeTypes() {
      fmt.Println(path, name)
    }
  }
}

Given these types:

package test

type Event[T any] struct {
  Name   string `json:"name"`
  Params T      `json:"params,omitempty"`
}
package event

type PageView test.Event[params.PageView]

requesting event.PageView puts event.PageView, test.Event and params.PageView (and everything they reference) in scope.

Concepts

  • Contemplate.Packages holds every loaded package, including transitive imports, keyed by import path.
  • Package.Types() returns all objects in a package scope.
  • Package.ScopeTypes() returns only the requested types and the types they reference. This is usually what a generator iterates over.
  • Contemplate.LookupTypesByType() finds constants and types derived from a named type, e.g. the values of an enum.
  • Package.Raw() exposes the underlying *packages.Package for anything not covered.

Configuration

Config carries json and yaml tags, so it can be embedded in a generator's config file:

directory: ./ # directory containing go.mod; env vars are expanded; defaults to cwd
packages:
  - path: github.com/foomo/sesamy-go/event
    types:
      - PageView

See the package documentation for the full API.

How to Contribute

Contributions are welcome! Please read the contributing guide.

Contributors

License

Distributed under MIT License, please read the license file for more details.

Made with ♥ foomo by bestbytes

S
Description
No description provided
Readme MIT
395 KiB
Languages
Go 83%
Makefile 17%