-
-
Notifications
You must be signed in to change notification settings - Fork 9
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
tagliatelle now warns on inline struct fields #8
Comments
Hello, currently, tagliatelle is format neutral, so I need to think about how to about that. Ref: #6 |
I see your PR, it's an opinionated approach because it's not following a neutral format. |
Hi,
I understand where you’re coming from. The latest tagliatelle is included in the latest golanglint-ci, so all projects that use golanglint-ci, have tagliatelle enabled, and use This package already can’t process |
It's a bit more complex: currently, you can define any struct tag, so it must be "neutral" or at least configurable.
This is interesting, maybe you can open another issue on this topic. |
It's not an issue that affects me directly. I'm saying that, while you've been thinking tagliatelle is format-neutral, I think it’s actually only compatible with struct tags that are compatible with |
a small note: https://go.dev/play/p/GqQtABzKMmK package main
import (
"encoding/json"
"fmt"
"os"
"testing"
)
type Foo struct {
Bar Bar `json:",inline"`
}
type Bar struct {
Name string `json:"name"`
}
func TestName(t *testing.T) {
f := &Foo{Bar: Bar{Name: "goo"}}
fmt.Println(json.NewEncoder(os.Stdout).Encode(f))
}
// {"Bar":{"name":"goo"}}
// <nil> https://go.dev/play/p/oAhZPIAD8n9 package main
import (
"encoding/json"
"fmt"
"os"
"testing"
)
type Foo struct {
Bar `json:",inline"`
}
type Bar struct {
Name string `json:"name"`
}
func TestName(t *testing.T) {
f := &Foo{Bar: Bar{Name: "goo"}}
fmt.Println(json.NewEncoder(os.Stdout).Encode(f))
}
// {"name":"goo"}
// <nil> 🤔 the yaml parser doesn't have the same behavior as json with inline. https://go.dev/play/p/0_GnAfICLTu package main
import (
"encoding/json"
"fmt"
"os"
"testing"
yamlv2 "gopkg.in/yaml.v2"
yamlv3 "gopkg.in/yaml.v3"
)
type Foo struct {
Bar Bar `json:",inline" yaml:",inline"`
}
type Bar struct {
Name string `json:"name" yaml:"name"`
}
func TestJSON(t *testing.T) {
f := &Foo{Bar: Bar{Name: "goo"}}
fmt.Println(json.NewEncoder(os.Stdout).Encode(f))
}
func TestYAMLv2(t *testing.T) {
f := &Foo{Bar: Bar{Name: "goo"}}
fmt.Println(yamlv2.NewEncoder(os.Stdout).Encode(f))
}
func TestYAMLv3(t *testing.T) {
f := &Foo{Bar: Bar{Name: "goo"}}
fmt.Println(yamlv3.NewEncoder(os.Stdout).Encode(f))
} |
So I'm not happy with this decision but I will follow you. In the future, I will rewrite tagliatelle to handle tag with a less neutral approach. FYI I'm also a maintainer of golangci-lint. |
After more research, in fact, The "inline" effect is produced by the empty value and an embedded field. https://go.dev/play/p/X7GB3rBC5fh package main
import (
"encoding/json"
"fmt"
"os"
"testing"
)
type Foo struct {
Bar `json:""`
}
type Bar struct {
Name string `json:"name"`
}
func TestJSON(t *testing.T) {
f := &Foo{Bar: Bar{Name: "goo"}}
fmt.Println(json.NewEncoder(os.Stdout).Encode(f))
} But The encoders don't follow the same specification but they overuse the |
here's the workarond that I'm currently using in my .golangci.yml:
ideally it should also check that it is attached to a struct embedding and not a field declaration but this got me by |
Since 7b21925, structs with
json:",inline"
fields now prompt a warning from tagliatelle that a name should be given in the expected camelCase.I think this is incorrect behavior as there is no field name for an
inline
struct.The text was updated successfully, but these errors were encountered: