fix: embedded should be nil if not exists (#6219)
This commit is contained in:
		
							parent
							
								
									b444011d09
								
							
						
					
					
						commit
						f0360dccbf
					
				| @ -580,8 +580,6 @@ func (field *Field) setupValuerAndSetter() { | |||||||
| 			case **bool: | 			case **bool: | ||||||
| 				if data != nil && *data != nil { | 				if data != nil && *data != nil { | ||||||
| 					field.ReflectValueOf(ctx, value).SetBool(**data) | 					field.ReflectValueOf(ctx, value).SetBool(**data) | ||||||
| 				} else { |  | ||||||
| 					field.ReflectValueOf(ctx, value).SetBool(false) |  | ||||||
| 				} | 				} | ||||||
| 			case bool: | 			case bool: | ||||||
| 				field.ReflectValueOf(ctx, value).SetBool(data) | 				field.ReflectValueOf(ctx, value).SetBool(data) | ||||||
| @ -601,8 +599,6 @@ func (field *Field) setupValuerAndSetter() { | |||||||
| 			case **int64: | 			case **int64: | ||||||
| 				if data != nil && *data != nil { | 				if data != nil && *data != nil { | ||||||
| 					field.ReflectValueOf(ctx, value).SetInt(**data) | 					field.ReflectValueOf(ctx, value).SetInt(**data) | ||||||
| 				} else { |  | ||||||
| 					field.ReflectValueOf(ctx, value).SetInt(0) |  | ||||||
| 				} | 				} | ||||||
| 			case int64: | 			case int64: | ||||||
| 				field.ReflectValueOf(ctx, value).SetInt(data) | 				field.ReflectValueOf(ctx, value).SetInt(data) | ||||||
| @ -667,8 +663,6 @@ func (field *Field) setupValuerAndSetter() { | |||||||
| 			case **uint64: | 			case **uint64: | ||||||
| 				if data != nil && *data != nil { | 				if data != nil && *data != nil { | ||||||
| 					field.ReflectValueOf(ctx, value).SetUint(**data) | 					field.ReflectValueOf(ctx, value).SetUint(**data) | ||||||
| 				} else { |  | ||||||
| 					field.ReflectValueOf(ctx, value).SetUint(0) |  | ||||||
| 				} | 				} | ||||||
| 			case uint64: | 			case uint64: | ||||||
| 				field.ReflectValueOf(ctx, value).SetUint(data) | 				field.ReflectValueOf(ctx, value).SetUint(data) | ||||||
| @ -721,8 +715,6 @@ func (field *Field) setupValuerAndSetter() { | |||||||
| 			case **float64: | 			case **float64: | ||||||
| 				if data != nil && *data != nil { | 				if data != nil && *data != nil { | ||||||
| 					field.ReflectValueOf(ctx, value).SetFloat(**data) | 					field.ReflectValueOf(ctx, value).SetFloat(**data) | ||||||
| 				} else { |  | ||||||
| 					field.ReflectValueOf(ctx, value).SetFloat(0) |  | ||||||
| 				} | 				} | ||||||
| 			case float64: | 			case float64: | ||||||
| 				field.ReflectValueOf(ctx, value).SetFloat(data) | 				field.ReflectValueOf(ctx, value).SetFloat(data) | ||||||
| @ -767,8 +759,6 @@ func (field *Field) setupValuerAndSetter() { | |||||||
| 			case **string: | 			case **string: | ||||||
| 				if data != nil && *data != nil { | 				if data != nil && *data != nil { | ||||||
| 					field.ReflectValueOf(ctx, value).SetString(**data) | 					field.ReflectValueOf(ctx, value).SetString(**data) | ||||||
| 				} else { |  | ||||||
| 					field.ReflectValueOf(ctx, value).SetString("") |  | ||||||
| 				} | 				} | ||||||
| 			case string: | 			case string: | ||||||
| 				field.ReflectValueOf(ctx, value).SetString(data) | 				field.ReflectValueOf(ctx, value).SetString(data) | ||||||
|  | |||||||
| @ -103,9 +103,16 @@ func TestEmbeddedPointerTypeStruct(t *testing.T) { | |||||||
| 		URL   string | 		URL   string | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	type Author struct { | ||||||
|  | 		ID    string | ||||||
|  | 		Name  string | ||||||
|  | 		Email string | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	type HNPost struct { | 	type HNPost struct { | ||||||
| 		*BasePost | 		*BasePost | ||||||
| 		Upvotes int32 | 		Upvotes int32 | ||||||
|  | 		*Author `gorm:"EmbeddedPrefix:user_"` // Embedded struct
 | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	DB.Migrator().DropTable(&HNPost{}) | 	DB.Migrator().DropTable(&HNPost{}) | ||||||
| @ -123,6 +130,10 @@ func TestEmbeddedPointerTypeStruct(t *testing.T) { | |||||||
| 	if hnPost.Title != "embedded_pointer_type" { | 	if hnPost.Title != "embedded_pointer_type" { | ||||||
| 		t.Errorf("Should find correct value for embedded pointer type") | 		t.Errorf("Should find correct value for embedded pointer type") | ||||||
| 	} | 	} | ||||||
|  | 
 | ||||||
|  | 	if hnPost.Author != nil { | ||||||
|  | 		t.Errorf("Expected to get back a nil Author but got: %v", hnPost.Author) | ||||||
|  | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| type Content struct { | type Content struct { | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Cr
						Cr