MCPcopy Create free account
hub / github.com/apache/fory / analyzeField

Function analyzeField

go/fory/codegen/utils.go:469–528  ·  view source on GitHub ↗

analyzeField analyzes a struct field and creates FieldInfo

(field *types.Var, structTag string, index int)

Source from the content-addressed store, hash-verified

467
468// analyzeField analyzes a struct field and creates FieldInfo
469func analyzeField(field *types.Var, structTag string, index int) (*FieldInfo, error) {
470 fieldType := field.Type()
471 goName := field.Name()
472 snakeName := toSnakeCase(goName)
473
474 // Check if field type is supported
475 if !isSupportedFieldType(fieldType) {
476 return nil, nil // Skip unsupported types
477 }
478
479 optionalElem, isOptional := getOptionalElementType(fieldType)
480 if isOptional && optionalElem != nil {
481 if ptr, ok := optionalElem.(*types.Pointer); ok {
482 switch ptr.Elem().Underlying().(type) {
483 case *types.Slice, *types.Map:
484 return nil, fmt.Errorf("field %s: optional.Optional is not allowed for slice/map", goName)
485 }
486 } else {
487 switch optionalElem.Underlying().(type) {
488 case *types.Struct, *types.Slice, *types.Map:
489 return nil, fmt.Errorf("field %s: optional.Optional is not allowed for struct/slice/map", goName)
490 }
491 }
492 }
493
494 // Analyze type information
495 isPrimitive := isPrimitiveType(fieldType)
496 isPointer := false
497 typeID := getTypeID(fieldType)
498 primitiveSize := getPrimitiveSize(fieldType)
499 tagID, hasTagID, err := parseGeneratedFieldTagID(structTag)
500 if err != nil {
501 return nil, err
502 }
503
504 // Handle pointer types
505 if ptr, ok := fieldType.(*types.Pointer); ok {
506 isPointer = true
507 fieldType = ptr.Elem()
508 isPrimitive = isPrimitiveType(fieldType)
509 typeID = getTypeID(fieldType)
510 primitiveSize = getPrimitiveSize(fieldType)
511 }
512
513 return &FieldInfo{
514 GoName: goName,
515 SnakeName: snakeName,
516 Type: field.Type(),
517 Index: index,
518 IsPrimitive: isPrimitive,
519 IsPointer: isPointer,
520 IsOptional: isOptional,
521 Nullable: isPointer || isOptional, // Pointer and optional types are nullable in xlang mode
522 TypeID: typeID,
523 TagID: tagID,
524 HasTagID: hasTagID,
525 PrimitiveSize: primitiveSize,
526 OptionalElem: optionalElem,

Callers 1

extractStructInfoFunction · 0.85

Calls 8

toSnakeCaseFunction · 0.85
isSupportedFieldTypeFunction · 0.85
getOptionalElementTypeFunction · 0.85
getTypeIDFunction · 0.85
getPrimitiveSizeFunction · 0.85
parseGeneratedFieldTagIDFunction · 0.85
isPrimitiveTypeFunction · 0.70
TypeMethod · 0.45

Tested by

no test coverage detected