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

Feature proposal: Render Statement with passed File #55

Closed
dsxack opened this issue Jan 28, 2019 · 2 comments
Closed

Feature proposal: Render Statement with passed File #55

dsxack opened this issue Jan 28, 2019 · 2 comments

Comments

@dsxack
Copy link
Contributor

dsxack commented Jan 28, 2019

Sometimes I need to render only body of File without imports to append exists file. So I need to call GoString() method on Statement and pass File (with presetted import aliases) as param to use valid import aliases in the code.

@dsxack dsxack changed the title Feature request: render Statement with passed File Feature proposal: render Statement with passed File Jan 28, 2019
@dsxack dsxack changed the title Feature proposal: render Statement with passed File Feature proposal: Render Statement with passed File Jan 28, 2019
@dsxack
Copy link
Contributor Author

dsxack commented Jan 28, 2019

// RenderWithFile renders the Statement to the provided writer.
func (s *Statement) RenderWithFile(writer io.Writer, f *File) error {
	buf := &bytes.Buffer{}
	if err := s.render(f, buf, nil); err != nil {
		return err
	}
	b, err := format.Source(buf.Bytes())
	if err != nil {
		return fmt.Errorf("Error %s while formatting source:\n%s", err, buf.String())
	}
	if _, err := writer.Write(b); err != nil {
		return err
	}
	return nil
}

// GoStringWithFile renders the Statement for testing. Any error will cause a panic.
func (s *Statement) GoStringWithFile(f *File) string {
	buf := bytes.Buffer{}
	if err := s.RenderWithFile(&buf, f); err != nil {
		panic(err)
	}
	return buf.String()
}

@dsxack
Copy link
Contributor Author

dsxack commented Jan 29, 2019

Fixed in #56

@dsxack dsxack closed this as completed Jan 29, 2019
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

1 participant