Skip to content

Commit 79044fb

Browse files
authored
Minor refactor the column type interface (#110)
1 parent 53b9eb6 commit 79044fb

File tree

4 files changed

+117
-84
lines changed

4 files changed

+117
-84
lines changed

parser/ast.go

+73-44
Original file line numberDiff line numberDiff line change
@@ -2971,7 +2971,7 @@ type QueryParam struct {
29712971
LBracePos Pos
29722972
RBracePos Pos
29732973
Name *Ident
2974-
Type Expr
2974+
Type ColumnType
29752975
}
29762976

29772977
func (q *QueryParam) Pos() Pos {
@@ -3182,7 +3182,7 @@ type ColumnDef struct {
31823182
NamePos Pos
31833183
ColumnEnd Pos
31843184
Name *NestedIdentifier
3185-
Type Expr
3185+
Type ColumnType
31863186
NotNull *NotNullLiteral
31873187
Nullable *NullLiteral
31883188

@@ -3298,72 +3298,85 @@ func (c *ColumnDef) Accept(visitor ASTVisitor) error {
32983298
return visitor.VisitColumnDef(c)
32993299
}
33003300

3301-
type ScalarTypeExpr struct {
3301+
type ColumnType interface {
3302+
Expr
3303+
Type() string
3304+
}
3305+
3306+
type ScalarType struct {
33023307
Name *Ident
33033308
}
33043309

3305-
func (s *ScalarTypeExpr) Pos() Pos {
3310+
func (s *ScalarType) Pos() Pos {
33063311
return s.Name.NamePos
33073312
}
33083313

3309-
func (s *ScalarTypeExpr) End() Pos {
3314+
func (s *ScalarType) End() Pos {
33103315
return s.Name.NameEnd
33113316
}
33123317

3313-
func (s *ScalarTypeExpr) String() string {
3318+
func (s *ScalarType) String() string {
33143319
return s.Name.String()
33153320
}
33163321

3317-
func (s *ScalarTypeExpr) Accept(visitor ASTVisitor) error {
3322+
func (s *ScalarType) Accept(visitor ASTVisitor) error {
33183323
visitor.enter(s)
33193324
defer visitor.leave(s)
33203325
if err := s.Name.Accept(visitor); err != nil {
33213326
return err
33223327
}
3323-
return visitor.VisitScalarTypeExpr(s)
3328+
return visitor.VisitScalarType(s)
33243329
}
33253330

3326-
type PropertyTypeExpr struct {
3331+
func (s *ScalarType) Type() string {
3332+
return s.Name.Name
3333+
}
3334+
3335+
type PropertyType struct {
33273336
Name *Ident
33283337
}
33293338

3330-
func (c *PropertyTypeExpr) Pos() Pos {
3339+
func (c *PropertyType) Pos() Pos {
33313340
return c.Name.NamePos
33323341
}
33333342

3334-
func (c *PropertyTypeExpr) End() Pos {
3343+
func (c *PropertyType) End() Pos {
33353344
return c.Name.NameEnd
33363345
}
33373346

3338-
func (c *PropertyTypeExpr) String() string {
3347+
func (c *PropertyType) String() string {
33393348
return c.Name.String()
33403349
}
33413350

3342-
func (c *PropertyTypeExpr) Accept(visitor ASTVisitor) error {
3351+
func (c *PropertyType) Accept(visitor ASTVisitor) error {
33433352
visitor.enter(c)
33443353
defer visitor.leave(c)
33453354
if err := c.Name.Accept(visitor); err != nil {
33463355
return err
33473356
}
3348-
return visitor.VisitPropertyTypeExpr(c)
3357+
return visitor.VisitPropertyType(c)
3358+
}
3359+
3360+
func (c *PropertyType) Type() string {
3361+
return c.Name.Name
33493362
}
33503363

3351-
type TypeWithParamsExpr struct {
3364+
type TypeWithParams struct {
33523365
LeftParenPos Pos
33533366
RightParenPos Pos
33543367
Name *Ident
33553368
Params []Literal
33563369
}
33573370

3358-
func (s *TypeWithParamsExpr) Pos() Pos {
3371+
func (s *TypeWithParams) Pos() Pos {
33593372
return s.Name.NamePos
33603373
}
33613374

3362-
func (s *TypeWithParamsExpr) End() Pos {
3375+
func (s *TypeWithParams) End() Pos {
33633376
return s.RightParenPos
33643377
}
33653378

3366-
func (s *TypeWithParamsExpr) String() string {
3379+
func (s *TypeWithParams) String() string {
33673380
var builder strings.Builder
33683381
builder.WriteString(s.Name.String())
33693382
builder.WriteByte('(')
@@ -3377,7 +3390,7 @@ func (s *TypeWithParamsExpr) String() string {
33773390
return builder.String()
33783391
}
33793392

3380-
func (s *TypeWithParamsExpr) Accept(visitor ASTVisitor) error {
3393+
func (s *TypeWithParams) Accept(visitor ASTVisitor) error {
33813394
visitor.enter(s)
33823395
defer visitor.leave(s)
33833396
if err := s.Name.Accept(visitor); err != nil {
@@ -3388,25 +3401,29 @@ func (s *TypeWithParamsExpr) Accept(visitor ASTVisitor) error {
33883401
return err
33893402
}
33903403
}
3391-
return visitor.VisitTypeWithParamsExpr(s)
3404+
return visitor.VisitTypeWithParams(s)
33923405
}
33933406

3394-
type ComplexTypeExpr struct {
3407+
func (s *TypeWithParams) Type() string {
3408+
return s.Name.Name
3409+
}
3410+
3411+
type ComplexType struct {
33953412
LeftParenPos Pos
33963413
RightParenPos Pos
33973414
Name *Ident
3398-
Params []Expr
3415+
Params []ColumnType
33993416
}
34003417

3401-
func (c *ComplexTypeExpr) Pos() Pos {
3418+
func (c *ComplexType) Pos() Pos {
34023419
return c.Name.NamePos
34033420
}
34043421

3405-
func (c *ComplexTypeExpr) End() Pos {
3422+
func (c *ComplexType) End() Pos {
34063423
return c.RightParenPos
34073424
}
34083425

3409-
func (c *ComplexTypeExpr) String() string {
3426+
func (c *ComplexType) String() string {
34103427
var builder strings.Builder
34113428
builder.WriteString(c.Name.String())
34123429
builder.WriteByte('(')
@@ -3420,7 +3437,7 @@ func (c *ComplexTypeExpr) String() string {
34203437
return builder.String()
34213438
}
34223439

3423-
func (c *ComplexTypeExpr) Accept(visitor ASTVisitor) error {
3440+
func (c *ComplexType) Accept(visitor ASTVisitor) error {
34243441
visitor.enter(c)
34253442
defer visitor.leave(c)
34263443
if err := c.Name.Accept(visitor); err != nil {
@@ -3431,25 +3448,29 @@ func (c *ComplexTypeExpr) Accept(visitor ASTVisitor) error {
34313448
return err
34323449
}
34333450
}
3434-
return visitor.VisitComplexTypeExpr(c)
3451+
return visitor.VisitComplexType(c)
3452+
}
3453+
3454+
func (c *ComplexType) Type() string {
3455+
return c.Name.Name
34353456
}
34363457

3437-
type NestedTypeExpr struct {
3458+
type NestedType struct {
34383459
LeftParenPos Pos
34393460
RightParenPos Pos
34403461
Name *Ident
34413462
Columns []Expr
34423463
}
34433464

3444-
func (n *NestedTypeExpr) Pos() Pos {
3465+
func (n *NestedType) Pos() Pos {
34453466
return n.Name.NamePos
34463467
}
34473468

3448-
func (n *NestedTypeExpr) End() Pos {
3469+
func (n *NestedType) End() Pos {
34493470
return n.RightParenPos
34503471
}
34513472

3452-
func (n *NestedTypeExpr) String() string {
3473+
func (n *NestedType) String() string {
34533474
var builder strings.Builder
34543475
// on the same level as the column type
34553476
builder.WriteString(n.Name.String())
@@ -3465,7 +3486,7 @@ func (n *NestedTypeExpr) String() string {
34653486
return builder.String()
34663487
}
34673488

3468-
func (n *NestedTypeExpr) Accept(visitor ASTVisitor) error {
3489+
func (n *NestedType) Accept(visitor ASTVisitor) error {
34693490
visitor.enter(n)
34703491
defer visitor.leave(n)
34713492
if err := n.Name.Accept(visitor); err != nil {
@@ -3476,7 +3497,11 @@ func (n *NestedTypeExpr) Accept(visitor ASTVisitor) error {
34763497
return err
34773498
}
34783499
}
3479-
return visitor.VisitNestedTypeExpr(n)
3500+
return visitor.VisitNestedType(n)
3501+
}
3502+
3503+
func (n *NestedType) Type() string {
3504+
return n.Name.Name
34803505
}
34813506

34823507
type CompressionCodec struct {
@@ -3689,29 +3714,29 @@ func (e *EnumValue) Accept(visitor ASTVisitor) error {
36893714
if err := e.Value.Accept(visitor); err != nil {
36903715
return err
36913716
}
3692-
return visitor.VisitEnumValueExpr(e)
3717+
return visitor.VisitEnumValue(e)
36933718
}
36943719

3695-
type EnumValueList struct {
3720+
type EnumType struct {
36963721
Name *Ident
36973722
ListPos Pos
36983723
ListEnd Pos
3699-
Enums []EnumValue
3724+
Values []EnumValue
37003725
}
37013726

3702-
func (e *EnumValueList) Pos() Pos {
3727+
func (e *EnumType) Pos() Pos {
37033728
return e.ListPos
37043729
}
37053730

3706-
func (e *EnumValueList) End() Pos {
3731+
func (e *EnumType) End() Pos {
37073732
return e.ListEnd
37083733
}
37093734

3710-
func (e *EnumValueList) String() string {
3735+
func (e *EnumType) String() string {
37113736
var builder strings.Builder
37123737
builder.WriteString(e.Name.String())
37133738
builder.WriteByte('(')
3714-
for i, enum := range e.Enums {
3739+
for i, enum := range e.Values {
37153740
if i > 0 {
37163741
builder.WriteString(", ")
37173742
}
@@ -3721,18 +3746,22 @@ func (e *EnumValueList) String() string {
37213746
return builder.String()
37223747
}
37233748

3724-
func (e *EnumValueList) Accept(visitor ASTVisitor) error {
3749+
func (e *EnumType) Accept(visitor ASTVisitor) error {
37253750
visitor.enter(e)
37263751
defer visitor.leave(e)
37273752
if err := e.Name.Accept(visitor); err != nil {
37283753
return err
37293754
}
3730-
for i := range e.Enums {
3731-
if err := e.Enums[i].Accept(visitor); err != nil {
3755+
for i := range e.Values {
3756+
if err := e.Values[i].Accept(visitor); err != nil {
37323757
return err
37333758
}
37343759
}
3735-
return visitor.VisitEnumValueExprList(e)
3760+
return visitor.VisitEnumType(e)
3761+
}
3762+
3763+
func (e *EnumType) Type() string {
3764+
return e.Name.Name
37363765
}
37373766

37383767
type IntervalExpr struct {

parser/ast_visitor.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,17 @@ type ASTVisitor interface {
7373
VisitWindowFunctionExpr(expr *WindowFunctionExpr) error
7474
VisitColumnDef(expr *ColumnDef) error
7575
VisitColumnExpr(expr *ColumnExpr) error
76-
VisitScalarTypeExpr(expr *ScalarTypeExpr) error
77-
VisitPropertyTypeExpr(expr *PropertyTypeExpr) error
78-
VisitTypeWithParamsExpr(expr *TypeWithParamsExpr) error
79-
VisitComplexTypeExpr(expr *ComplexTypeExpr) error
80-
VisitNestedTypeExpr(expr *NestedTypeExpr) error
76+
VisitScalarType(expr *ScalarType) error
77+
VisitPropertyType(expr *PropertyType) error
78+
VisitTypeWithParams(expr *TypeWithParams) error
79+
VisitComplexType(expr *ComplexType) error
80+
VisitNestedType(expr *NestedType) error
8181
VisitCompressionCodec(expr *CompressionCodec) error
8282
VisitNumberLiteral(expr *NumberLiteral) error
8383
VisitStringLiteral(expr *StringLiteral) error
8484
VisitRatioExpr(expr *RatioExpr) error
85-
VisitEnumValueExpr(expr *EnumValue) error
86-
VisitEnumValueExprList(expr *EnumValueList) error
85+
VisitEnumValue(expr *EnumValue) error
86+
VisitEnumType(expr *EnumType) error
8787
VisitIntervalExpr(expr *IntervalExpr) error
8888
VisitEngineExpr(expr *EngineExpr) error
8989
VisitColumnTypeExpr(expr *ColumnTypeExpr) error
@@ -678,35 +678,35 @@ func (v *DefaultASTVisitor) VisitColumnExpr(expr *ColumnExpr) error {
678678
return nil
679679
}
680680

681-
func (v *DefaultASTVisitor) VisitScalarTypeExpr(expr *ScalarTypeExpr) error {
681+
func (v *DefaultASTVisitor) VisitScalarType(expr *ScalarType) error {
682682
if v.Visit != nil {
683683
return v.Visit(expr)
684684
}
685685
return nil
686686
}
687687

688-
func (v *DefaultASTVisitor) VisitPropertyTypeExpr(expr *PropertyTypeExpr) error {
688+
func (v *DefaultASTVisitor) VisitPropertyType(expr *PropertyType) error {
689689
if v.Visit != nil {
690690
return v.Visit(expr)
691691
}
692692
return nil
693693
}
694694

695-
func (v *DefaultASTVisitor) VisitTypeWithParamsExpr(expr *TypeWithParamsExpr) error {
695+
func (v *DefaultASTVisitor) VisitTypeWithParams(expr *TypeWithParams) error {
696696
if v.Visit != nil {
697697
return v.Visit(expr)
698698
}
699699
return nil
700700
}
701701

702-
func (v *DefaultASTVisitor) VisitComplexTypeExpr(expr *ComplexTypeExpr) error {
702+
func (v *DefaultASTVisitor) VisitComplexType(expr *ComplexType) error {
703703
if v.Visit != nil {
704704
return v.Visit(expr)
705705
}
706706
return nil
707707
}
708708

709-
func (v *DefaultASTVisitor) VisitNestedTypeExpr(expr *NestedTypeExpr) error {
709+
func (v *DefaultASTVisitor) VisitNestedType(expr *NestedType) error {
710710
if v.Visit != nil {
711711
return v.Visit(expr)
712712
}
@@ -741,14 +741,14 @@ func (v *DefaultASTVisitor) VisitRatioExpr(expr *RatioExpr) error {
741741
return nil
742742
}
743743

744-
func (v *DefaultASTVisitor) VisitEnumValueExpr(expr *EnumValue) error {
744+
func (v *DefaultASTVisitor) VisitEnumValue(expr *EnumValue) error {
745745
if v.Visit != nil {
746746
return v.Visit(expr)
747747
}
748748
return nil
749749
}
750750

751-
func (v *DefaultASTVisitor) VisitEnumValueExprList(expr *EnumValueList) error {
751+
func (v *DefaultASTVisitor) VisitEnumType(expr *EnumType) error {
752752
if v.Visit != nil {
753753
return v.Visit(expr)
754754
}

0 commit comments

Comments
 (0)