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

     1  package config
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  // IndentOption represents the option for the INDENT rule.
     9  type IndentOption struct {
    10  	CustomizableSeverityOption
    11  	Style string
    12  	// Deprecated: not used
    13  	Newline          string
    14  	NotInsertNewline bool
    15  }
    16  
    17  // UnmarshalYAML implements yaml.v2 Unmarshaler interface.
    18  func (i *IndentOption) UnmarshalYAML(unmarshal func(interface{}) error) error {
    19  	var option struct {
    20  		Style            string `yaml:"style"`
    21  		Newline          string `yaml:"newline"`
    22  		NotInsertNewline bool   `yaml:"not_insert_newline"`
    23  	}
    24  	if err := unmarshal(&option); err != nil {
    25  		return err
    26  	}
    27  
    28  	var style string
    29  	switch option.Style {
    30  	case "tab":
    31  		style = "\t"
    32  	case "4":
    33  		style = strings.Repeat(" ", 4)
    34  	case "2":
    35  		style = strings.Repeat(" ", 2)
    36  	case "":
    37  		break
    38  	default:
    39  		return fmt.Errorf("%s is an invalid style option. valid option is tab, 4 or 2", option.Style)
    40  	}
    41  	i.Style = style
    42  
    43  	switch option.Newline {
    44  	case "\n", "\r", "\r\n", "":
    45  		i.Newline = option.Newline
    46  	default:
    47  		return fmt.Errorf(`%s is an invalid newline option. valid option is \n, \r or \r\n`, option.Newline)
    48  	}
    49  	i.NotInsertNewline = option.NotInsertNewline
    50  	return nil
    51  }
    52  
    53  // UnmarshalTOML implements toml Unmarshaler interface.
    54  func (i *IndentOption) UnmarshalTOML(data interface{}) error {
    55  	optionsMap := map[string]interface{}{}
    56  	for k, v := range data.(map[string]interface{}) {
    57  		optionsMap[k] = v.(string)
    58  	}
    59  
    60  	if style, ok := optionsMap["style"]; ok {
    61  		styleStr := style.(string)
    62  		switch styleStr {
    63  		case "\t":
    64  			styleStr = "\t"
    65  		case "tab":
    66  			styleStr = "\t"
    67  		case "4":
    68  			styleStr = strings.Repeat(" ", 4)
    69  		case "2":
    70  			styleStr = strings.Repeat(" ", 2)
    71  		case "":
    72  			break
    73  		default:
    74  			return fmt.Errorf("%s is an invalid style option. valid option is \\t, tab, 4 or 2", style)
    75  		}
    76  		i.Style = styleStr
    77  	}
    78  
    79  	if newLine, ok := optionsMap["newline"]; ok {
    80  		newLineStr := newLine.(string)
    81  		switch newLineStr {
    82  		case "\n", "\r", "\r\n", "":
    83  			i.Newline = newLineStr
    84  		default:
    85  			return fmt.Errorf(`%s is an invalid newline option. valid option is \n, \r or \r\n`, newLine)
    86  		}
    87  	}
    88  
    89  	if insertNoNewLine, ok := optionsMap["not_insert_newline"]; ok {
    90  		i.NotInsertNewline = insertNoNewLine.(bool)
    91  	}
    92  
    93  	return nil
    94  }