gitee.com/zhongguo168a/gocodes@v0.0.0-20230609140523-e1828349603f/datax/schemax/decl-class.go (about)

     1  package schemax
     2  
     3  type ClassDecl struct {
     4  	//
     5  	Package string
     6  	//
     7  	Name string
     8  	//
     9  	Parent string
    10  	// 如果Kind==Struct
    11  	Fields []*Field
    12  	//
    13  	Comments []string
    14  	//
    15  	Tags map[string]string
    16  }
    17  
    18  func (decl *ClassDecl) GetName() string {
    19  	return decl.Name
    20  }
    21  
    22  func (decl *ClassDecl) GetPackage() string {
    23  	return decl.Package
    24  }
    25  
    26  func (decl *ClassDecl) GetIdent() string {
    27  	return decl.Package + "." + decl.Name
    28  }
    29  
    30  func (decl *ClassDecl) RefType() string {
    31  	return "classdecl"
    32  }
    33  
    34  func (decl *ClassDecl) GetField(name string) (f *Field) {
    35  	for _, val := range decl.Fields {
    36  		if val.Name == name {
    37  			return val
    38  		}
    39  	}
    40  
    41  	p := decl.GetParent()
    42  	if p != nil {
    43  		return p.GetField(name)
    44  	}
    45  
    46  	return nil
    47  }
    48  
    49  func (decl *ClassDecl) GetParent() *ClassDecl {
    50  	if decl.Parent == "" {
    51  		return nil
    52  	}
    53  
    54  	ip := GetDeclByKey(decl.Parent)
    55  	p, ok := ip.(*ClassDecl)
    56  	if ok {
    57  		return p
    58  	}
    59  	return nil
    60  }
    61  
    62  // GetAllField 获取类的属性字段,包括父类
    63  func (decl *ClassDecl) GetAllField() (r []*Field) {
    64  	cur := decl
    65  	for {
    66  		for _, val := range cur.Fields {
    67  			r = append(r, val)
    68  		}
    69  
    70  		if cur.Parent == "" {
    71  			return
    72  		}
    73  
    74  		ip := GetDeclByKey(cur.Parent)
    75  		p, ok := ip.(*ClassDecl)
    76  		if ok == false {
    77  			return
    78  		}
    79  		cur = p
    80  	}
    81  
    82  }