Skip to content

Commit

Permalink
all: make use of stringer in more places
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
mvdan committed Aug 22, 2024
1 parent b5ac87c commit a20e523
Show file tree
Hide file tree
Showing 12 changed files with 196 additions and 111 deletions.
20 changes: 8 additions & 12 deletions cue/token/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,36 +115,32 @@ var NoPos = Pos{}
// RelPos indicates the relative position of token to the previous token.
type RelPos int

//go:generate go run golang.org/x/tools/cmd/stringer -type=RelPos -linecomment

const (
// NoRelPos indicates no relative position is specified.
NoRelPos RelPos = iota
NoRelPos RelPos = iota // invalid

// Elided indicates that the token for which this position is defined is
// not rendered at all.
Elided
Elided // elided

// NoSpace indicates there is no whitespace before this token.
NoSpace
NoSpace // nospace

// Blank means there is horizontal space before this token.
Blank
Blank // blank

// Newline means there is a single newline before this token.
Newline
Newline // newline

// NewSection means there are two or more newlines before this token.
NewSection
NewSection // section

relMask = 0xf
relShift = 4
)

var relNames = []string{
"invalid", "elided", "nospace", "blank", "newline", "section",
}

func (p RelPos) String() string { return relNames[p] }

func (p RelPos) Pos() Pos {
return Pos{nil, int(p)}
}
Expand Down
28 changes: 28 additions & 0 deletions cue/token/relpos_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 2 additions & 19 deletions internal/core/adt/composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,8 @@ func (s *StructInfo) useForAccept() bool {
// vertexStatus indicates the evaluation progress of a Vertex.
type vertexStatus int8

//go:generate go run golang.org/x/tools/cmd/stringer -type=vertexStatus

const (
// unprocessed indicates a Vertex has not been processed before.
// Value must be nil.
Expand Down Expand Up @@ -529,25 +531,6 @@ const (
finalized
)

func (s vertexStatus) String() string {
switch s {
case unprocessed:
return "unprocessed"
case evaluating:
return "evaluating"
case partial:
return "partial"
case conjuncts:
return "conjuncts"
case evaluatingArcs:
return "evaluatingArcs"
case finalized:
return "finalized"
default:
return "unknown"
}
}

func (v *Vertex) Status() vertexStatus {
return v.status
}
Expand Down
33 changes: 2 additions & 31 deletions internal/core/adt/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ func openDebugGraph(ctx *OpContext, v *Vertex, name string) {
// dependencies are balanced.
type depKind int

//go:generate go run golang.org/x/tools/cmd/stringer -type=depKind

const (
// PARENT dependencies are used to track the completion of parent
// closedContexts within the closedness tree.
Expand Down Expand Up @@ -200,37 +202,6 @@ const (
TEST // Always refers to self.
)

func (k depKind) String() string {
switch k {
case PARENT:
return "PARENT"
case ARC:
return "ARC"
case NOTIFY:
return "NOTIFY"
case TASK:
return "TASK"
case DISJUNCT:
return "DISJUNCT"
case EVAL:
return "EVAL"
case COMP:
return "COMP"
case ROOT:
return "ROOT"

case INIT:
return "INIT"
case DEFER:
return "DEFER"
case SHARED:
return "SHARED"
case TEST:
return "TEST"
}
panic("unreachable")
}

// ccDep is used to record counters which is used for debugging only.
// It is purpose is to be precise about matching inc/dec as well as to be able
// to traverse dependency.
Expand Down
35 changes: 35 additions & 0 deletions internal/core/adt/depkind_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions internal/core/adt/errorcode_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 7 additions & 21 deletions internal/core/adt/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,45 +42,31 @@ import (
// control flow. No other aspects of an error may influence control flow.
type ErrorCode int8

//go:generate go run golang.org/x/tools/cmd/stringer -type=ErrorCode -linecomment

const (
// An EvalError is a fatal evaluation error.
EvalError ErrorCode = iota
EvalError ErrorCode = iota // eval

// A UserError is a fatal error originating from the user.
UserError
UserError // user

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

// IncompleteError means an evaluation could not complete because of
// insufficient information that may still be added later.
IncompleteError
IncompleteError // incomplete

// A CycleError indicates a reference error. It is considered to be
// an incomplete error, as reference errors may be broken by providing
// a concrete value.
CycleError
CycleError // cycle
)

func (c ErrorCode) String() string {
switch c {
case EvalError:
return "eval"
case UserError:
return "user"
case StructuralCycleError:
return "structural cycle"
case IncompleteError:
return "incomplete"
case CycleError:
return "cycle"
}
return "unknown"
}

// Bottom represents an error or bottom symbol.
//
// Although a Bottom node holds control data, it should not be created until the
Expand Down
27 changes: 27 additions & 0 deletions internal/core/adt/runmode_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 2 additions & 14 deletions internal/core/adt/sched.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ func (s schedState) String() string {
// runMode indicates how to proceed after a condition could not be met.
type runMode uint8

//go:generate go run golang.org/x/tools/cmd/stringer -type=runMode

const (
// ignore indicates that the new evaluator should not do any processing.
// This is mostly used in the transition from old to new evaluator and
Expand All @@ -196,20 +198,6 @@ const (
finalize
)

func (r runMode) String() string {
switch r {
case ignore:
return "ignore"
case attemptOnly:
return "attemptOnly"
case yield:
return "yield"
case finalize:
return "finalize"
}
return "unknown"
}

// condition is a bit mask of states that a task may depend on.
//
// There are generally two types of states: states that are met if all tasks
Expand Down
28 changes: 28 additions & 0 deletions internal/core/adt/vertexstatus_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 5 additions & 14 deletions internal/httplog/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,17 @@ type Logger interface {

type EventKind int

//go:generate go run golang.org/x/tools/cmd/stringer -type=EventKind -linecomment

const (
NoEvent EventKind = iota
KindClientSendRequest
KindClientRecvResponse
NoEvent EventKind = iota
KindClientSendRequest // http client->
KindClientRecvResponse // http client<-

// TODO KindServerRecvRequest
// TODO KindServerSendResponse
)

func (k EventKind) String() string {
switch k {
case KindClientSendRequest:
return "http client->"
case KindClientRecvResponse:
return "http client<-"
default:
return "unknown"
}
}

// Request represents an HTTP request.
type Request struct {
ID int64 `json:"id"`
Expand Down
Loading

0 comments on commit a20e523

Please sign in to comment.