github.com/oam-dev/kubevela@v1.9.11/references/cuegen/decl.go (about)

     1  /*
     2  Copyright 2023 The KubeVela Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package cuegen
    18  
    19  import (
    20  	goast "go/ast"
    21  
    22  	cueast "cuelang.org/go/cue/ast"
    23  	cuetoken "cuelang.org/go/cue/token"
    24  )
    25  
    26  // Decl is an interface that can build a cueast.Decl.
    27  type Decl interface {
    28  	Build() cueast.Decl
    29  }
    30  
    31  // CommonFields is a struct that contains common fields for all decls.
    32  type CommonFields struct {
    33  	Expr    cueast.Expr
    34  	Name    string
    35  	Comment *goast.CommentGroup
    36  	Doc     *goast.CommentGroup
    37  	Pos     cuetoken.Pos
    38  }
    39  
    40  // Struct is a struct that represents a CUE struct.
    41  type Struct struct {
    42  	CommonFields
    43  }
    44  
    45  // Build creates a cueast.Decl from Struct.
    46  func (s *Struct) Build() cueast.Decl {
    47  	d := &cueast.Field{
    48  		Label: cueast.NewIdent(s.Name),
    49  		Value: s.Expr,
    50  	}
    51  
    52  	makeComments(d, &commentUnion{comment: s.Comment, doc: s.Doc})
    53  	cueast.SetPos(d, s.Pos)
    54  
    55  	return d
    56  }