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

     1  package config
     2  
     3  import (
     4  	"encoding/json"
     5  )
     6  
     7  const packageJsonFileNameForJs = "package.json"
     8  const packageJsonFileNameForJsExtension = ".json"
     9  
    10  type jsonConfigLoader struct {
    11  	filePath string
    12  }
    13  
    14  func (j jsonConfigLoader) LoadExternalConfig() (*ExternalConfig, error) {
    15  	data, err := loadFileContent(j.filePath)
    16  	if err != nil {
    17  		return nil, err
    18  	}
    19  
    20  	var config ExternalConfig
    21  	var jsonData jsonEmbeddedConfig
    22  	// do not unmarshal strict. JS specific package.json will contain
    23  	// other values as well.
    24  	if jsonErr := json.Unmarshal(data, &jsonData); jsonErr != nil {
    25  		return nil, jsonErr
    26  	}
    27  
    28  	readConfig := jsonData.toExternalConfig()
    29  	if readConfig == nil {
    30  		return nil, nil
    31  	}
    32  	config = *readConfig
    33  
    34  	config.SourcePath = j.filePath
    35  
    36  	return &config, nil
    37  }
    38  
    39  type jsonEmbeddedConfig struct {
    40  	Protolint *Lint `json:"protolint"`
    41  }
    42  
    43  func (p jsonEmbeddedConfig) toExternalConfig() *ExternalConfig {
    44  	if p.Protolint == nil {
    45  		return nil
    46  	}
    47  
    48  	return &ExternalConfig{
    49  		Lint: *p.Protolint,
    50  	}
    51  }