Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api request json body data type support interface{} #2888

Open
yasin-wu opened this issue Feb 16, 2023 · 10 comments
Open

api request json body data type support interface{} #2888

yasin-wu opened this issue Feb 16, 2023 · 10 comments

Comments

@yasin-wu
Copy link

core/mapping/unmarshaler.go

func (u *Unmarshaler) processNamedFieldWithValue(fieldType reflect.Type, value reflect.Value,
	vp valueWithParent, key string, opts *fieldOptionsWithContext, fullName string) error {
	mapValue := vp.value
	if mapValue == nil {
		if opts.optional() {
			return nil
		}

		return fmt.Errorf("field %s mustn't be nil", key)
	}

	if !value.CanSet() {
		return fmt.Errorf("field %s is not settable", key)
	}

	maybeNewValue(fieldType, value)

	if yes, err := u.processFieldTextUnmarshaler(fieldType, value, mapValue); yes {
		return err
	}

	fieldKind := Deref(fieldType).Kind()
	switch fieldKind {
-	case reflect.Array, reflect.Map, reflect.Slice, reflect.Struct:
+       case reflect.Array, reflect.Map, reflect.Slice, reflect.Struct, reflect.Interface:
		return u.processFieldNotFromString(fieldType, value, vp, opts, fullName)
	default:
		if u.opts.fromString || opts.fromString() {
			return u.processNamedFieldWithValueFromString(fieldType, value, mapValue,
				key, opts, fullName)
		}

		return u.processFieldNotFromString(fieldType, value, vp, opts, fullName)
	}
}
func (u *Unmarshaler) processFieldPrimitive(field reflect.StructField, value reflect.Value,
	mapValue interface{}, opts *fieldOptionsWithContext, fullName string) error {
	fieldType := field.Type
	typeKind := Deref(fieldType).Kind()
	valueKind := reflect.TypeOf(mapValue).Kind()

	switch {
	case typeKind == reflect.Slice && valueKind == reflect.Slice:
		return u.fillSlice(fieldType, value, mapValue)
	case typeKind == reflect.Map && valueKind == reflect.Map:
		return u.fillMap(field, value, mapValue)
+	case typeKind == reflect.Interface:
+ 		value.Set(reflect.ValueOf(mapValue))
+ 		return nil	
	default:
		switch v := mapValue.(type) {
		case json.Number:
			return u.processFieldPrimitiveWithJSONNumber(field, value, v, opts, fullName)
		default:
			if typeKind == valueKind {
				if err := validateValueInOptions(mapValue, opts.options()); err != nil {
					return err
				}

				return fillWithSameType(field, value, mapValue, opts)
			}
		}
	}
@yasin-wu
Copy link
Author

.api文件中可以使用interface数据类型,实际在请求过程中会报错:type mismatch

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


The interface data type can be used in the .api file, but an error will be reported during the request process: type mismatch

@kagxin
Copy link

kagxin commented Feb 24, 2023

@yasin-wu 我们是通过直接修改生成的 hander 部分的代码,把 httpx.Parse 替换掉,直接用 json.Unmarshal 反序列化到 req 结构体变量中使用的,可以正常使用 interface{}

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


@yasin-wu We directly modify the generated hander part of the code, replace httpx.Parse, and directly use json.Unmarshal to deserialize it into the req structure variable, which can be used normally Use interface{}

@henryjhenry
Copy link

我的做法是让接口调用方把数据json序列化一遍,传字符串,收到字符串再反序列化成自己想要的结构体

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


My approach is to let the interface caller serialize the data json again, pass the string, receive the string and then deserialize it into the desired structure

@J-Orange-Sun
Copy link

遇到了同样的问题,业务里面有需要 interface{} 作为动态的参数结构体

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


Encountered the same problem. There is a need for interface{} as a dynamic parameter structure in the business.

@Kseng
Copy link

Kseng commented Oct 22, 2024

请问这个部分有其他解法吗?不自己修改handle的情况下 能不能就用go-zero的httpx parse解析

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


Is there any other solution to this part? Without modifying the handle myself, can I use go-zero's httpx parse to parse it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants