Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jizhuozhi/intellij-go-optional-chaining
https://github.com/jizhuozhi/intellij-go-optional-chaining
Last synced: 9 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/jizhuozhi/intellij-go-optional-chaining
- Owner: jizhuozhi
- License: mit
- Created: 2023-07-10T04:04:19.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-02-08T16:35:09.000Z (11 months ago)
- Last Synced: 2024-02-08T17:45:21.704Z (11 months ago)
- Language: Kotlin
- Size: 72.3 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Optional Chaining
Plugin is avaliable on [JetBrains Marketplace](https://plugins.jetbrains.com/plugin/22229-optional-chaining)
## Basic
Optional Chaining (Safe navigation) can be achieved by writing expressions in annotations.
A simple example:
```go
f := func(a *A) (*E, error) {
//optional a?.b()!.C()?!.D.Value()
}
```Through this plugin (select the comment, right click, and then click `Optional Chaining`, you can also customize the shortcut key) the following code will be generated
```go
f := func(a *A) (*Value, error) {
//optional a?.b()!.C()?!.D.Value()
_0 := a
if _0 == nil {
return nil, nil
}
_1, err := _0.b()
if err != nil {
return nil, err
}
_2, err := _1.C()
if err != nil || _2 == nil {
return nil, err
}
return _2.D.Value(), nil
}
```You can see that expressions support four methods of getting values or executing functions
1. `.`: Use the same method of getting a value or executing a function as Go, and the statement will be translated in the original way
2. `?.`: Optional, if the value is `nil`, the function will end here and return the zero value of the corresponding type (if it is a numeric type, it is ` 0`, the pointer is `nil`, if it is a structure, it is `T{}`, and the zero value of other types defined in Go)
3. `!.`: Error-able, valid only for function calls, and requires the return value of the outer function to return two results, and the second result must be of type error. If the function call returns `err` is not `nil`, the function will end here and return an error
4. `?!.`: Optional and error-prone, the function is the combination of `?.` and `!.`All statements except these four operators will be replaced with the original statement. (The plugin supports two types of function signatures `fun (...) T` and `fun (...) (T, error)`, the first type of function does not support < code>!.`)
## Comment format:
In order to avoid wrong parsing, the plugin requires that comments must be on a single line and written in the following format
`//optional ${your expression}` (replace `${your expression}` with your expression)