Go API Documentation

github.com/TykTechnologies/tyk/cli/linter

No package summary is available.

Package

Files: 1. Third party imports: 0. Imports from organisation: 0. Tests: 0. Benchmarks: 0.

Types

stringFormat

This type doesn't have documentation.

Field name Field type Comment
type

func(string) bool

No comment on field.
type stringFormat func(string) bool

Functions

func Run

Run will lint the configuration file. It will return the path to the config file that was checked, a list of warnings and an error, if any happened.

func Run(schm string, paths []string) (string, []string, error) {
	addFormats(&gojsonschema.FormatCheckers)
	var conf config.Config
	if err := config.Load(paths, &conf); err != nil {
		return "", nil, err
	}
	schemaLoader := gojsonschema.NewBytesLoader([]byte(schm))

	var orig map[string]interface{}
	f, err := os.Open(conf.Private.OriginalPath)
	if err != nil {
		return "", nil, err
	}
	defer f.Close()
	if err := json.NewDecoder(f).Decode(&orig); err != nil {
		return "", nil, err
	}
	if v, ok := orig["Monitor"]; ok {
		// As the old confs wrongly capitalized this key. Would
		// be fixed by WriteConf below, but we want the JSON
		// schema to not flag this error.
		orig["monitor"] = v
		delete(orig, "Monitor")
	}

	fileLoader := gojsonschema.NewGoLoader(orig)
	result, err := gojsonschema.Validate(schemaLoader, fileLoader)
	if err != nil {
		return "", nil, err
	}

	// ensure it's well formatted and the keys are all lowercase
	if err := config.WriteConf(conf.Private.OriginalPath, &conf); err != nil {
		return "", nil, err
	}

	return conf.Private.OriginalPath, resultWarns(result), nil
}

Cognitive complexity: 13, Cyclomatic complexity: 7

Uses: config.Config, config.Load, config.WriteConf, gojsonschema.FormatCheckers, gojsonschema.NewBytesLoader, gojsonschema.NewGoLoader, gojsonschema.Validate, json.NewDecoder, os.Open.

func (stringFormat) IsFormat

func (f stringFormat) IsFormat(v interface{}) bool {
	s := v.(string)
	if s == "" {
		return true	// empty string is ok
	}
	return f(s)
}

Cognitive complexity: 3, Cyclomatic complexity: 2

Private functions

func addFormats

addFormats (chain *gojsonschema.FormatCheckerChain)
References: errors.As, net.AddrError, net.SplitHostPort, os.Stat.

func resultWarns

resultWarns (result *gojsonschema.Result) []string
References: fmt.Sprintf, gojsonschema.DoesNotMatchFormatError.


Tests

Files: 1. Third party imports: 0. Imports from organisation: 0. Tests: 2. Benchmarks: 0.

Vars

var tests = []struct {
	name	string
	in	string
	want	interface{}
}{
	{
		"InvalidJSON", `{`,
		"unexpected EOF",
	},
	{
		"WrongType", `{"enable_jsvm": 3}`,
		"cannot unmarshal number into Go struct field Config.enable_jsvm of type bool",
	},
	{
		"FieldTypo", `{"enable_jsvmm": true}`,
		"Additional property enable_jsvmm is not allowed",
	},
	{"Empty", `{}`, nil},
	{"Default", onDefaults(`{}`), nil},
	{"OldMonitor", `{"Monitor": {}}`, nil},
	{"NullObject", `{"event_handlers": null}`, nil},
	{
		"MissingPath", `{"app_path": "missing-path"}`,
		"app_path: Path does not exist or is not accessible",
	},
	{
		"ExtraPort", `{"listen_address": "foo.com:8080"}`,
		"listen_address: Address should be a host without port",
	},
	{
		"BadHost", `{"storage": {"host": "::::"}}`,
		"storage.host: Address should be a host without port",
	},
	{
		"BadLogLevel", `{"log_level": "catastrophic"}`,
		`log_level: log_level must be one of the following: "", "debug", "info", "warn", "error"`,
	},
	{
		"BadStorageType", `{"storage": {"type": "cd-rom"}}`,
		`storage.type: storage.type must be one of the following: "", "redis"`,
	},
	{
		"BadPolicySource", `{"policies": {"policy_source": "internet"}}`,
		`policies.policy_source: policies.policy_source must be one of the following: "", "service", "rpc"`,
	},
	{
		"MalformedDnsCacheEntry", `{"dns_cache": { "enabled": true, "tttl": 10} }`,
		`dns_cache: Additional property tttl is not allowed`,
	},
	{
		"BadDnsCacheTTL", `{"dns_cache": { "enabled": false, "ttl": -2 } }`,
		`dns_cache.ttl: Must be greater than or equal to -1`,
	},
	{
		"ExtraDnsCacheCheckInterval", `{"dns_cache": { "enabled": true, "ttl": -1, "check_interval": 2500 } }`,
		`dns_cache: Additional property check_interval is not allowed`,
	},
	{
		"InvalidDnsCacheMultipleIPsHandleStrategy", `{"dns_cache": { "enabled": true, "ttl": 1, "multiple_ips_handle_strategy": "true" } }`,
		`dns_cache.multiple_ips_handle_strategy: dns_cache.multiple_ips_handle_strategy must be one of the following: "pick_first", "random", "no_cache"`,
	},
}

Test functions

TestLint

References: ioutil.ReadFile, ioutil.TempFile, strings.Join, testing.T.

TestMain

References: os.Chdir, os.Exit.