Skip to content

Commit f03161d

Browse files
committed
cue/parser: attach attributes to correct field
Right now they are attached to the first label in a label sequence, but it should be the last. To avoid any doubt, make the formatter insert curly braces for such cases. This also fixes a bug where attributes at the end of a multi-label field were attached to the wrong field and discarded when formatting. Change-Id: If19e2b95dd7c5c69a3523ce46e8a30a47609d70c Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2706 Reviewed-by: Marcel van Lohuizen <[email protected]>
1 parent 0265ac2 commit f03161d

File tree

14 files changed

+99
-35
lines changed

14 files changed

+99
-35
lines changed

cue/format/node.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func (f *formatter) decl(decl ast.Decl) {
138138
// Lbrace does not signal the intend to collapse fields.
139139
for n.Label.Pos().IsValid() || f.printer.cfg.simplify {
140140
obj, ok := n.Value.(*ast.StructLit)
141-
if !ok || len(obj.Elts) != 1 || (obj.Lbrace.IsValid() && !f.printer.cfg.simplify) {
141+
if !ok || len(obj.Elts) != 1 || (obj.Lbrace.IsValid() && !f.printer.cfg.simplify) || len(n.Attrs) > 0 {
142142
break
143143
}
144144

@@ -155,7 +155,7 @@ func (f *formatter) decl(decl ast.Decl) {
155155
}
156156

157157
mem, ok := obj.Elts[0].(*ast.Field)
158-
if !ok {
158+
if !ok || len(mem.Attrs) > 0 {
159159
break
160160
}
161161
entry := labelEntry{mem.Label, mem.Optional != token.NoPos}

cue/format/testdata/expressions.golden

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ package expressions
3636
bbbb: 100 @go(Bbbb) /* a */ @xml(,attr) // comment
3737
}
3838

39+
foo: {bar: string @go(-)}
40+
3941
e: 1 + 2*3
4042
e: 1 * 2 * 3 // error
4143
e: >=2 & <=3

cue/format/testdata/expressions.input

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ package expressions
3636
bbbb: 100 @go(Bbbb) /* a */ @xml(,attr) // comment
3737
}
3838

39+
foo bar: string @go(-)
40+
3941
e: 1+2*3
4042
e: 1*2*3 // error
4143
e: >=2 & <=3

cue/format/testdata/simplify.input

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
a "B": 42
44

5-
"a.b" "foo-" "cc_dd": x
5+
"a.b" "foo-" "cc_dd": x

cue/parser/parser.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ func (p *parser) parseField(allowEmit bool) (decl ast.Decl) {
762762
a := &ast.Attribute{At: p.pos, Text: p.lit}
763763
p.next()
764764
c.closeNode(p, a)
765-
this.Attrs = append(this.Attrs, a)
765+
m.Attrs = append(m.Attrs, a)
766766
}
767767
p.closeList()
768768

@@ -775,7 +775,7 @@ func (p *parser) parseField(allowEmit bool) (decl ast.Decl) {
775775

776776
case token.FOR, token.IF:
777777
if !allowComprehension {
778-
p.errf(p.pos, "comprehension not alowed for this field")
778+
p.errf(p.pos, "comprehension not allowed for this field")
779779
}
780780
clauses := p.parseComprehensionClauses()
781781
return &ast.ComprehensionDecl{

encoding/protobuf/testdata/attributes.proto.out.cue

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ StructWrap: {
6565
// 3) Forward attributes from client proxy to server proxy for HTTP requests.
6666
Attributes: {
6767
// A map of attribute name to its value.
68-
attributes <_>: Attributes_AttributeValue
68+
attributes: {
69+
<_>: Attributes_AttributeValue
70+
} @protobuf(1,type=map<string,AttributeValue>)
6971
}
7072

7173
// Specifies one attribute value with different type.
@@ -101,7 +103,9 @@ Attributes_AttributeValue: {
101103
// Defines a string map.
102104
Attributes_StringMap: {
103105
// Holds a set of name/value pairs.
104-
entries <_>: string
106+
entries: {
107+
<_>: string
108+
} @protobuf(1,type=map<string,string>)
105109
}
106110

107111
// Defines a list of attributes in compressed format optimized for transport.
@@ -116,33 +120,51 @@ CompressedAttributes: {
116120
words?: [...string] @protobuf(1)
117121

118122
// Holds attributes of type STRING, DNS_NAME, EMAIL_ADDRESS, URI
119-
strings <_>: int32
123+
strings: {
124+
<_>: int32
125+
} @protobuf(2,type=map<sint32,sint32>)
120126

121127
// Holds attributes of type INT64
122-
int64s <_>: int64
128+
int64s: {
129+
<_>: int64
130+
} @protobuf(3,type=map<sint32,int64>)
123131

124132
// Holds attributes of type DOUBLE
125-
doubles <_>: float64
133+
doubles: {
134+
<_>: float64
135+
} @protobuf(4,type=map<sint32,double>)
126136

127137
// Holds attributes of type BOOL
128-
bools <_>: bool
138+
bools: {
139+
<_>: bool
140+
} @protobuf(5,type=map<sint32,bool>)
129141

130142
// Holds attributes of type TIMESTAMP
131-
timestamps <_>: timestamp.Timestamp
143+
timestamps: {
144+
<_>: timestamp.Timestamp
145+
} @protobuf(6,type=map<sint32,google.protobuf.Timestamp>,"(gogoproto.nullable)=false","(gogoproto.stdtime)")
132146

133147
// Holds attributes of type DURATION
134-
durations <_>: duration.Duration
148+
durations: {
149+
<_>: duration.Duration
150+
} @protobuf(7,type=map<sint32,google.protobuf.Duration>,"(gogoproto.nullable)=false","(gogoproto.stdduration)")
135151

136152
// Holds attributes of type BYTES
137-
bytes <_>: bytes
153+
bytes: {
154+
<_>: bytes
155+
} @protobuf(8,type=map<sint32,bytes>)
138156

139157
// Holds attributes of type STRING_MAP
140-
stringMaps <_>: StringMap
158+
stringMaps: {
159+
<_>: StringMap
160+
} @protobuf(9,type=map<sint32,StringMap>,string_maps,"(gogoproto.nullable)=false")
141161
}
142162

143163
// A map of string to string. The keys and values in this map are dictionary
144164
// indices (see the [Attributes][istio.mixer.v1.CompressedAttributes] message for an explanation)
145165
StringMap: {
146166
// Holds a set of name/value pairs.
147-
entries <_>: int32
167+
entries: {
168+
<_>: int32
169+
} @protobuf(1,type=map<sint32,sint32>)
148170
}

encoding/protobuf/testdata/client_config.proto.out.cue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,9 @@ HttpClientConfig: {
144144
// Map of control configuration indexed by destination.service. This
145145
// is used to support per-service configuration for cases where a
146146
// mixerclient serves multiple services.
147-
serviceConfigs <_>: ServiceConfig
147+
serviceConfigs: {
148+
<_>: ServiceConfig
149+
} @protobuf(2,type=map<string,ServiceConfig>,service_configs)
148150

149151
// Default destination service name if none was specified in the
150152
// client request.

encoding/protobuf/testdata/gateway.proto.out.cue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,9 @@ Gateway: {
213213
// label search is restricted to the configuration namespace in which the
214214
// the resource is present. In other words, the Gateway resource must
215215
// reside in the same namespace as the gateway workload instance.
216-
selector <_>: string
216+
selector: {
217+
<_>: string
218+
} @protobuf(2,type=map<string,string>)
217219
selector? <name>: name
218220
}
219221

encoding/protobuf/testdata/istio.io/api/mixer/v1/attributes_proto_gen.cue

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ StructWrap: {
6565
// 3) Forward attributes from client proxy to server proxy for HTTP requests.
6666
Attributes: {
6767
// A map of attribute name to its value.
68-
attributes <_>: Attributes_AttributeValue
68+
attributes: {
69+
<_>: Attributes_AttributeValue
70+
} @protobuf(1,type=map<string,AttributeValue>)
6971
}
7072

7173
// Specifies one attribute value with different type.
@@ -101,7 +103,9 @@ Attributes_AttributeValue: {
101103
// Defines a string map.
102104
Attributes_StringMap: {
103105
// Holds a set of name/value pairs.
104-
entries <_>: string
106+
entries: {
107+
<_>: string
108+
} @protobuf(1,type=map<string,string>)
105109
}
106110

107111
// Defines a list of attributes in compressed format optimized for transport.
@@ -116,33 +120,51 @@ CompressedAttributes: {
116120
words?: [...string] @protobuf(1)
117121

118122
// Holds attributes of type STRING, DNS_NAME, EMAIL_ADDRESS, URI
119-
strings <_>: int32
123+
strings: {
124+
<_>: int32
125+
} @protobuf(2,type=map<sint32,sint32>)
120126

121127
// Holds attributes of type INT64
122-
int64s <_>: int64
128+
int64s: {
129+
<_>: int64
130+
} @protobuf(3,type=map<sint32,int64>)
123131

124132
// Holds attributes of type DOUBLE
125-
doubles <_>: float64
133+
doubles: {
134+
<_>: float64
135+
} @protobuf(4,type=map<sint32,double>)
126136

127137
// Holds attributes of type BOOL
128-
bools <_>: bool
138+
bools: {
139+
<_>: bool
140+
} @protobuf(5,type=map<sint32,bool>)
129141

130142
// Holds attributes of type TIMESTAMP
131-
timestamps <_>: timestamp.Timestamp
143+
timestamps: {
144+
<_>: timestamp.Timestamp
145+
} @protobuf(6,type=map<sint32,google.protobuf.Timestamp>,"(gogoproto.nullable)=false","(gogoproto.stdtime)")
132146

133147
// Holds attributes of type DURATION
134-
durations <_>: duration.Duration
148+
durations: {
149+
<_>: duration.Duration
150+
} @protobuf(7,type=map<sint32,google.protobuf.Duration>,"(gogoproto.nullable)=false","(gogoproto.stdduration)")
135151

136152
// Holds attributes of type BYTES
137-
bytes <_>: bytes
153+
bytes: {
154+
<_>: bytes
155+
} @protobuf(8,type=map<sint32,bytes>)
138156

139157
// Holds attributes of type STRING_MAP
140-
stringMaps <_>: StringMap
158+
stringMaps: {
159+
<_>: StringMap
160+
} @protobuf(9,type=map<sint32,StringMap>,string_maps,"(gogoproto.nullable)=false")
141161
}
142162

143163
// A map of string to string. The keys and values in this map are dictionary
144164
// indices (see the [Attributes][istio.mixer.v1.CompressedAttributes] message for an explanation)
145165
StringMap: {
146166
// Holds a set of name/value pairs.
147-
entries <_>: int32
167+
entries: {
168+
<_>: int32
169+
} @protobuf(1,type=map<sint32,sint32>)
148170
}

encoding/protobuf/testdata/istio.io/api/mixer/v1/config/client/client_config_proto_gen.cue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,9 @@ HttpClientConfig: {
143143
// Map of control configuration indexed by destination.service. This
144144
// is used to support per-service configuration for cases where a
145145
// mixerclient serves multiple services.
146-
serviceConfigs <_>: ServiceConfig
146+
serviceConfigs: {
147+
<_>: ServiceConfig
148+
} @protobuf(2,type=map<string,ServiceConfig>,service_configs)
147149

148150
// Default destination service name if none was specified in the
149151
// client request.

0 commit comments

Comments
 (0)