<commit_before>package contour

import "fmt"

type UpdateErr struct {
	k    string
	typ  SettingType
	slug string
}

func (e UpdateErr) Error() string {
	return fmt.Sprintf("update of %s failed: %s", e.k, e.slug)
}

type BasicUpdateErr struct {
	typ SettingType
	k   string
}

func (e BasicUpdateErr) Error() string {
	return fmt.Sprintf("%s: %s settings cannot be updated by a basic update", e.k, e.typ)
}

type CoreUpdateErr struct {
	typ SettingType
	k   string
}

func (e CoreUpdateErr) Error() string {
	return fmt.Sprintf("%s: %s settings cannot be updated by a core update", e.k, e.typ)
}

// Only non-core settings are updateable. This assumes that the lock has
// already been obtained by the caller.
func (s *Settings) update(k string, v interface{}) error {
	// if can't update, a false will also return an error explaining why.
	//can := s.canUpdate(typ, k)
	//if !can {
	//	return fmt.Errorf("%se: cannot update using %s", typ)
	//}
	val, _ := s.settings[k]
	val.Value = v
	s.settings[k] = val
	return nil
}

// UpdateBool updates a bool setting. If the setting k doesn't exist, both a
// false and a SettingNotFoundErr will be returned. If the setting k is not
// updateable, both a false and one of the following errors will be returned:
// CoreUpdateErr. BasicUpdateErr, or UpdateErr.
func UpdateBool(k string, v bool) error { return settings.UpdateBool(k, v) }
func (s *Settings) UpdateBool(k string, v bool) error {
	s.mu.Lock()
	defer s.mu.Unlock()
	return s.updateBool(k, v)
}

func (s *Settings) updateBool(k string, v bool) error {
	return s.update(k, v)
}

// UpdateInt updates an int setting. If the setting k doesn't exist, both a
// false and a SettingNotFoundErr will be returned. If the setting k is not
// updateable, both a false and one of the following errors will be returned:
// CoreUpdateErr. BasicUpdateErr, or UpdateErr.
func UpdateInt(k string, v int) error { return settings.UpdateInt(k, v) }
func (s *Settings) UpdateInt(k string, v int) error {
	s.mu.Lock()
	defer s.mu.Unlock()
	return s.updateInt(k, v)
}

func (s *Settings) updateInt(k string, v int) error {
	return s.update(k, v)
}

// UpdateInt64 updates an int64 setting. If the setting k doesn't exist, both a
// false and a SettingNotFoundErr will be returned. If the setting k is not
// updateable, both a false and one of the following errors will be returned:
// CoreUpdateErr. BasicUpdateErr, or UpdateErr.
func UpdateInt64(k string, v int64) error { return settings.UpdateInt64(k, v) }
func (s *Settings) UpdateInt64(k string, v int64) error {
	s.mu.Lock()
	defer s.mu.Unlock()
	return s.updateInt64(k, v)
}

func (s *Settings) updateInt64(k string, v int64) error {
	return s.update(k, v)
}

// UpdateString updates a string setting. If the setting k doesn't exist, both
// a false and a SettingNotFoundErr will be returned. If the setting k is not
// updateable, both a false and one of the following errors will be returned:
// CoreUpdateErr. BasicUpdateErr, or UpdateErr.
func UpdateString(k string, v string) error { return settings.UpdateString(k, v) }
func (s *Settings) UpdateString(k, v string) error {
	s.mu.Lock()
	defer s.mu.Unlock()
	return s.updateString(k, v)
}

func (s *Settings) updateString(k, v string) error {
	return s.update(k, v)
}

// canUpdate checks to see if the passed setting key is updateable. If the key
// doesn't exist, both a false and a SettingNotFoundErr will be returned. If
// the setting is not updateable, both a false and an Update type specific err
// will be returned, e.g. CoreUpdateErr. This assumes that the lock has already
// been obtained by the caller.
//
// core settings can never be set.
// settings that are not a ConfFileVar, EnvVar, and Flag, i.e. a Basic setting,
//   cannot be set by a configuration file, environment variable, or a flag as
//   it has not explicitly been exposed to them. They can only be set by the<commit_msg>better document Update related stuff<commit_after>package contour

import "fmt"

// UpdateErr is any error that happens on an update that isn't one of the
// following: SettingNotFoundErr, BasicUpdateErr, or CoreUpdateErr.
type UpdateErr struct {
	k    string
	typ  SettingType
	slug string
}

func (e UpdateErr) Error() string {
	return fmt.Sprintf("update of %s failed: %s", e.k, e.slug)
}

// BasicUpdateErr happens when there's an attempt to do a basic update on a
// non-basic setting.
type BasicUpdateErr struct {
	typ SettingType
	k   string
}

func (e BasicUpdateErr) Error() string {
	return fmt.Sprintf("%s: %s settings cannot be updated by a basic update", e.k, e.typ)
}

// CoreUpdateErr happens when there's an attempt to do a core update, which
// should never occur, or update a Core setting.
type CoreUpdateErr struct {
	typ SettingType
	k   string
}

