# favadi/protoc-go-inject-tag architecture

> Architecture diagram of the GitHub repository favadi/protoc-go-inject-tag, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/favadi/protoc-go-inject-tag
- Repository: https://github.com/favadi/protoc-go-inject-tag

## Overview

##### Main Components
- Command-Line Interface (CLI) / Entry Point: This is implemented in main.go (and verified with main_test.go for testing). It handles the invocation of the tool, parsing command-line flags (e.g., -input, -verbose, -remove_tag_comment, and others).
- Parsing and Injection Logic: Files like parse.go, file.go, and verbose.go contain the logic for processing generated .pb.go files. They scan for specific comments (e.g., “@gotags:”) and then update the corresponding struct tags in the code.
- Protocol Buffer and Example Files: The pb directory contains example files (test.proto, test.pb.go) and a helper script (code_gen.sh). Although not part of the tool’s internal runtime logic, they illustrate how to generate the input (via protoc) and test the tag injection.
- External Dependencies and Configuration: The project uses Go modules (go.mod, go.sum) to manage its dependencies. Configuration files like .golangci.yml, .travis.yml, and .gitignore show that code quality, CI, and version control practices are in place.

##### Relationship and Interactions
- The flow starts at the CLI (main.go), where the tool accepts command-line options provided by the user.
- The CLI passes the parameters to the parsing/injection component that processes the input files identified by the pattern (e.g., “*.pb.go”).
- As the parser reads a .pb.go file, it identifies the special comment annotations in the source code.
- On encountering these annotations, it invokes routines (likely in parse.go and the associated helper files) that modify the struct definitions accordingly, injecting custom tags.
- If the -remove_tag_comment flag is provided, the injection component removes the annotation comments from the output, ensuring compatibility with other tools like openapi generators.
- The tool then writes the modified code back to disk.
- A separate external process (illustrated in the pb/code_gen.sh script) is used earlier in the workflow to generate the .pb.go files by invoking protoc with the go plugin.

##### Architectural Patterns and Design Principles
- The project follows a clearly modular design with separation between the CLI interface and the core business logic (i.e., parsing and code injection).
- It relies on the “separation of concerns” principle—CLI handling, parsing/injection processing, and integration with external protobuf tools are kept clearly distinct.
- Given its command-line nature, the project is straightforward and single-process, so while it is not a full-blown microservice architecture, the components (CLI, processing logic, external integration) are organically layered.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Process Subgraph
    subgraph "External Process"
        EP["External Protoc Generation"]:::external
        PB["Protocol Buffer & Example Files"]:::external
    end

    %% CLI Layer Subgraph
    subgraph "CLI Layer"
        CLI["CLI / Main Entry"]:::cli
        CLI_Test["CLI Tests"]:::cli
    end

    %% Parsing & Injection Module Subgraph
    subgraph "Parsing & Injection Module"
        Parser["Parser"]:::parsing
        FileHandler["File Handler"]:::parsing
        VerboseHandler["Verbose Handler"]:::parsing
        Note["Injects custom tags based_on_@gotags_comments"]:::annotation
    end

    %% File Output
    Modified["Modified .pb.go Files"]:::output

    %% External Dependencies & Configuration Subgraph
    subgraph "External Dependencies & Configuration"
        Gomod["Go Module"]:::config
        Gosum["Go Sum"]:::config
        GolangCI["GolangCI Config"]:::config
        Travis["Travis CI Config"]:::config
        Gitignore["Git Ignore"]:::config
        License["License"]:::config
    end

    %% Data Flow Connections
    EP -->|"generates"| PB
    PB -->|"provides_input"| CLI
    CLI -->|"initiates"| Parser
    CLI -->|"initiates"| FileHandler
    CLI -->|"initiates"| VerboseHandler
    Parser -->|"injects_tags"| Modified
    FileHandler -->|"updates_files"| Modified
    VerboseHandler -->|"handles_logging"| Modified
    CLI_Test ---|"tests"| CLI

    %% Connections from CLI to External Dependencies (dotted)
    CLI ---|"uses"| Gomod
    CLI ---|"uses"| Gosum
    CLI ---|"uses"| GolangCI
    CLI ---|"uses"| Travis
    CLI ---|"uses"| Gitignore
    CLI ---|"uses"| License

    %% Styles
    classDef cli fill:#ffcccc,stroke:#990000,stroke-width:2px;
    classDef parsing fill:#ccffcc,stroke:#009900,stroke-width:2px;
    classDef external fill:#ccccff,stroke:#000099,stroke-width:2px;
    classDef config fill:#ffffcc,stroke:#999900,stroke-width:2px;
    classDef output fill:#e0e0e0,stroke:#333333,stroke-width:2px;
    classDef annotation fill:#ffffe0,stroke:#999900,stroke-width:2px,stroke-dasharray:5 5

    %% Click Events
    click CLI "https://github.com/favadi/protoc-go-inject-tag/blob/master/main.go"
    click CLI_Test "https://github.com/favadi/protoc-go-inject-tag/blob/master/main_test.go"
    click Parser "https://github.com/favadi/protoc-go-inject-tag/blob/master/parse.go"
    click FileHandler "https://github.com/favadi/protoc-go-inject-tag/blob/master/file.go"
    click VerboseHandler "https://github.com/favadi/protoc-go-inject-tag/blob/master/verbose.go"
    click PB "https://github.com/favadi/protoc-go-inject-tag/tree/master/pb"
    click EP "https://github.com/favadi/protoc-go-inject-tag/blob/master/pb/code_gen.sh"
    click Gomod "https://github.com/favadi/protoc-go-inject-tag/blob/master/go.mod"
    click Gosum "https://github.com/favadi/protoc-go-inject-tag/blob/master/go.sum"
    click GolangCI "https://github.com/favadi/protoc-go-inject-tag/blob/master/.golangci.yml"
    click Travis "https://github.com/favadi/protoc-go-inject-tag/blob/master/.travis.yml"
    click Gitignore "https://github.com/favadi/protoc-go-inject-tag/blob/master/.gitignore"
    click License "https://github.com/favadi/protoc-go-inject-tag/tree/master/LICENSE"
```
