Skip to content
This repository was archived by the owner on Mar 3, 2023. It is now read-only.

Improve Config API with regard to scoped settings - #4567

Closed
nathansobo wants to merge 5 commits into
masterfrom
ns-improve-scoped-config-api
Closed

nathansobo wants to merge 5 commits into
masterfrom
ns-improve-scoped-config-api

Conversation

@nathansobo

Copy link
Copy Markdown
Contributor

This may be able to supplant #4558.

/cc @maxbrunsfeld: I got the get/set API looking good.

Problems with Config API

The Config API still has some awkwardness leftover from when scoped settings were merged in. It would be nice to make the API more coherent considering the expanded role of settings.

  • We're using a private method, ::addScopedSettings in packages. We need a public equivalent. The main difference is that ::addScopedSettings takes a source argument. The source is used to distinguish between user settings and package settings internally, so it's important.
  • Methods related to defaults don't make sense in the presence of scoped settings. The methods ::isDefault, ::restoreDefault, and ::getDefault don't actually deal with true default values from the schemas. Instead they consider a value "default" if its source isn't the user's config.cson

Proposed Solution

We can eliminate ::addScopedSettings and the methods that deal with defaults from the API. Instead, we replace them with the ability to talk about the source of a given setting when getting and setting it. Then the settings view can make decisions based on the source being the user's config.cson, rather than exposing a murky conception of "default" in settings.

We propose to replace the following methods:

  • getDefault
  • isDefault
  • restoreDefault
  • addScopedSettings

With these new signatures for get and set

  • set(keyPath, value, [{scopeSelector, source}])
  • get(keyPath, [{scope, sources, excludeSources}])

Plus the new unset method... which can be used to remove a single key path or all key paths from a given scope.

  • unset([keyPath], [{source}])

If we need to know if a value is assigned in the user's settings, we specify the desired source in our getter. If we get no value back, we can assume we're using some other value (maybe a schema default, mabye from a package). If we want to determine the non-user-assigned value, we exclude it as a source when getting the setting.

Steps

  • Store global settings in the scoped property store under * so they're associated with a source
  • Refactor the scoped-property-store to allow mutating values without always adding new entries to the cascade (Group property sets into mutable layers scoped-property-store#8).
  • Bugfix: Ensure the most recently assigned setting is honored when there is a specificity tie.

@nathansobo
nathansobo force-pushed the ns-improve-scoped-config-api branch 2 times, most recently from 07288da to 76c9181 Compare December 12, 2014 16:07
@maxbrunsfeld
maxbrunsfeld force-pushed the ns-improve-scoped-config-api branch 2 times, most recently from be4b9f6 to 8186613 Compare December 13, 2014 00:37
@nathansobo

Copy link
Copy Markdown
Contributor Author

I've realized that we may want to keep an add method after all so we can be explicit about the cascade. Previously we were thinking of updating the cascade position on every call to set, but that is going to be hard to reason about. Here's a loose specification for the behavior I'm envisioning. It's pretty close to what we already have working on this PR, and actually makes things simple.

Config Specification

Config settings are organized in a cascade, similar to style sheets. Each entry in the cascade is a hash mapping scope selectors to objects containing key-value pairs of settings. Each entry in the cascade is also associated with a source, which is usually a path to a .cson or .json file on disk containing the serialized entry. Each entry is also associated with a priority, a floating point number indicating the entry's location in the cascade, using a descending sort order.

Sample Cascade:

[
  {
    source: '~/.atom/packages/package-a/settings'
    priority: 1
    settings:
      'atom-workspace':
        'foo.enableBar': true
  }
  {
    source: '~/.atom/packages/package-b/settings'
    priority: 0.5
    settings:
      'atom-text-editor':
        'foo.enableBaz': false
  }
  {
    source: '~/.atom/config.cson'
    priority: -1
    settings:
      'atom-workspace':
        'foo.enableBar': false
  }
]

Just like in CSS, later entries in the cascade take precedence. The user's settings are associated with a priority of -1 so they always appear last in the cascade. When new entries are added to the cascade, a priority can be specified. If no priority is specified, one will automatically be assigned between 0 and 1 so that the entry becomes the penultimate entry in the cascade just before the user settings.

Config Methods

add(source, scopedSettings, {priority})

Add an entry to the cascade, associated with a source and a priority. If add is called twice with the same source, the existing entry for the same source is replaced. Returns a disposable that can be used to remove the entry. This means we probably don't need a remove method.

set(keyPath, value, {source, scopeSelector})

Set the value of a key path in the cascade. The assignment will specifically assign settings in the entry associated with the specified source. If no source is specified, it defaults to the user's config file path. If no scopeSelector is specified, it defaults to the global selector, *.

Open question: what happens if no cascade entry exists for the specified source? Should one be created? Should it fail with an exception? Should it fail silently? I think failing with an exception might make the most sense because the user settings will always exist and if the caller is specifying another source they presumably know what they're doing.

unset(keyPath, {source, scopeSelector})

Delete the key path in the specified source and scope selector. If no scope selector is provided, it defaults to *.

get(keyPath, {scope, sources, excludeSources})

Get the value associated with a key path. If a scope is provided, it will be matched against selectors in the cascade. If no scope is provided, only globally-scoped properties will be matched. The sources and excludeSources options force the match to include or exclude arrays of specific entries.

@benogle

benogle commented Dec 15, 2014

Copy link
Copy Markdown
Contributor

The user's settings are associated with a priority of -1 so they always appear last in the cascade. When new entries are added to the cascade, a priority can be specified. If no priority is specified, one will automatically be assigned between 0 and 1 so that the entry becomes the penultimate entry in the cascade just before the user settings.

Some nits:

  • Why between 0 and 1? Why not a similar system to the panel priority?
  • Why is -1 higher priority than 1? That seems backward to me, and might be confusing to anyone else consuming the API

@maxbrunsfeld

Copy link
Copy Markdown
Contributor

Yeah, I agree with the panel-like priority system. I think the user setting should have priority Infinity.

@maxbrunsfeld
maxbrunsfeld force-pushed the ns-improve-scoped-config-api branch 2 times, most recently from 92acba6 to 3c53fcc Compare December 15, 2014 23:33
@maxbrunsfeld maxbrunsfeld mentioned this pull request Dec 16, 2014
11 of 12 tasks
@maxbrunsfeld
maxbrunsfeld force-pushed the ns-improve-scoped-config-api branch from 3c53fcc to c112e4e Compare December 16, 2014 02:00
@maxbrunsfeld

Copy link
Copy Markdown
Contributor

As per discussion w/ @benogle, the API changes are being implemented in #4631, which we'll merge first. I've separated out the scoped-property-store work into this branch.

@maxbrunsfeld
maxbrunsfeld force-pushed the ns-improve-scoped-config-api branch 3 times, most recently from 7a5ac7d to eec6d61 Compare December 16, 2014 22:35
maxbrunsfeld and others added 5 commits December 19, 2014 14:20
This reverts commit dc2a453.

Conflicts:
	spec/config-spec.coffee
	src/config.coffee
There are a few failing specs, but I think they can all be addressed by
having a `source` for all properties and using it to target only the
user settings in various cases.
@nathansobo

Copy link
Copy Markdown
Contributor Author

This was long ago addressed in other PRs.

@nathansobo nathansobo closed this May 28, 2015
@thomasjo
thomasjo deleted the ns-improve-scoped-config-api branch August 22, 2015 06:48
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants