Skip to content

Commit

Permalink
syntax: rename river tags to alloy
Browse files Browse the repository at this point in the history
  • Loading branch information
rfratto committed Mar 25, 2024
1 parent ad62898 commit b6a163d
Show file tree
Hide file tree
Showing 17 changed files with 283 additions and 283 deletions.
28 changes: 14 additions & 14 deletions syntax/encoding/alloyjson/alloyjson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func TestBlock(t *testing.T) {

func TestBlock_Empty_Required_Block_Slice(t *testing.T) {
type wrapper struct {
Blocks []testBlock `river:"some_block,block"`
Blocks []testBlock `alloy:"some_block,block"`
}

tt := []struct {
Expand All @@ -224,19 +224,19 @@ func TestBlock_Empty_Required_Block_Slice(t *testing.T) {
}

type testBlock struct {
Number int `river:"number,attr,optional"`
String string `river:"string,attr,optional"`
Boolean bool `river:"boolean,attr,optional"`
Array []any `river:"array,attr,optional"`
Object map[string]any `river:"object,attr,optional"`

Labeled []labeledBlock `river:"labeled_block,block,optional"`
Blocks []testBlock `river:"inner_block,block,optional"`
Number int `alloy:"number,attr,optional"`
String string `alloy:"string,attr,optional"`
Boolean bool `alloy:"boolean,attr,optional"`
Array []any `alloy:"array,attr,optional"`
Object map[string]any `alloy:"object,attr,optional"`

Labeled []labeledBlock `alloy:"labeled_block,block,optional"`
Blocks []testBlock `alloy:"inner_block,block,optional"`
}

type labeledBlock struct {
TestBlock testBlock `river:",squash"`
Label string `river:",label"`
TestBlock testBlock `alloy:",squash"`
Label string `alloy:",label"`
}

func TestNilBody(t *testing.T) {
Expand Down Expand Up @@ -300,8 +300,8 @@ func TestHideDefaults(t *testing.T) {
}

type defaultsBlock struct {
Name string `river:"name,attr,optional"`
Age int `river:"age,attr,optional"`
Name string `alloy:"name,attr,optional"`
Age int `alloy:"age,attr,optional"`
}

var _ syntax.Defaulter = (*defaultsBlock)(nil)
Expand All @@ -315,7 +315,7 @@ func (d *defaultsBlock) SetToDefault() {

func TestMapBlocks(t *testing.T) {
type block struct {
Value map[string]any `river:"block,block,optional"`
Value map[string]any `alloy:"block,block,optional"`
}
val := block{Value: map[string]any{"field": "value"}}

Expand Down
30 changes: 15 additions & 15 deletions syntax/internal/syntaxtags/syntaxtags.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,43 +109,43 @@ func (f Field) IsLabel() bool { return f.Flags&FlagLabel != 0 }
// Get returns the list of tagged fields for some struct type ty. Get panics if
// ty is not a struct type.
//
// Get examines each tagged field in ty for a river key. The river key is then
// Get examines each tagged field in ty for an alloy key. The alloy key is then
// parsed as containing a name for the field, followed by a required
// comma-separated list of options. The name may be empty for fields which do
// not require a name. Get will ignore any field that is not tagged with a
// river key.
// not require a name. Get will ignore any field that is not tagged with an
// alloy key.
//
// Get will treat anonymous struct fields as if the inner fields were fields in
// the outer struct.
//
// Examples of struct field tags and their meanings:
//
// // Field is used as a required block named "my_block".
// Field struct{} `river:"my_block,block"`
// Field struct{} `alloy:"my_block,block"`
//
// // Field is used as an optional block named "my_block".
// Field struct{} `river:"my_block,block,optional"`
// Field struct{} `alloy:"my_block,block,optional"`
//
// // Field is used as a required attribute named "my_attr".
// Field string `river:"my_attr,attr"`
// Field string `alloy:"my_attr,attr"`
//
// // Field is used as an optional attribute named "my_attr".
// Field string `river:"my_attr,attr,optional"`
// Field string `alloy:"my_attr,attr,optional"`
//
// // Field is used for storing the label of the block which the struct
// // represents.
// Field string `river:",label"`
// Field string `alloy:",label"`
//
// // Attributes and blocks inside of Field are exposed as top-level fields.
// Field struct{} `river:",squash"`
// Field struct{} `alloy:",squash"`
//
// Blocks []struct{} `river:"my_block_prefix,enum"`
// Blocks []struct{} `alloy:"my_block_prefix,enum"`
//
// With the exception of the `river:",label"` and `river:",squash" tags, all
// With the exception of the `alloy:",label"` and `alloy:",squash" tags, all
// tagged fields must have a unique name.
//
// The type of tagged fields may be any Go type, with the exception of
// `river:",label"` tags, which must be strings.
// `alloy:",label"` tags, which must be strings.
func Get(ty reflect.Type) []Field {
if k := ty.Kind(); k != reflect.Struct {
panic(fmt.Sprintf("syntaxtags: Get requires struct kind, got %s", k))
Expand All @@ -164,13 +164,13 @@ func Get(ty reflect.Type) []Field {
panic(fmt.Sprintf("syntax: anonymous fields not supported %s", printPathToField(ty, field.Index)))
}

tag, tagged := field.Tag.Lookup("river")
tag, tagged := field.Tag.Lookup("alloy")
if !tagged {
continue
}

if !field.IsExported() {
panic(fmt.Sprintf("syntax: river tag found on unexported field at %s", printPathToField(ty, field.Index)))
panic(fmt.Sprintf("syntax: alloy tag found on unexported field at %s", printPathToField(ty, field.Index)))
}

options := strings.SplitN(tag, ",", 2)
Expand All @@ -195,7 +195,7 @@ func Get(ty reflect.Type) []Field {

flags, ok := parseFlags(options[1])
if !ok {
panic(fmt.Sprintf("syntax: unrecognized river tag format %q at %s", tag, printPathToField(ty, tf.Index)))
panic(fmt.Sprintf("syntax: unrecognized alloy tag format %q at %s", tag, printPathToField(ty, tf.Index)))
}
tf.Flags = flags

Expand Down
62 changes: 31 additions & 31 deletions syntax/internal/syntaxtags/syntaxtags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ func Test_Get(t *testing.T) {
type Struct struct {
IgnoreMe bool

ReqAttr string `river:"req_attr,attr"`
OptAttr string `river:"opt_attr,attr,optional"`
ReqBlock struct{} `river:"req_block,block"`
OptBlock struct{} `river:"opt_block,block,optional"`
ReqEnum []struct{} `river:"req_enum,enum"`
OptEnum []struct{} `river:"opt_enum,enum,optional"`
Label string `river:",label"`
ReqAttr string `alloy:"req_attr,attr"`
OptAttr string `alloy:"opt_attr,attr,optional"`
ReqBlock struct{} `alloy:"req_block,block"`
OptBlock struct{} `alloy:"opt_block,block,optional"`
ReqEnum []struct{} `alloy:"req_enum,enum"`
OptEnum []struct{} `alloy:"opt_enum,enum,optional"`
Label string `alloy:",label"`
}

fs := syntaxtags.Get(reflect.TypeOf(Struct{}))
Expand All @@ -39,34 +39,34 @@ func Test_Get(t *testing.T) {

func TestEmbedded(t *testing.T) {
type InnerStruct struct {
InnerField1 string `river:"inner_field_1,attr"`
InnerField2 string `river:"inner_field_2,attr"`
InnerField1 string `alloy:"inner_field_1,attr"`
InnerField2 string `alloy:"inner_field_2,attr"`
}

type Struct struct {
Field1 string `river:"parent_field_1,attr"`
Field1 string `alloy:"parent_field_1,attr"`
InnerStruct
Field2 string `river:"parent_field_2,attr"`
Field2 string `alloy:"parent_field_2,attr"`
}
require.PanicsWithValue(t, "syntax: anonymous fields not supported syntaxtags_test.Struct.InnerStruct", func() { syntaxtags.Get(reflect.TypeOf(Struct{})) })
}

func TestSquash(t *testing.T) {
type InnerStruct struct {
InnerField1 string `river:"inner_field_1,attr"`
InnerField2 string `river:"inner_field_2,attr"`
InnerField1 string `alloy:"inner_field_1,attr"`
InnerField2 string `alloy:"inner_field_2,attr"`
}

type Struct struct {
Field1 string `river:"parent_field_1,attr"`
Inner InnerStruct `river:",squash"`
Field2 string `river:"parent_field_2,attr"`
Field1 string `alloy:"parent_field_1,attr"`
Inner InnerStruct `alloy:",squash"`
Field2 string `alloy:"parent_field_2,attr"`
}

type StructWithPointer struct {
Field1 string `river:"parent_field_1,attr"`
Inner *InnerStruct `river:",squash"`
Field2 string `river:"parent_field_2,attr"`
Field1 string `alloy:"parent_field_1,attr"`
Inner *InnerStruct `alloy:",squash"`
Field2 string `alloy:"parent_field_2,attr"`
}

expect := []syntaxtags.Field{
Expand Down Expand Up @@ -101,16 +101,16 @@ func TestSquash(t *testing.T) {

func TestDeepSquash(t *testing.T) {
type Inner2Struct struct {
InnerField1 string `river:"inner_field_1,attr"`
InnerField2 string `river:"inner_field_2,attr"`
InnerField1 string `alloy:"inner_field_1,attr"`
InnerField2 string `alloy:"inner_field_2,attr"`
}

type InnerStruct struct {
Inner2Struct Inner2Struct `river:",squash"`
Inner2Struct Inner2Struct `alloy:",squash"`
}

type Struct struct {
Inner InnerStruct `river:",squash"`
Inner InnerStruct `alloy:",squash"`
}

expect := []syntaxtags.Field{
Expand Down Expand Up @@ -140,41 +140,41 @@ func Test_Get_Panics(t *testing.T) {

t.Run("Tagged fields must be exported", func(t *testing.T) {
type Struct struct {
attr string `river:"field,attr"` // nolint:unused //nolint:syntaxtags
attr string `alloy:"field,attr"` // nolint:unused //nolint:syntaxtags
}
expect := `syntax: river tag found on unexported field at syntaxtags_test.Struct.attr`
expect := `syntax: alloy tag found on unexported field at syntaxtags_test.Struct.attr`
expectPanic(t, expect, Struct{})
})

t.Run("Options are required", func(t *testing.T) {
type Struct struct {
Attr string `river:"field"` //nolint:syntaxtags
Attr string `alloy:"field"` //nolint:syntaxtags
}
expect := `syntax: field syntaxtags_test.Struct.Attr tag is missing options`
expectPanic(t, expect, Struct{})
})

t.Run("Field names must be unique", func(t *testing.T) {
type Struct struct {
Attr string `river:"field1,attr"`
Block string `river:"field1,block,optional"` //nolint:syntaxtags
Attr string `alloy:"field1,attr"`
Block string `alloy:"field1,block,optional"` //nolint:syntaxtags
}
expect := `syntax: field name field1 already used by syntaxtags_test.Struct.Attr`
expectPanic(t, expect, Struct{})
})

t.Run("Name is required for non-label field", func(t *testing.T) {
type Struct struct {
Attr string `river:",attr"` //nolint:syntaxtags
Attr string `alloy:",attr"` //nolint:syntaxtags
}
expect := `syntaxtags: non-empty field name required at syntaxtags_test.Struct.Attr`
expectPanic(t, expect, Struct{})
})

t.Run("Only one label field may exist", func(t *testing.T) {
type Struct struct {
Label1 string `river:",label"`
Label2 string `river:",label"`
Label1 string `alloy:",label"`
Label2 string `alloy:",label"`
}
expect := `syntax: label field already used by syntaxtags_test.Struct.Label2`
expectPanic(t, expect, Struct{})
Expand Down
Loading

0 comments on commit b6a163d

Please sign in to comment.