示例#1
0
func Value2Struct(meta *tool.FieldMetaV1, structValue string, fd *pbmeta.FieldDescriptor, callback func(string, string) bool) (isValue2Struct bool, hasError bool) {

	if meta == nil {
		return
	}

	if meta.String2Struct == false {
		return
	}

	if !fd.IsMessageType() {
		hasError = true
		log.Errorf("%s is not message type", fd.Name())
		return
	}

	msgD := fd.MessageDesc()

	if msgD == nil {
		hasError = true
		log.Errorf("%s message not found", fd.Name())
		return
	}

	p := newLineParser(fd, structValue)

	// 匹配顺序从高到低

	defer func() {

		err := recover()

		switch err.(type) {
		// 运行时错误
		case interface {
			RuntimeError()
		}:
			// 继续外抛, 方便调试
			panic(err)
		case error:
			hasError = true
			log.Errorf("field: %s parse error, %v", fd.Name(), err)

		default:
			isValue2Struct = true
		}

	}()

	p.NextToken()

	for {

		if p.TokenID() != Token_Identifier {
			hasError = true
			log.Errorf("expect key in field: %s", fd.Name())
			return
		}

		key := p.TokenValue()

		structFD := FieldByNameWithMeta(msgD, key)

		// 尝试查找字段定义
		if structFD == nil {

			hasError = true
			log.Errorf("%s field not found ", key)
			return
		}

		p.NextToken()

		if p.TokenID() != Token_Comma {
			hasError = true
			log.Errorf("%s need ':' split value", key)
			return
		}

		p.NextToken()

		value := p.TokenValue()

		// 按照正常流程转换值
		if afterValue, ok := ValueConvetor(structFD, value); ok {

			if !callback(key, afterValue) {
				hasError = true
				return
			}

		} else {
			hasError = true
			log.Errorf("%s convert failed", key)
			return
		}

		p.NextToken()

	}

	return
}