func (e CoreUpdateErr) Error() string {
	return fmt.Sprintf("%s: %s settings cannot be updated by a core update", e.k, e.typ)
}

// Only non-core settings are updateable. This assumes that the lock has
// already been obtained by the caller.
func (s *Settings) update(k string, v interface{}) error {
	// if can't update, a false will also return an error explaining why.
	//can := s.canUpdate(typ, k)
	//if !can {
	//	return fmt.Errorf("%se: cannot update using %s", typ)
	//}
	val, _ := s.settings[k]
	val.Value = v
	s.settings[k] = val
	return nil
}

// UpdateBool updates a bool setting. If the setting k doesn't exist, both a
// false and a SettingNotFoundErr will be returned. If the setting k is not
// updateable, both a false and one of the following errors will be returned:
// CoreUpdateErr. BasicUpdateErr, or UpdateErr.
func UpdateBool(k string, v bool) error { return settings.UpdateBool(k, v) }

// UpdateBool updates a bool setting. If the setting k doesn't exist, both a
// false and a SettingNotFoundErr will be returned. If the setting k is not
// updateable, both a false and one of the following errors will be returned:
// CoreUpdateErr. BasicUpdateErr, or UpdateErr.
func (s *Settings) UpdateBool(k string, v bool) error {
	s.mu.Lock()
	defer s.mu.Unlock()
	return s.updateBool(k, v)
}

// this assumes the lock is held by the caller.
func (s *Settings) updateBool(k string, v bool) error {
	return s.update(k, v)
}

// UpdateInt updates an int setting. If the setting k doesn't exist, both a
// false and a SettingNotFoundErr will be returned. If the setting k is not
// updateable, both a false and one of the following errors will be returned:
// CoreUpdateErr. BasicUpdateErr, or UpdateErr.
func UpdateInt(k string, v int) error { return settings.UpdateInt(k, v) }

// UpdateInt updates an int setting. If the setting k doesn't exist, both a
// false and a SettingNotFoundErr will be returned. If the setting k is not
// updateable, both a false and one of the following errors will be returned:
// CoreUpdateErr. BasicUpdateErr, or UpdateErr.
func (s *Settings) UpdateInt(k string, v int) error {
	s.mu.Lock()
	defer s.mu.Unlock()
	return s.updateInt(k, v)
}

// this assumes the lock is held by the caller.
func (s *Settings) updateInt(k string, v int) error {
	return s.update(k, v)
}

// UpdateInt64 updates an int64 setting. If the setting k doesn't exist, both a
// false and a SettingNotFoundErr will be returned. If the setting k is not
// updateable, both a false and one of the following errors will be returned:
// CoreUpdateErr. BasicUpdateErr, or UpdateErr.
func UpdateInt64(k string, v int64) error { return settings.UpdateInt64(k, v) }

// UpdateInt64 updates an int64 setting. If the setting k doesn't exist, both a
// false and a SettingNotFoundErr will be returned. If the setting k is not
// updateable, both a false and one of the following errors will be returned:
// CoreUpdateErr. BasicUpdateErr, or UpdateErr.
func (s *Settings) UpdateInt64(k string, v int64) error {
	s.mu.Lock()
	defer s.mu.Unlock()
	return s.updateInt64(k, v)
}

// this assumes the lock is held by the caller.
func (s *Settings) updateInt64(k string, v int64) error {
	return s.update(k, v)
}

// UpdateString updates a string setting. If the setting k doesn't exist, both
// a false and a SettingNotFoundErr will be returned. If the setting k is not
// updateable, both a false and one of the following errors will be returned:
// CoreUpdateErr. BasicUpdateErr, or UpdateErr.
func UpdateString(k string, v string) error { return settings.UpdateString(k, v) }

// UpdateString updates a string setting. If the setting k doesn't exist, both
// a false and a SettingNotFoundErr will be returned. If the setting k is not
// updateable, both a false and one of the following errors will be returned:
// CoreUpdateErr. BasicUpdateErr, or UpdateErr.
func (s *Settings) UpdateString(k, v string) error {
	s.mu.Lock()
	defer s.mu.Unlock()
	return s.updateString(k, v)
}

// this assumes the lock is held by the caller.
func (s *Settings) updateString(k, v string) error {
	return s.update(k, v)
}

// canUpdate checks to see if the passed setting key is updateable. If the key
// doesn't exist, both a false and a SettingNotFoundErr will be returned. If
// the setting is not updateable, both a false and an Update type specific err
// will be returned, e.g. CoreUpdateErr. This assumes that the lock has already
// been obtained by the caller.
//
// core settings can never be set.
// settings that are not a ConfFileVar, EnvVar, and Flag, i.e. a Basic setting,
//   cannot be set by a configuration file, environment variable, or a flag as
//   it has not explicitly been exposed to them. They can only be set by the
{'fraction_non_alphanumeric': 0.09458605435011586, 'fraction_numerical': 0.004634506003791869, 'mean_word_length': 3.721531576330184, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2386614', 'n_tokens_mistral': 2893, 'n_tokens_neox': 2852, 'n_words': 1359}