let gorm can scan protobuf struct without add an ignore tag ("-") manually
link: https://github.com/go-gorm/gorm/issues/4877
This commit is contained in:
		
							parent
							
								
									b8f33a42a4
								
							
						
					
					
						commit
						5d15e1a500
					
				@ -155,7 +155,7 @@ func ParseWithSpecialTableName(dest interface{}, cacheStore *sync.Map, namer Nam
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for i := 0; i < modelType.NumField(); i++ {
 | 
			
		||||
		if fieldStruct := modelType.Field(i); ast.IsExported(fieldStruct.Name) {
 | 
			
		||||
		if fieldStruct := modelType.Field(i); ast.IsExported(fieldStruct.Name) && fieldStruct.Tag.Get("json") != "-" {
 | 
			
		||||
			if field := schema.ParseField(fieldStruct); field.EmbeddedSchema != nil {
 | 
			
		||||
				schema.Fields = append(schema.Fields, field.EmbeddedSchema.Fields...)
 | 
			
		||||
			} else {
 | 
			
		||||
 | 
			
		||||
@ -297,3 +297,21 @@ func TestEmbeddedStructForCustomizedNamingStrategy(t *testing.T) {
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestParseProtobufStruct(t *testing.T) {
 | 
			
		||||
	type Response struct {
 | 
			
		||||
		Code                 *uint32                `protobuf:"varint,1,opt,name=code" json:"code,omitempty"`
 | 
			
		||||
		XXX_NoUnkeyedLiteral struct{}               `json:"-"`
 | 
			
		||||
		XXX_unrecognized     []byte                 `json:"-"`
 | 
			
		||||
		XXX_sizecache        int32                  `json:"-"`
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	responseSchema, err := schema.Parse(&Response{}, &sync.Map{}, CustomizedNamingStrategy{schema.NamingStrategy{}})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Fatalf("failed to parse protobuf struct, got error %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if len(responseSchema.Fields) != 1 || responseSchema.Fields[0].Name != "Code" {
 | 
			
		||||
		t.Errorf("schema %v do include private field in protobuf struct", responseSchema)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user