visitSetFunc()

in lib/generator.js [1748:1823]


  visitSetFunc(ast, structName, level = 0){
    const fieldName = _format(_name(ast.fieldName));
    const fileldtype = structName + _format(fieldName);
    const itemName = structName + _format(fieldName);
    if (ast.fieldValue.fieldType === 'array') {
      if (_name(ast.fieldValue.fieldItemType)) {
        this.emit(`func (s *${structName}) Set${fieldName}(v []${this.getType(_name(ast.fieldValue.fieldItemType))}) *${structName} {\n`, level);
        this.emit(`s.${fieldName} = v\n`, level + 1);
        this.emit(`return s\n`, level + 1);
        this.emit(`}\n`, level);
        this.emit(`\n`, level);
      } else if (ast.fieldValue.fieldItemType.type === 'map') {
        this.emit(`func (s *${structName}) Set${fieldName}(v []`, level);
        this.visitType(ast.fieldValue.fieldItemType);
        this.emit(`) *${structName} {\n`);
        this.emit(`s.${fieldName} = v\n`, level + 1);
        this.emit(`return s\n`, level + 1);
        this.emit(`}\n`, level);
        this.emit(`\n`, level);
      } else if (ast.fieldValue.fieldItemType.type === 'modelBody') {
        this.emit(`func (s *${structName}) Set${fieldName}(v []${this.getType(fileldtype)}) *${structName} {\n`, level);
        this.emit(`s.${fieldName} = v\n`, level + 1);
        this.emit(`return s\n`, level + 1);
        this.emit(`}\n`, level);
        this.emit(`\n`, level);
      } else if (ast.fieldValue.fieldItemType.fieldType === 'array') {
        this.emit(`func (s *${structName}) Set${fieldName}(v [][]`, level);
        this.emitFuncArray(ast.fieldValue.fieldItemType, itemName);
        this.emit(`) *${structName} {\n`);
        this.emit(`s.${fieldName} = v\n`, level + 1);
        this.emit(`return s\n`, level + 1);
        this.emit(`}\n`, level);
        this.emit(`\n`, level);
      }
    } else if (ast.fieldValue.type === 'modelBody') {
      this.emit(`func (s *${structName}) Set${fieldName}(v *${fileldtype}) *${structName} {\n`, level);
      this.emit(`s.${fieldName} = v\n`, level + 1);
      this.emit(`return s\n`, level + 1);
      this.emit(`}\n`, level);
      this.emit(`\n`, level);
    } else if (_name(ast.fieldValue.fieldType) && ast.fieldValue.fieldType.idType === 'module') {
      const fieldType = ast.fieldValue.fieldType;
      this.emit(`func (s *${structName}) Set${fieldName}(v ${this.clientName[_name(fieldType)]}) *${structName} {\n`, level);
      this.emit(`s.${fieldName} = v\n`, level + 1);
      this.emit(`return s\n`, level + 1);
      this.emit(`}\n`, level);
      this.emit(`\n`, level);
    } else if (ast.fieldValue.fieldType.type === 'moduleModel' || ast.fieldValue.fieldType.type === 'moduleTypedef' || ast.fieldValue.fieldType.type === 'subModel') {
      this.emit(`func (s *${structName}) Set${fieldName}(v `, level);
      this.visitType(ast.fieldValue.fieldType, level);
      this.emit(`) *${structName} {\n`, level);
      this.emit(`s.${fieldName} = v\n`, level + 1);
      this.emit(`return s\n`, level + 1);
      this.emit(`}\n`, level);
      this.emit(`\n`, level);
    } else {
      var fieldType = '';
      if (!_name(ast.fieldValue.fieldType)) {
        fieldType = ast.fieldValue.fieldType;
      } else {
        fieldType = _name(ast.fieldValue.fieldType);
      }

      this.emit(`func (s *${structName}) Set${fieldName}(v `, level);
      this.visitType(ast.fieldValue, level, {});
      this.emit(`) *${structName} {\n`, level);
      if (!DSL.util.isBasicType(fieldType) || _isFilterType(fieldType)) {
        this.emit(`s.${fieldName} = v\n`, level + 1);
      } else {
        this.emit(`s.${fieldName} = &v\n`, level + 1);
      }
      this.emit(`return s\n`, level + 1);
      this.emit(`}\n`, level);
      this.emit(`\n`, level);
    }
  }