goccmack / gocc

Parser / Scanner Generator
Other
612 stars 48 forks source link

Serializing an AST structure with gob and then deserializing fails #89

Open continuum-dipanjan-mazumder opened 5 years ago

continuum-dipanjan-mazumder commented 5 years ago

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

awalterschulze commented 5 years ago

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 commented 5 years ago

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.

continuum-dipanjan-mazumder commented 5 years ago

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...