Skip to content

Commit

Permalink
decoder: Refactor newExpression & plumb PathContext (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko authored Jan 17, 2023
1 parent 6ac47d1 commit f9adfd8
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 26 deletions.
5 changes: 3 additions & 2 deletions decoder/expr_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
)

type List struct {
expr hcl.Expression
cons schema.List
expr hcl.Expression
cons schema.List
pathCtx *PathContext
}

func (l List) CompletionAtPos(ctx context.Context, pos hcl.Pos) []lang.Candidate {
Expand Down
5 changes: 3 additions & 2 deletions decoder/expr_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
)

type Map struct {
expr hcl.Expression
cons schema.Map
expr hcl.Expression
cons schema.Map
pathCtx *PathContext
}

func (m Map) CompletionAtPos(ctx context.Context, pos hcl.Pos) []lang.Candidate {
Expand Down
5 changes: 3 additions & 2 deletions decoder/expr_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
)

type Object struct {
expr hcl.Expression
cons schema.Object
expr hcl.Expression
cons schema.Object
pathCtx *PathContext
}

func (obj Object) CompletionAtPos(ctx context.Context, pos hcl.Pos) []lang.Candidate {
Expand Down
5 changes: 3 additions & 2 deletions decoder/expr_one_of.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
)

type OneOf struct {
expr hcl.Expression
cons schema.OneOf
expr hcl.Expression
cons schema.OneOf
pathCtx *PathContext
}

func (oo OneOf) CompletionAtPos(ctx context.Context, pos hcl.Pos) []lang.Candidate {
Expand Down
5 changes: 3 additions & 2 deletions decoder/expr_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
)

type Set struct {
expr hcl.Expression
cons schema.Set
expr hcl.Expression
cons schema.Set
pathCtx *PathContext
}

func (s Set) CompletionAtPos(ctx context.Context, pos hcl.Pos) []lang.Candidate {
Expand Down
5 changes: 3 additions & 2 deletions decoder/expr_tuple.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
)

type Tuple struct {
expr hcl.Expression
cons schema.Tuple
expr hcl.Expression
cons schema.Tuple
pathCtx *PathContext
}

func (t Tuple) CompletionAtPos(ctx context.Context, pos hcl.Pos) []lang.Candidate {
Expand Down
76 changes: 62 additions & 14 deletions decoder/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,79 @@ type Expression interface {
}

func (d *PathDecoder) newExpression(expr hcl.Expression, cons schema.Constraint) Expression {
return newExpression(d.pathCtx, expr, cons)
}

func newExpression(pathContext *PathContext, expr hcl.Expression, cons schema.Constraint) Expression {
switch c := cons.(type) {
case schema.AnyExpression:
return Any{expr: expr, cons: c, pathCtx: d.pathCtx}
return Any{
expr: expr,
cons: c,
pathCtx: pathContext,
}
case schema.LiteralType:
return LiteralType{expr: expr, cons: c}
case schema.Reference:
return Reference{expr: expr, cons: c}
return LiteralType{
expr: expr,
cons: c,
}
case schema.LiteralValue:
return LiteralValue{
expr: expr,
cons: c,
}
case schema.TypeDeclaration:
return TypeDeclaration{expr: expr, cons: c}
return TypeDeclaration{
expr: expr,
cons: c,
}
case schema.Keyword:
return Keyword{expr: expr, cons: c}
return Keyword{
expr: expr,
cons: c,
}
case schema.Reference:
return Reference{
expr: expr,
cons: c,
}
case schema.List:
return List{expr: expr, cons: c}
return List{
expr: expr,
cons: c,
pathCtx: pathContext,
}
case schema.Set:
return Set{expr: expr, cons: c}
return Set{
expr: expr,
cons: c,
pathCtx: pathContext,
}
case schema.Tuple:
return Tuple{expr: expr, cons: c}
return Tuple{
expr: expr,
cons: c,
pathCtx: pathContext,
}
case schema.Object:
return Object{expr: expr, cons: c}
return Object{
expr: expr,
cons: c,
pathCtx: pathContext,
}
case schema.Map:
return Map{expr: expr, cons: c}
return Map{
expr: expr,
cons: c,
pathCtx: pathContext,
}
case schema.OneOf:
return OneOf{expr: expr, cons: c}
case schema.LiteralValue:
return LiteralValue{expr: expr, cons: c}
return OneOf{
expr: expr,
cons: c,
pathCtx: pathContext,
}

}

return unknownExpression{}
Expand Down

0 comments on commit f9adfd8

Please sign in to comment.