github.com/stackb/rules_proto@v0.0.0-20240221195024-5428336c51f1/pkg/protoc/language_config.go (about)

     1  package protoc
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  	"strconv"
     7  )
     8  
     9  // LanguageConfig carries a set of configured Plugins and Rules that will
    10  // contribute to a protoc invocation.
    11  type LanguageConfig struct {
    12  	Name    string
    13  	Protoc  string
    14  	Enabled bool
    15  	Plugins map[string]bool
    16  	Rules   map[string]bool
    17  }
    18  
    19  // newLanguageConfig constructs a new language configuration having the
    20  // given name and list of default rule configurations.
    21  func newLanguageConfig(name string, rules ...*LanguageRuleConfig) *LanguageConfig {
    22  	c := &LanguageConfig{
    23  		Name:    name,
    24  		Rules:   make(map[string]bool),
    25  		Plugins: make(map[string]bool),
    26  		Enabled: true,
    27  	}
    28  	for _, rule := range rules {
    29  		c.Rules[rule.Name] = true
    30  	}
    31  	return c
    32  }
    33  
    34  // GetEnabledRules filters the list of enabled Rules in a sorted manner.
    35  func (c *LanguageConfig) GetRulesByIntent(intent bool) []string {
    36  	names := make([]string, 0)
    37  	for name, want := range c.Rules {
    38  		if want != intent {
    39  			continue
    40  		}
    41  		names = append(names, name)
    42  	}
    43  	sort.Strings(names)
    44  	return names
    45  }
    46  
    47  func (c *LanguageConfig) clone() *LanguageConfig {
    48  	clone := newLanguageConfig(c.Name)
    49  	clone.Enabled = c.Enabled
    50  	clone.Protoc = c.Protoc
    51  	for k, v := range c.Plugins {
    52  		clone.Plugins[k] = v
    53  	}
    54  	for k, v := range c.Rules {
    55  		clone.Rules[k] = v
    56  	}
    57  	return clone
    58  }
    59  
    60  // parseDirective parses the directive string or returns error.
    61  func (c *LanguageConfig) parseDirective(cfg *PackageConfig, d, param, value string) error {
    62  	intent := parseIntent(param)
    63  	switch intent.Value {
    64  	case "enabled", "enable":
    65  		enabled, err := strconv.ParseBool(value)
    66  		if err != nil {
    67  			return fmt.Errorf("enable %s: %w", value, err)
    68  		}
    69  		c.Enabled = enabled
    70  	case "plugin":
    71  		c.Plugins[value] = intent.Want
    72  	case "protoc":
    73  		c.Protoc = value
    74  	case "rule":
    75  		c.Rules[value] = intent.Want
    76  	default:
    77  		return fmt.Errorf("unknown parameter %q", value)
    78  	}
    79  
    80  	return nil
    81  }
    82  
    83  // fromYAML loads configuration from the yaml rule confug.
    84  func (c *LanguageConfig) fromYAML(y *YLanguage) error {
    85  	if c.Name != y.Name {
    86  		return fmt.Errorf("yaml language mismatch: want %q got %q", c.Name, y.Name)
    87  	}
    88  	for _, plugin := range y.Plugin {
    89  		c.Plugins[plugin] = true
    90  	}
    91  	for _, rule := range y.Rule {
    92  		c.Rules[rule] = true
    93  	}
    94  	if y.Enabled != nil {
    95  		c.Enabled = *y.Enabled
    96  	} else {
    97  		c.Enabled = true
    98  	}
    99  	return nil
   100  }