-
Notifications
You must be signed in to change notification settings - Fork 764
Revamp user preferences serialization/deserialization #2272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jakebailey
wants to merge
22
commits into
main
Choose a base branch
from
jabaile/user-prefs-json
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 13 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
c4286f5
Make user prefs serializable
jakebailey bca6d8f
Very redo
jakebailey 1deb813
Deep copy
jakebailey 1763af6
Another struct
jakebailey 86b1d94
Simpler
jakebailey bf21cb2
Simpler
jakebailey 88a928f
Remove copying
jakebailey 67db3e9
more unused copies
jakebailey 18be102
Restore comments
jakebailey 820c516
Merge branch 'main' into jabaile/user-prefs-json
jakebailey 7ff185e
Default just pointer
jakebailey 0c95a82
Remove
jakebailey 8b08250
Cleanup
jakebailey 46cf5f1
remove old dead test code
jakebailey 711f302
Just return
jakebailey 6c703eb
Delete alias support
jakebailey 4728fba
Explict unstable
jakebailey 9c38c4e
Merge branch 'main' into jabaile/user-prefs-json
jakebailey 8ae6651
test, what
jakebailey 74c815b
Move stuff around but I don't know how to improve quoteStyle, quotePr…
jakebailey 2b4b1f6
Try and deal with old vs new
jakebailey 66b3ae6
Merge branch 'main' into jabaile/user-prefs-json
jakebailey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| package lsutil | ||
|
|
||
| import ( | ||
| "reflect" | ||
| "testing" | ||
|
|
||
| "github.com/go-json-experiment/json" | ||
| "github.com/microsoft/typescript-go/internal/modulespecifiers" | ||
| "gotest.tools/v3/assert" | ||
| ) | ||
|
|
||
| func fillNonZeroValues(v reflect.Value, path string) { | ||
| t := v.Type() | ||
| for i := range t.NumField() { | ||
| field := v.Field(i) | ||
| fieldType := t.Field(i) | ||
| if !field.CanSet() { | ||
| continue | ||
| } | ||
| fieldPath := path + "." + fieldType.Name | ||
| switch field.Kind() { | ||
| case reflect.Bool: | ||
| field.SetBool(true) | ||
| case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: | ||
| field.SetInt(1) | ||
| case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: | ||
| field.SetUint(1) | ||
| case reflect.String: | ||
| val := getValidStringValue(field.Type(), fieldPath) | ||
| field.SetString(val) | ||
| case reflect.Slice: | ||
| if field.Type().Elem().Kind() == reflect.String { | ||
| field.Set(reflect.ValueOf([]string{"test"})) | ||
| } | ||
| case reflect.Struct: | ||
| fillNonZeroValues(field, fieldPath) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func getValidStringValue(t reflect.Type, path string) string { | ||
| typeName := t.String() | ||
| switch typeName { | ||
| case "lsutil.QuotePreference": | ||
| return string(QuotePreferenceSingle) | ||
| case "lsutil.JsxAttributeCompletionStyle": | ||
| return string(JsxAttributeCompletionStyleBraces) | ||
| case "lsutil.IncludePackageJsonAutoImports": | ||
| return string(IncludePackageJsonAutoImportsOn) | ||
| case "lsutil.IncludeInlayParameterNameHints": | ||
| return string(IncludeInlayParameterNameHintsAll) | ||
| case "modulespecifiers.ImportModuleSpecifierPreference": | ||
| return string(modulespecifiers.ImportModuleSpecifierPreferenceRelative) | ||
| case "modulespecifiers.ImportModuleSpecifierEndingPreference": | ||
| return string(modulespecifiers.ImportModuleSpecifierEndingPreferenceJs) | ||
| default: | ||
| return "test" | ||
| } | ||
| } | ||
|
|
||
| func TestUserPreferencesRoundtrip(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| original := &UserPreferences{} | ||
| fillNonZeroValues(reflect.ValueOf(original).Elem(), "UserPreferences") | ||
|
|
||
| jsonBytes, err := json.Marshal(original) | ||
| assert.NilError(t, err) | ||
|
|
||
| t.Run("UnmarshalJSONFrom", func(t *testing.T) { | ||
| t.Parallel() | ||
| parsed := &UserPreferences{} | ||
| err2 := json.Unmarshal(jsonBytes, parsed) | ||
| assert.NilError(t, err2) | ||
| assert.DeepEqual(t, original, parsed) | ||
| }) | ||
|
|
||
| t.Run("parseWorker", func(t *testing.T) { | ||
| t.Parallel() | ||
| var config map[string]any | ||
| err2 := json.Unmarshal(jsonBytes, &config) | ||
| assert.NilError(t, err2) | ||
| parsed := &UserPreferences{} | ||
jakebailey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| parsed.parseWorker(config) | ||
| assert.DeepEqual(t, original, parsed) | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.