gocc icon indicating copy to clipboard operation
gocc copied to clipboard

Serializing an AST structure with gob and then deserializing fails

Open continuum-dipanjan-mazumder opened this issue 5 years ago • 3 comments

I am trying to serialize an AST object using gob with below function:

//Encodes an object in gob

func encodeToGob(object interface{}) (bytes.Buffer, error) {

var gobData bytes.Buffer   
gob.Register(ast.StringToken{})
gob.Register(ast.Object{})
gob.Register(ast.Elements{})
gob.Register(ast.TemplateString{})
e := gob.NewEncoder(&gobData)

if err := e.Encode(object); err != nil {
	fmt.Println("Err is:", err)
	return gobData, err
} else {
	return gobData, nil
}
} 

encode works but when i try to decode with below function : `func decodeGob(gobData bytes.Buffer, targetType interface{}) error { gob.Register(ast.StringToken{}) gob.Register(ast.Object{}) gob.Register(ast.Elements{}) gob.Register(ast.TemplateString{})

d := gob.NewDecoder(&gobData)

if err := d.Decode(targetType); err != nil {
	return err
}
return nil

} `

Its throwing error as below: gob: ast.TemplateString is not assignable to type ast.Value

The ast object is totally under gocc's user's control. So it is up to you to create an ast that is serializable. I usually make my asts protobuf objects.

awalterschulze avatar May 21 '19 20:05 awalterschulze

Could you try and make targetType be more specific than interface{}?

I think you would also need to include the ast file for this to be debugable.

awalterschulze avatar May 21 '19 20:05 awalterschulze

I have worked it out it was problem where the ast.TemplateString was implementing the interface ast.Value but ast.TemplateString methods were having pointer reciever , i had to remove the pointer reciever to make it work...