Repository: cloudwego/kitex
File: pkg/generic/thrift/parse.go (Go)

Code before:
		Struct: &descriptor.StructDescriptor{
			FieldsByID:   map[int32]*descriptor.FieldDescriptor{},
			FieldsByName: map[string]*descriptor.FieldDescriptor{},
		},
	}
	var reqType *descriptor.TypeDescriptor
	reqType, err = parseType(field.Type, tree, structsCache, initRecursionDepth, opt)
	if err != nil {
		return err
	}
	hasRequestBase := false
	if reqType.Type == descriptor.STRUCT {
		for _, f := range reqType.Struct.FieldsByName {
			if f.Type.IsRequestBase {
				hasRequestBase = true
				break
			}
		}
	}
	reqField := &descriptor.FieldDescriptor{
		Name: field.Name,
		ID:   field.ID,
		Type: reqType,
	}
	if opt != nil {
		reqField.IsGoTagAliasDisabledFromOpt = opt.IsGoTagAliasDisabled
	}
	req.Struct.FieldsByID[field.ID] = reqField
	req.Struct.FieldsByName[field.Name] = reqField
	// parse response filed
	resp := &descriptor.TypeDescriptor{
		Type: descriptor.STRUCT,
		Struct: &descriptor.StructDescriptor{
			FieldsByID:   map[int32]*descriptor.FieldDescriptor{},
			FieldsByName: map[string]*descriptor.FieldDescriptor{},
		},
	}
	var respType *descriptor.TypeDescriptor
	respType, err = parseType(fn.FunctionType, tree, structsCache, initRecursionDepth, opt)
	if err != nil {
		return err
	}
	respField := &descriptor.FieldDescriptor{
		Type: respType,
	}
	if opt != nil {
		respField.IsGoTagAliasDisabledFromOpt = opt.IsGoTagAliasDisabled
	}
	// response has no name or id
	resp.Struct.FieldsByID[0] = respField
	resp.Struct.FieldsByName[""] = respField

Review comment (suggestion):
`opt != nil` is an implicit condition. If a non-null opt is passed in, it will cause alias to be unexpectedly disabled. At this place, you should use WithDisableXXXOption. When setting this, mark a separate field to judge whether the user has set it.

Code after:
			FieldsByName: map[string]*descriptor.FieldDescriptor{},
		},
	}

	var reqType *descriptor.TypeDescriptor
	reqType, err = parseType(field.Type, tree, structsCache, initRecursionDepth, opts)
	if err != nil {
		return err
	}
	hasRequestBase := false
	if reqType.Type == descriptor.STRUCT {
		for _, f := range reqType.Struct.FieldsByName {
			if f.Type.IsRequestBase {
				hasRequestBase = true
				break
			}
		}
	}
	reqField := &descriptor.FieldDescriptor{
		Name:     field.Name,
		ID:       field.ID,
		Type:     reqType,
		GoTagOpt: opts.goTag,
	}
	req.Struct.FieldsByID[field.ID] = reqField
	req.Struct.FieldsByName[field.Name] = reqField
	// parse response filed
	resp := &descriptor.TypeDescriptor{
		Type: descriptor.STRUCT,
		Struct: &descriptor.StructDescriptor{
			FieldsByID:   map[int32]*descriptor.FieldDescriptor{},
			FieldsByName: map[string]*descriptor.FieldDescriptor{},
		},
	}
	var respType *descriptor.TypeDescriptor
	respType, err = parseType(fn.FunctionType, tree, structsCache, initRecursionDepth, opts)
	if err != nil {
		return err
	}
	respField := &descriptor.FieldDescriptor{
		Type:     respType,
		GoTagOpt: opts.goTag,
	}
	// response has no name or id
	resp.Struct.FieldsByID[0] = respField
	resp.Struct.FieldsByName[""] = respField

	if len(fn.Throws) > 0 {
		// only support single exception
		field := fn.Throws[0]
		var exceptionType *descriptor.TypeDescriptor