Repository: zyedidia/micro
File: internal/config/settings.go (Go)

Code before:
	// because they have been modified by the user in this session
	ModifiedSettings map[string]bool

	// VolatileSettings is a map of settings which should not be written to disk
	// because they have been temporarily set for this session only
	VolatileSettings map[string]bool
)

func init() {
	ModifiedSettings = make(map[string]bool)
	VolatileSettings = make(map[string]bool)
}

func validateParsedSettings() error {
	var err error
	defaults := DefaultAllSettings()
	for k, v := range parsedSettings {
		if strings.HasPrefix(reflect.TypeOf(v).String(), "map") {
			if strings.HasPrefix(k, "ft:") {
				for k1, v1 := range v.(map[string]interface{}) {
					if _, ok := defaults[k1]; ok && !verifySetting(k1, reflect.TypeOf(v1), reflect.TypeOf(defaults[k1])) {
						err = fmt.Errorf("Error: setting '%s' has incorrect type (%s), using default value: %v (%s)", k1, reflect.TypeOf(v1), defaults[k1], reflect.TypeOf(defaults[k1]))
						parsedSettings[k].(map[string]interface{})[k1] = defaults[k1]
						continue
					}
					if e := OptionIsValid(k1, v1); e != nil {
						err = e
						parsedSettings[k].(map[string]interface{})[k1] = defaults[k1]
					}
				}
			} else {
				if _, e := glob.Compile(k); e != nil {
					err = errors.New("Error with glob setting " + k + ": " + e.Error())
					continue
				}
				for k1, v1 := range v.(map[string]interface{}) {
					if _, ok := defaults[k1]; ok && !verifySetting(k1, reflect.TypeOf(v1), reflect.TypeOf(defaults[k1])) {
						err = fmt.Errorf("Error: setting '%s' has incorrect type (%s), using default value: %v (%s)", k1, reflect.TypeOf(v1), defaults[k1], reflect.TypeOf(defaults[k1]))
						parsedSettings[k].(map[string]interface{})[k1] = defaults[k1]
						continue
					}
					if e := OptionIsValid(k1, v1); e != nil {
						err = e
						parsedSettings[k].(map[string]interface{})[k1] = defaults[k1]
					}
				}
			}
			continue
		}

		if k == "autosave" {

Review comment (suggestion):
Maybe do this inside `verifySetting()`? That would slim `validateParsedSettings()` down quite a bit.

Code after:
	// ModifiedSettings is a map of settings which should be written to disk
	// because they have been modified by the user in this session
	ModifiedSettings map[string]bool

	// VolatileSettings is a map of settings which should not be written to disk
	// because they have been temporarily set for this session only
	VolatileSettings map[string]bool
)

func init() {
	ModifiedSettings = make(map[string]bool)
	VolatileSettings = make(map[string]bool)
}

func validateParsedSettings() error {
	var err error
	defaults := DefaultAllSettings()
	for k, v := range parsedSettings {
		if strings.HasPrefix(reflect.TypeOf(v).String(), "map") {
			if strings.HasPrefix(k, "ft:") {
				for k1, v1 := range v.(map[string]interface{}) {
					if _, ok := defaults[k1]; ok {
						if e := verifySetting(k1, v1, defaults[k1]); e != nil {
							err = e
							parsedSettings[k].(map[string]interface{})[k1] = defaults[k1]
							continue
						}
					}
				}
			} else {
				if _, e := glob.Compile(k); e != nil {
					err = errors.New("Error with glob setting " + k + ": " + e.Error())
					delete(parsedSettings, k)
					continue
				}
				for k1, v1 := range v.(map[string]interface{}) {
					if _, ok := defaults[k1]; ok {
						if e := verifySetting(k1, v1, defaults[k1]); e != nil {
							err = e
							parsedSettings[k].(map[string]interface{})[k1] = defaults[k1]
							continue
						}
					}
				}
			}
			continue
		}

		if k == "autosave" {
			// if autosave is a boolean convert it to float
			s, ok := v.(bool)