Skip to content

Commit a20e523

Browse files
committed
all: make use of stringer in more places
This replaces about 100 lines of manually maintained code with about 190 lines of automatically generated code with stringer. The strings are now the constant names themselves, or in the case of `stringer -linecomment`, in inline comments following the names. Signed-off-by: Daniel Martí <[email protected]> Change-Id: I90e0463fcf82ebe2d6cfd43f90aed4442f71eb16 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1199854 Unity-Result: CUE porcuepine <[email protected]> Reviewed-by: Matthew Sackman <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
1 parent b5ac87c commit a20e523

12 files changed

+196
-111
lines changed

cue/token/position.go

+8-12
Original file line numberDiff line numberDiff line change
@@ -115,36 +115,32 @@ var NoPos = Pos{}
115115
// RelPos indicates the relative position of token to the previous token.
116116
type RelPos int
117117

118+
//go:generate go run golang.org/x/tools/cmd/stringer -type=RelPos -linecomment
119+
118120
const (
119121
// NoRelPos indicates no relative position is specified.
120-
NoRelPos RelPos = iota
122+
NoRelPos RelPos = iota // invalid
121123

122124
// Elided indicates that the token for which this position is defined is
123125
// not rendered at all.
124-
Elided
126+
Elided // elided
125127

126128
// NoSpace indicates there is no whitespace before this token.
127-
NoSpace
129+
NoSpace // nospace
128130

129131
// Blank means there is horizontal space before this token.
130-
Blank
132+
Blank // blank
131133

132134
// Newline means there is a single newline before this token.
133-
Newline
135+
Newline // newline
134136

135137
// NewSection means there are two or more newlines before this token.
136-
NewSection
138+
NewSection // section
137139

138140
relMask = 0xf
139141
relShift = 4
140142
)
141143

142-
var relNames = []string{
143-
"invalid", "elided", "nospace", "blank", "newline", "section",
144-
}
145-
146-
func (p RelPos) String() string { return relNames[p] }
147-
148144
func (p RelPos) Pos() Pos {
149145
return Pos{nil, int(p)}
150146
}

cue/token/relpos_string.go

+28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/core/adt/composite.go

+2-19
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,8 @@ func (s *StructInfo) useForAccept() bool {
499499
// vertexStatus indicates the evaluation progress of a Vertex.
500500
type vertexStatus int8
501501

502+
//go:generate go run golang.org/x/tools/cmd/stringer -type=vertexStatus
503+
502504
const (
503505
// unprocessed indicates a Vertex has not been processed before.
504506
// Value must be nil.
@@ -529,25 +531,6 @@ const (
529531
finalized
530532
)
531533

532-
func (s vertexStatus) String() string {
533-
switch s {
534-
case unprocessed:
535-
return "unprocessed"
536-
case evaluating:
537-
return "evaluating"
538-
case partial:
539-
return "partial"
540-
case conjuncts:
541-
return "conjuncts"
542-
case evaluatingArcs:
543-
return "evaluatingArcs"
544-
case finalized:
545-
return "finalized"
546-
default:
547-
return "unknown"
548-
}
549-
}
550-
551534
func (v *Vertex) Status() vertexStatus {
552535
return v.status
553536
}

internal/core/adt/debug.go

+2-31
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ func openDebugGraph(ctx *OpContext, v *Vertex, name string) {
152152
// dependencies are balanced.
153153
type depKind int
154154

155+
//go:generate go run golang.org/x/tools/cmd/stringer -type=depKind
156+
155157
const (
156158
// PARENT dependencies are used to track the completion of parent
157159
// closedContexts within the closedness tree.
@@ -200,37 +202,6 @@ const (
200202
TEST // Always refers to self.
201203
)
202204

203-
func (k depKind) String() string {
204-
switch k {
205-
case PARENT:
206-
return "PARENT"
207-
case ARC:
208-
return "ARC"
209-
case NOTIFY:
210-
return "NOTIFY"
211-
case TASK:
212-
return "TASK"
213-
case DISJUNCT:
214-
return "DISJUNCT"
215-
case EVAL:
216-
return "EVAL"
217-
case COMP:
218-
return "COMP"
219-
case ROOT:
220-
return "ROOT"
221-
222-
case INIT:
223-
return "INIT"
224-
case DEFER:
225-
return "DEFER"
226-
case SHARED:
227-
return "SHARED"
228-
case TEST:
229-
return "TEST"
230-
}
231-
panic("unreachable")
232-
}
233-
234205
// ccDep is used to record counters which is used for debugging only.
235206
// It is purpose is to be precise about matching inc/dec as well as to be able
236207
// to traverse dependency.

internal/core/adt/depkind_string.go

+35
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/core/adt/errorcode_string.go

+27
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/core/adt/errors.go

+7-21
Original file line numberDiff line numberDiff line change
@@ -42,45 +42,31 @@ import (
4242
// control flow. No other aspects of an error may influence control flow.
4343
type ErrorCode int8
4444

45+
//go:generate go run golang.org/x/tools/cmd/stringer -type=ErrorCode -linecomment
46+
4547
const (
4648
// An EvalError is a fatal evaluation error.
47-
EvalError ErrorCode = iota
49+
EvalError ErrorCode = iota // eval
4850

4951
// A UserError is a fatal error originating from the user.
50-
UserError
52+
UserError // user
5153

5254
// StructuralCycleError means a structural cycle was found. Structural
5355
// cycles are permanent errors, but they are not passed up recursively,
5456
// as a unification of a value with a structural cycle with one that
5557
// doesn't may still give a useful result.
56-
StructuralCycleError
58+
StructuralCycleError // structural cycle
5759

5860
// IncompleteError means an evaluation could not complete because of
5961
// insufficient information that may still be added later.
60-
IncompleteError
62+
IncompleteError // incomplete
6163

6264
// A CycleError indicates a reference error. It is considered to be
6365
// an incomplete error, as reference errors may be broken by providing
6466
// a concrete value.
65-
CycleError
67+
CycleError // cycle
6668
)
6769

68-
func (c ErrorCode) String() string {
69-
switch c {
70-
case EvalError:
71-
return "eval"
72-
case UserError:
73-
return "user"
74-
case StructuralCycleError:
75-
return "structural cycle"
76-
case IncompleteError:
77-
return "incomplete"
78-
case CycleError:
79-
return "cycle"
80-
}
81-
return "unknown"
82-
}
83-
8470
// Bottom represents an error or bottom symbol.
8571
//
8672
// Although a Bottom node holds control data, it should not be created until the

internal/core/adt/runmode_string.go

+27
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/core/adt/sched.go

+2-14
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ func (s schedState) String() string {
176176
// runMode indicates how to proceed after a condition could not be met.
177177
type runMode uint8
178178

179+
//go:generate go run golang.org/x/tools/cmd/stringer -type=runMode
180+
179181
const (
180182
// ignore indicates that the new evaluator should not do any processing.
181183
// This is mostly used in the transition from old to new evaluator and
@@ -196,20 +198,6 @@ const (
196198
finalize
197199
)
198200

199-
func (r runMode) String() string {
200-
switch r {
201-
case ignore:
202-
return "ignore"
203-
case attemptOnly:
204-
return "attemptOnly"
205-
case yield:
206-
return "yield"
207-
case finalize:
208-
return "finalize"
209-
}
210-
return "unknown"
211-
}
212-
213201
// condition is a bit mask of states that a task may depend on.
214202
//
215203
// There are generally two types of states: states that are met if all tasks

internal/core/adt/vertexstatus_string.go

+28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/httplog/event.go

+5-14
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,17 @@ type Logger interface {
1313

1414
type EventKind int
1515

16+
//go:generate go run golang.org/x/tools/cmd/stringer -type=EventKind -linecomment
17+
1618
const (
17-
NoEvent EventKind = iota
18-
KindClientSendRequest
19-
KindClientRecvResponse
19+
NoEvent EventKind = iota
20+
KindClientSendRequest // http client->
21+
KindClientRecvResponse // http client<-
2022

2123
// TODO KindServerRecvRequest
2224
// TODO KindServerSendResponse
2325
)
2426

25-
func (k EventKind) String() string {
26-
switch k {
27-
case KindClientSendRequest:
28-
return "http client->"
29-
case KindClientRecvResponse:
30-
return "http client<-"
31-
default:
32-
return "unknown"
33-
}
34-
}
35-
3627
// Request represents an HTTP request.
3728
type Request struct {
3829
ID int64 `json:"id"`

0 commit comments

Comments
 (0)