github.com/easysoft/zendata@v0.0.0-20240513203326-705bd5a7fd67/internal/pkg/service/def.go (about)

     1  package service
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/easysoft/zendata/internal/pkg/domain"
     7  	"github.com/easysoft/zendata/internal/pkg/helper"
     8  	i118Utils "github.com/easysoft/zendata/pkg/utils/i118"
     9  	logUtils "github.com/easysoft/zendata/pkg/utils/log"
    10  	stringUtils "github.com/easysoft/zendata/pkg/utils/string"
    11  	"github.com/easysoft/zendata/pkg/utils/vari"
    12  	"github.com/fatih/color"
    13  	"gopkg.in/yaml.v2"
    14  )
    15  
    16  type DefService struct {
    17  	ResService      *ResService      `inject:""`
    18  	FieldService    *FieldService    `inject:""`
    19  	CombineService  *CombineService  `inject:""`
    20  	OutputService   *OutputService   `inject:""`
    21  	ProtobufService *ProtobufService `inject:""`
    22  	FileService     *FileService     `inject:""`
    23  }
    24  
    25  func (s *DefService) LoadDataContentDef(filesContents [][]byte, fieldsToExport *[]string) (ret domain.DefData) {
    26  	ret = domain.DefData{}
    27  	for _, f := range filesContents {
    28  		right := s.LoadContentDef(f)
    29  		ret = s.MergeDef(ret, right, fieldsToExport)
    30  	}
    31  
    32  	return
    33  }
    34  
    35  func (s *DefService) LoadContentDef(content []byte) (ret domain.DefData) {
    36  	content = helper.ReplaceSpecialChars(content)
    37  	err := yaml.Unmarshal(content, &ret)
    38  	if err != nil {
    39  		logUtils.PrintToWithColor(i118Utils.I118Prt.Sprintf("fail_to_parse_file", ""), color.FgCyan)
    40  		return
    41  	}
    42  
    43  	return
    44  }
    45  
    46  func (s *DefService) MergeDef(defaultDef domain.DefData, configDef domain.DefData, fieldsToExport *[]string) domain.DefData {
    47  	if configDef.Type == "article" && configDef.Content != "" {
    48  		s.convertArticleContent(&configDef)
    49  	}
    50  
    51  	s.mergerDefine(&defaultDef, &configDef, fieldsToExport)
    52  	s.orderFields(&defaultDef, *fieldsToExport)
    53  
    54  	for index := range defaultDef.Fields {
    55  		if vari.GlobalVars.Trim {
    56  			defaultDef.Fields[index].Prefix = ""
    57  			defaultDef.Fields[index].Postfix = ""
    58  		}
    59  	}
    60  
    61  	return defaultDef
    62  }
    63  
    64  func (s *DefService) convertArticleContent(config *domain.DefData) {
    65  	field := domain.DefField{}
    66  	field.Type = config.Type
    67  	field.From = config.From
    68  	field.Range = "`" + config.Content + "`"
    69  
    70  	config.Fields = append(config.Fields, field)
    71  }
    72  
    73  func (s *DefService) mergerDefine(defaultDef, configDef *domain.DefData, fieldsToExport *[]string) {
    74  	isSetFieldsToExport := false
    75  	if len(*fieldsToExport) > 0 {
    76  		isSetFieldsToExport = true
    77  	}
    78  
    79  	defaultFieldMap := map[string]*domain.DefField{}
    80  	configFieldMap := map[string]*domain.DefField{}
    81  	sortedKeys := make([]string, 0)
    82  
    83  	//if configDef.Type != "" {
    84  	//	vari.GlobalVars.DefDataType = configDef.Type
    85  	//} else if defaultDef.Type != "" {
    86  	//	vari.GlobalVars.DefDataType = defaultDef.Type
    87  	//} else {
    88  	//	vari.GlobalVars.DefDataType = consts.DefTypeText
    89  	//}
    90  
    91  	if configDef.Content != "" && defaultDef.Content == "" {
    92  		defaultDef.Content = configDef.Content
    93  	}
    94  	if configDef.From != "" && defaultDef.From == "" {
    95  		defaultDef.From = configDef.From
    96  	}
    97  	if configDef.Type != "" && defaultDef.Type == "" {
    98  		defaultDef.Type = configDef.Type
    99  	}
   100  
   101  	for i, field := range defaultDef.Fields {
   102  		if !isSetFieldsToExport {
   103  			*fieldsToExport = append(*fieldsToExport, field.Field)
   104  		}
   105  
   106  		defaultDef.Fields[i].FileDir = vari.GlobalVars.ConfigFileDir
   107  		CreatePathToFieldMap(&defaultDef.Fields[i], defaultFieldMap, nil)
   108  	}
   109  	for i, field := range configDef.Fields {
   110  		vari.GlobalVars.TopFieldMap[field.Field] = field
   111  		if !isSetFieldsToExport {
   112  			if !stringUtils.StrInArr(field.Field, *fieldsToExport) {
   113  				*fieldsToExport = append(*fieldsToExport, field.Field)
   114  			}
   115  		}
   116  
   117  		configDef.Fields[i].FileDir = vari.GlobalVars.ConfigFileDir
   118  		CreatePathToFieldMap(&configDef.Fields[i], configFieldMap, &sortedKeys)
   119  	}
   120  
   121  	// overwrite
   122  	for path, field := range configFieldMap {
   123  		parent, exist := defaultFieldMap[path]
   124  		if exist {
   125  			CopyField(*field, parent)
   126  			defaultFieldMap[path] = parent
   127  		}
   128  	}
   129  
   130  	// append
   131  	for _, key := range sortedKeys {
   132  		field := configFieldMap[key]
   133  		if field == nil || strings.Index(field.Path, "~~") > -1 {
   134  			continue
   135  		} // ignore no-top fields
   136  
   137  		_, exist := defaultFieldMap[field.Path]
   138  		if !exist {
   139  			defaultDef.Fields = append(defaultDef.Fields, *field)
   140  		}
   141  	}
   142  
   143  	for _, field := range defaultDef.Fields {
   144  		vari.GlobalVars.TopFieldMap[field.Field] = field
   145  	}
   146  }
   147  
   148  func (s *DefService) orderFields(defaultDef *domain.DefData, fieldsToExport []string) {
   149  	mp := map[string]domain.DefField{}
   150  	for _, field := range defaultDef.Fields {
   151  		mp[field.Field] = field
   152  	}
   153  
   154  	fields := make([]domain.DefField, 0)
   155  	for _, fieldName := range fieldsToExport {
   156  		fields = append(fields, mp[fieldName])
   157  	}
   158  
   159  	defaultDef.Fields = fields
   160  }
   161  
   162  func CreatePathToFieldMap(field *domain.DefField, mp map[string]*domain.DefField, keys *[]string) {
   163  	if field.Path == "" { // root
   164  		field.Path = field.Field
   165  	}
   166  
   167  	path := field.Path
   168  	//logUtils.Screen(path + " -> " + field.ZdField)
   169  	mp[path] = field
   170  
   171  	if keys != nil {
   172  		*keys = append(*keys, path)
   173  	}
   174  
   175  	if len(field.Fields) > 0 {
   176  		for i := range field.Fields {
   177  			field.Fields[i].Path = field.Path + "~~" + field.Fields[i].Field
   178  
   179  			CreatePathToFieldMap(&field.Fields[i], mp, keys)
   180  		}
   181  	}
   182  }
   183  
   184  func CopyField(child domain.DefField, parent *domain.DefField) {
   185  	if child.Note != "" {
   186  		(*parent).Note = child.Note
   187  	}
   188  	if child.Range != "" {
   189  		(*parent).Range = child.Range
   190  	}
   191  
   192  	(*parent).Prefix = child.Prefix
   193  	(*parent).Postfix = child.Postfix
   194  	(*parent).Divider = child.Divider
   195  
   196  	if child.Loop != "" {
   197  		(*parent).Loop = child.Loop
   198  	}
   199  	(*parent).Loopfix = child.Loopfix
   200  	(*parent).Format = child.Format
   201  
   202  	if child.From != "" {
   203  		(*parent).From = child.From
   204  	}
   205  	if child.Select != "" {
   206  		(*parent).Select = child.Select
   207  	}
   208  	if child.Where != "" {
   209  		(*parent).Where = child.Where
   210  	}
   211  	if child.Use != "" {
   212  		(*parent).Use = child.Use
   213  	}
   214  	if child.From != "" {
   215  		(*parent).From = child.From
   216  	}
   217  
   218  	if child.Type != "" {
   219  		(*parent).Type = child.Type
   220  	}
   221  
   222  	if child.Precision != 0 {
   223  		(*parent).Precision = child.Precision
   224  	}
   225  	if child.Length != 0 {
   226  		(*parent).Length = child.Length
   227  	}
   228  }