injecttag

package
v0.10.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2025 License: MIT, BSD-2-Clause Imports: 9 Imported by: 0

README

pkg/injecttag

Struct tag injection library for Go source files.

Forked from github.com/favadi/protoc-go-inject-tag (MIT License) with exported API for library use.

Purpose

This package provides programmatic access to struct tag injection. It reads special comments in Go files and injects them as struct tags.

Usage

import "github.com/OpenAudio/ddex-proto/pkg/injecttag"

func main() {
    // Read the generated .pb.go file
    src, err := os.ReadFile("generated.pb.go")
    if err != nil {
        log.Fatal(err)
    }

    // Parse file and find injection points
    areas, err := injecttag.ParseFile("generated.pb.go", src, nil)
    if err != nil {
        log.Fatal(err)
    }

    // Write modified file with injected tags
    err = injecttag.WriteFile("generated.pb.go", areas, false)
    if err != nil {
        log.Fatal(err)
    }
}

API

Main Functions:

  • ParseFile(inputPath string, src interface{}, xxxSkip []string) ([]TextArea, error)
  • WriteFile(inputPath string, areas []TextArea, removeTagComment bool) error
  • Logf(format string, v ...interface{})

Types:

  • TextArea - Represents an injection point
  • Verbose bool - Controls verbose logging

See Also

  • cmd/protoc-go-inject-tag - CLI wrapper
  • pkg/ddexgen - DDEX code generation

Attribution

Original work by @favadi and contributors.
Maintained fork by the OpenAudio/ddex-proto team.

See LICENSE for MIT License.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Verbose = false

Verbose controls whether verbose logging is enabled

Functions

func Logf

func Logf(format string, v ...interface{})

Logf logs a formatted message if verbose mode is enabled

func WriteFile

func WriteFile(inputPath string, areas []TextArea, removeTagComment bool) (err error)

WriteFile writes the modified file with injected custom tags

Types

type TextArea

type TextArea struct {
	Start        int
	End          int
	CurrentTag   string
	InjectTag    string
	CommentStart int
	CommentEnd   int
}

TextArea represents an area in the source code where tags will be injected

func ParseFile

func ParseFile(inputPath string, src interface{}, xxxSkip []string) (areas []TextArea, err error)

ParseFile parses a Go source file and returns areas where custom tags should be injected

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL