github.com/mailru/activerecord@v1.12.2/internal/pkg/parser/fieldobject.go (about)

     1  package parser
     2  
     3  import (
     4  	"fmt"
     5  	"go/ast"
     6  
     7  	"github.com/mailru/activerecord/internal/pkg/arerror"
     8  	"github.com/mailru/activerecord/internal/pkg/ds"
     9  )
    10  
    11  // Процесс парсинга декларативного описания связи между сущностями
    12  func ParseFieldsObject(dst *ds.RecordPackage, fieldsobject []*ast.Field) error {
    13  	for _, fieldobject := range fieldsobject {
    14  		if fieldobject.Names == nil || len(fieldobject.Names) != 1 {
    15  			return &arerror.ErrParseTypeFieldStructDecl{Err: arerror.ErrNameDeclaration}
    16  		}
    17  
    18  		newfieldobj := ds.FieldObject{
    19  			Name: fieldobject.Names[0].Name,
    20  		}
    21  
    22  		switch t := fieldobject.Type.(type) {
    23  		case *ast.Ident:
    24  			if err := checkBoolType(fieldobject.Type); err != nil {
    25  				return &arerror.ErrParseTypeFieldStructDecl{Name: newfieldobj.Name, FieldType: t.Name, Err: arerror.ErrTypeNotBool}
    26  			}
    27  
    28  			newfieldobj.Unique = true
    29  		case *ast.ArrayType:
    30  			if t.Len != nil {
    31  				return &arerror.ErrParseTypeFieldStructDecl{Name: newfieldobj.Name, FieldType: t.Elt.(*ast.Ident).Name, Err: arerror.ErrTypeNotSlice}
    32  			}
    33  
    34  			if t.Elt.(*ast.Ident).Name != string(TypeBool) {
    35  				return &arerror.ErrParseTypeFieldStructDecl{Name: newfieldobj.Name, FieldType: t.Elt.(*ast.Ident).Name, Err: arerror.ErrTypeNotBool}
    36  			}
    37  
    38  			newfieldobj.Unique = false
    39  		default:
    40  			return &arerror.ErrParseTypeFieldStructDecl{Name: newfieldobj.Name, FieldType: fmt.Sprintf("%T", t), Err: arerror.ErrUnknown}
    41  		}
    42  
    43  		tagParam, err := splitTag(fieldobject, CheckFlagEmpty, map[TagNameType]ParamValueRule{})
    44  		if err != nil {
    45  			return &arerror.ErrParseTypeFieldStructDecl{Name: newfieldobj.Name, Err: err}
    46  		}
    47  
    48  		for _, kv := range tagParam {
    49  			switch kv[0] {
    50  			case "field":
    51  				fldNum, ok := dst.FieldsMap[kv[1]]
    52  				if !ok {
    53  					return &arerror.ErrParseTypeFieldObjectTagDecl{Name: newfieldobj.Name, TagName: kv[0], TagValue: kv[1], Err: arerror.ErrFieldNotExist}
    54  				}
    55  
    56  				dst.Fields[fldNum].ObjectLink = newfieldobj.Name
    57  				newfieldobj.Field = kv[1]
    58  			case "key":
    59  				newfieldobj.Key = kv[1]
    60  			case "object":
    61  				newfieldobj.ObjectName = kv[1]
    62  			default:
    63  				return &arerror.ErrParseTypeFieldObjectTagDecl{Name: newfieldobj.Name, TagName: kv[0], TagValue: kv[1], Err: arerror.ErrParseTagUnknown}
    64  			}
    65  		}
    66  
    67  		if err = dst.AddFieldObject(newfieldobj); err != nil {
    68  			return err
    69  		}
    70  	}
    71  
    72  	return nil
    73  }