github.com/yoheimuta/protolint@v0.49.8-0.20240515023657-4ecaebb7575d/internal/linter/config/importsSortedOption.go (about)

     1  package config
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  // ImportsSortedOption represents the option for the IMPORTS_SORTED rule.
     8  type ImportsSortedOption struct {
     9  	CustomizableSeverityOption
    10  	// Deprecated: not used
    11  	Newline string
    12  }
    13  
    14  // UnmarshalYAML implements yaml.v2 Unmarshaler interface.
    15  func (i *ImportsSortedOption) UnmarshalYAML(unmarshal func(interface{}) error) error {
    16  	var option struct {
    17  		Newline string `yaml:"newline"`
    18  	}
    19  	if err := unmarshal(&option); err != nil {
    20  		return err
    21  	}
    22  
    23  	switch option.Newline {
    24  	case "\n", "\r", "\r\n", "":
    25  		i.Newline = option.Newline
    26  	default:
    27  		return fmt.Errorf(`%s is an invalid newline option. valid option is \n, \r or \r\n`, option.Newline)
    28  	}
    29  	return nil
    30  }
    31  
    32  // UnmarshalTOML implements toml Unmarshaler interface.
    33  func (i *ImportsSortedOption) UnmarshalTOML(data interface{}) error {
    34  	optionsMap := map[string]interface{}{}
    35  	for k, v := range data.(map[string]interface{}) {
    36  		optionsMap[k] = v.(string)
    37  	}
    38  
    39  	if newline, ok := optionsMap["newline"]; ok {
    40  		switch newline.(string) {
    41  		case "\n", "\r", "\r\n", "":
    42  			i.Newline = newline.(string)
    43  		default:
    44  			return fmt.Errorf(`%s is an invalid newline option. valid option is \n, \r or \r\n`, newline)
    45  		}
    46  	}
    47  	return nil
    48  }