Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
continuum-dipanjan-mazumder opened this issue May 21, 2019 · 3 comments

Comments

@continuum-dipanjan-mazumder
Copy link

continuum-dipanjan-mazumder commented May 21, 2019

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
Copy link
Collaborator

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
Copy link
Collaborator

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants