Skip to content
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

misc: rename rivertags packages to syntaxtags #19

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Package rivertags exposes an Analyzer which lints river tags.
package rivertags
// Package syntaxtags exposes an Analyzer which lints Alloy syntax tags.
package syntaxtags

import (
"fmt"
Expand All @@ -12,17 +12,17 @@ import (
)

var Analyzer = &analysis.Analyzer{
Name: "rivertags",
Doc: "perform validation checks on River tags",
Name: "syntaxtags",
Doc: "perform validation checks on Alloy syntax tags",
Run: run,
}

var noLintRegex = regexp.MustCompile(`//\s*nolint:(\S+)`)

var (
riverTagRegex = regexp.MustCompile(`river:"([^"]*)"`)
jsonTagRegex = regexp.MustCompile(`json:"([^"]*)"`)
yamlTagRegex = regexp.MustCompile(`yaml:"([^"]*)"`)
syntaxTagRegex = regexp.MustCompile(`river:"([^"]*)"`)
jsonTagRegex = regexp.MustCompile(`json:"([^"]*)"`)
yamlTagRegex = regexp.MustCompile(`yaml:"([^"]*)"`)
)

// Rules for river tag linting:
Expand Down Expand Up @@ -53,12 +53,12 @@ func run(p *analysis.Pass) (interface{}, error) {
sNode := sInfo.Node
s := sInfo.Type

var hasRiverTags bool
var hasSyntaxTags bool

for i := 0; i < s.NumFields(); i++ {
matches := riverTagRegex.FindAllStringSubmatch(s.Tag(i), -1)
matches := syntaxTagRegex.FindAllStringSubmatch(s.Tag(i), -1)
if len(matches) > 0 {
hasRiverTags = true
hasSyntaxTags = true
break
}
}
Expand All @@ -68,7 +68,7 @@ func run(p *analysis.Pass) (interface{}, error) {
field := s.Field(i)
nodeField := lookupField(sNode, i)

// Ignore fields with //nolint:rivertags in them.
// Ignore fields with //nolint:syntaxtags in them.
if comments := nodeField.Comment; comments != nil {
for _, comment := range comments.List {
if lintingDisabled(comment.Text) {
Expand All @@ -77,8 +77,8 @@ func run(p *analysis.Pass) (interface{}, error) {
}
}

matches := riverTagRegex.FindAllStringSubmatch(s.Tag(i), -1)
if len(matches) == 0 && hasRiverTags {
matches := syntaxTagRegex.FindAllStringSubmatch(s.Tag(i), -1)
if len(matches) == 0 && hasSyntaxTags {
// If this struct has River tags, but this field only has json/yaml
// tags, emit an error.
jsonMatches := jsonTagRegex.FindAllStringSubmatch(s.Tag(i), -1)
Expand All @@ -87,7 +87,7 @@ func run(p *analysis.Pass) (interface{}, error) {
if len(jsonMatches) > 0 || len(yamlMatches) > 0 {
p.Report(analysis.Diagnostic{
Pos: field.Pos(),
Category: "rivertags",
Category: "syntaxtags",
Message: "field has yaml or json tags, but no river tags",
})
}
Expand All @@ -98,7 +98,7 @@ func run(p *analysis.Pass) (interface{}, error) {
} else if len(matches) > 1 {
p.Report(analysis.Diagnostic{
Pos: field.Pos(),
Category: "rivertags",
Category: "syntaxtags",
Message: "field should not have more than one river tag",
})
}
Expand All @@ -107,32 +107,32 @@ func run(p *analysis.Pass) (interface{}, error) {
if field.Anonymous() {
p.Report(analysis.Diagnostic{
Pos: field.Pos(),
Category: "rivertags",
Category: "syntaxtags",
Message: "river tags may not be given to anonymous fields",
})
}
if !field.Exported() {
p.Report(analysis.Diagnostic{
Pos: field.Pos(),
Category: "rivertags",
Category: "syntaxtags",
Message: "river tags may only be given to exported fields",
})
}
if len(nodeField.Names) > 1 {
// Report "a, b, c int `river:"name,attr"`" as invalid usage.
p.Report(analysis.Diagnostic{
Pos: field.Pos(),
Category: "rivertags",
Category: "syntaxtags",
Message: "river tags should not be inserted on field names separated by commas",
})
}

for _, match := range matches {
diagnostics := lintRiverTag(field, match[1])
diagnostics := lintSyntaxTag(field, match[1])
for _, diag := range diagnostics {
p.Report(analysis.Diagnostic{
Pos: field.Pos(),
Category: "rivertags",
Category: "syntaxtags",
Message: diag,
})
}
Expand All @@ -149,7 +149,7 @@ func lintingDisabled(comment string) bool {
for _, match := range matches {
// Iterate over A,B,C by comma and see if our linter is included.
for _, disabledLinter := range strings.Split(match[1], ",") {
if disabledLinter == "rivertags" {
if disabledLinter == "syntaxtags" {
return true
}
}
Expand Down Expand Up @@ -216,7 +216,7 @@ type structInfo struct {
Type *types.Struct
}

func lintRiverTag(ty *types.Var, tag string) (diagnostics []string) {
func lintSyntaxTag(ty *types.Var, tag string) (diagnostics []string) {
if tag == "" {
diagnostics = append(diagnostics, "river tag should not be empty")
return
Expand Down Expand Up @@ -319,7 +319,7 @@ var fieldNameRegex = regexp.MustCompile("^[a-z][a-z0-9_]*$")

func validateFieldName(name string) (diagnostics []string) {
if !fieldNameRegex.MatchString(name) {
msg := fmt.Sprintf("%q must be a valid river snake_case identifier", name)
msg := fmt.Sprintf("%q must be a valid syntax snake_case identifier", name)
diagnostics = append(diagnostics, msg)
}

Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/agentlint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ package main

import (
"github.com/grafana/agent/internal/cmd/agentlint/internal/findcomponents"
"github.com/grafana/agent/internal/cmd/agentlint/internal/rivertags"
"github.com/grafana/agent/internal/cmd/agentlint/internal/syntaxtags"
"golang.org/x/tools/go/analysis/multichecker"
)

func main() {
multichecker.Main(
findcomponents.Analyzer,
rivertags.Analyzer,
syntaxtags.Analyzer,
)
}
12 changes: 6 additions & 6 deletions syntax/encoding/riverjson/riverjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strings"

"github.com/grafana/river/internal/reflectutil"
"github.com/grafana/river/internal/rivertags"
"github.com/grafana/river/internal/syntaxtags"
"github.com/grafana/river/internal/value"
"github.com/grafana/river/token/builder"
)
Expand Down Expand Up @@ -39,7 +39,7 @@ func encodeStructAsBody(rv reflect.Value) jsonBody {

switch rv.Kind() {
case reflect.Struct:
fields := rivertags.Get(rv.Type())
fields := syntaxtags.Get(rv.Type())
defaults := reflect.New(rv.Type()).Elem()
if defaults.CanAddr() && defaults.Addr().Type().Implements(goRiverDefaulter) {
defaults.Addr().Interface().(value.Defaulter).SetToDefault()
Expand Down Expand Up @@ -85,7 +85,7 @@ func encodeStructAsBody(rv reflect.Value) jsonBody {
// encodeFieldAsStatements encodes an individual field from a struct as a set
// of statements. One field may map to multiple statements in the case of a
// slice of blocks.
func encodeFieldAsStatements(prefix []string, field rivertags.Field, fieldValue reflect.Value) []jsonStatement {
func encodeFieldAsStatements(prefix []string, field syntaxtags.Field, fieldValue reflect.Value) []jsonStatement {
fieldName := strings.Join(field.Name, ".")

for fieldValue.Kind() == reflect.Pointer {
Expand Down Expand Up @@ -204,9 +204,9 @@ func mergeStringSlice(a, b []string) []string {

// getBlockLabel returns the label for a given block.
func getBlockLabel(rv reflect.Value) string {
tags := rivertags.Get(rv.Type())
tags := syntaxtags.Get(rv.Type())
for _, tag := range tags {
if tag.Flags&rivertags.FlagLabel != 0 {
if tag.Flags&syntaxtags.FlagLabel != 0 {
return reflectutil.Get(rv, tag).String()
}
}
Expand All @@ -222,7 +222,7 @@ func encodeEnumElementToStatements(prefix []string, enumElement reflect.Value) [
enumElement = enumElement.Elem()
}

fields := rivertags.Get(enumElement.Type())
fields := syntaxtags.Get(enumElement.Type())

statements := []jsonStatement{}

Expand Down
6 changes: 3 additions & 3 deletions syntax/internal/reflectutil/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package reflectutil
import (
"reflect"

"github.com/grafana/river/internal/rivertags"
"github.com/grafana/river/internal/syntaxtags"
)

// GetOrAlloc returns the nested field of value corresponding to index.
// GetOrAlloc panics if not given a struct.
func GetOrAlloc(value reflect.Value, field rivertags.Field) reflect.Value {
func GetOrAlloc(value reflect.Value, field syntaxtags.Field) reflect.Value {
return GetOrAllocIndex(value, field.Index)
}

Expand Down Expand Up @@ -51,7 +51,7 @@ func deferencePointer(value reflect.Value) reflect.Value {
// It is similar to [reflect/Value.FieldByIndex] but can handle traversing
// through nil pointers. If Get traverses through a nil pointer, a non-settable
// zero value for the final field is returned.
func Get(value reflect.Value, field rivertags.Field) reflect.Value {
func Get(value reflect.Value, field syntaxtags.Field) reflect.Value {
if len(field.Index) == 1 {
return value.Field(field.Index[0])
}
Expand Down
8 changes: 4 additions & 4 deletions syntax/internal/reflectutil/walk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"

"github.com/grafana/river/internal/reflectutil"
"github.com/grafana/river/internal/rivertags"
"github.com/grafana/river/internal/syntaxtags"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand All @@ -25,7 +25,7 @@ func TestDeeplyNested_Access(t *testing.T) {
s.Field1.Field2.Field3.Value = "Hello, world!"

rv := reflect.ValueOf(&s).Elem()
innerValue := reflectutil.GetOrAlloc(rv, rivertags.Field{Index: []int{0, 0, 0, 0}})
innerValue := reflectutil.GetOrAlloc(rv, syntaxtags.Field{Index: []int{0, 0, 0, 0}})
assert.True(t, innerValue.CanSet())
assert.Equal(t, reflect.String, innerValue.Kind())
}
Expand All @@ -44,7 +44,7 @@ func TestDeeplyNested_Allocate(t *testing.T) {
var s Struct

rv := reflect.ValueOf(&s).Elem()
innerValue := reflectutil.GetOrAlloc(rv, rivertags.Field{Index: []int{0, 0, 0, 0}})
innerValue := reflectutil.GetOrAlloc(rv, syntaxtags.Field{Index: []int{0, 0, 0, 0}})
require.True(t, innerValue.CanSet())
require.Equal(t, reflect.String, innerValue.Kind())

Expand All @@ -66,7 +66,7 @@ func TestDeeplyNested_NoAllocate(t *testing.T) {
var s Struct

rv := reflect.ValueOf(&s).Elem()
innerValue := reflectutil.Get(rv, rivertags.Field{Index: []int{0, 0, 0, 0}})
innerValue := reflectutil.Get(rv, syntaxtags.Field{Index: []int{0, 0, 0, 0}})
assert.False(t, innerValue.CanSet())
assert.Equal(t, reflect.String, innerValue.Kind())
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Package rivertags decodes a struct type into river object
// and structural tags.
package rivertags
// Package syntaxtags decodes a struct type into syntax object and structural
// tags.
package syntaxtags

import (
"fmt"
Expand Down Expand Up @@ -159,7 +159,7 @@ func Get(ty reflect.Type) []Field {
)

for _, field := range reflect.VisibleFields(ty) {
// River does not support embedding of fields
// Alloy's syntax does not support embedding of fields
if field.Anonymous {
panic(fmt.Sprintf("syntax: anonymous fields not supported %s", printPathToField(ty, field.Index)))
}
Expand Down
Loading
Loading