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

Raw function to insert raw string into the output #50

Open
magicsong opened this issue Nov 8, 2018 · 8 comments
Open

Raw function to insert raw string into the output #50

magicsong opened this issue Nov 8, 2018 · 8 comments

Comments

@magicsong
Copy link

Is it possible to toinsert a pile of code like

f.Insert(`
type Repository struct {
	ArchiveURL       string      `json:"archive_url"`
	AssigneesURL     string      `json:"assignees_url"`
	BlobsURL         string      `json:"blobs_url"`
	BranchesURL      string      `json:"branches_url"`
	CloneURL         string      `json:"clone_url"`
	CollaboratorsURL string      `json:"collaborators_url"`
	CommentsURL      string      `json:"comments_url"`
	CommitsURL       string      `json:"commits_url"`
	CompareURL       string      `json:"compare_url"`
	ContentsURL      string      `json:"contents_url"`
	ContributorsURL  string      `json:"contributors_url"`
	CreatedAt        string      `json:"created_at"`
	DefaultBranch    string      `json:"default_branch"`
	Description      string      `json:"description"`
	DownloadsURL     string      `json:"downloads_url"`
	EventsURL        string      `json:"events_url"`
	Fork             bool        `json:"fork"`
	Forks            float64     `json:"forks"`
	ForksCount       float64     `json:"forks_count"`
	ForksURL         string      `json:"forks_url"`
	FullName         string      `json:"full_name"`
	GitCommitsURL    string      `json:"git_commits_url"`
	GitRefsURL       string      `json:"git_refs_url"`
	GitTagsURL       string      `json:"git_tags_url"`
	GitURL           string      `json:"git_url"`
	HasDownloads     bool        `json:"has_downloads"`
	HasIssues        bool        `json:"has_issues"`
	HasWiki          bool        `json:"has_wiki"`
	Homepage         interface{} `json:"homepage"`
	HooksURL         string      `json:"hooks_url"`
	HtmlURL          string      `json:"html_url"`
	ID               float64     `json:"id"`
	IssueCommentURL  string      `json:"issue_comment_url"`
	IssueEventsURL   string      `json:"issue_events_url"`
	IssuesURL        string      `json:"issues_url"`
	KeysURL          string      `json:"keys_url"`
	LabelsURL        string      `json:"labels_url"`
	Language         string      `json:"language"`
	LanguagesURL     string      `json:"languages_url"`
	MasterBranch     string      `json:"master_branch"`
	MergesURL        string      `json:"merges_url"`
	MilestonesURL    string      `json:"milestones_url"`
	MirrorURL        interface{} `json:"mirror_url"`
	Name             string      `json:"name"`
	NetworkCount     float64     `json:"network_count"`
	NotificationsURL string      `json:"notifications_url"`
	OpenIssues       float64     `json:"open_issues"`
	OpenIssuesCount  float64     `json:"open_issues_count"`
	Owner            struct {
		AvatarURL         string  `json:"avatar_url"`
		EventsURL         string  `json:"events_url"`
		FollowersURL      string  `json:"followers_url"`
		FollowingURL      string  `json:"following_url"`
		GistsURL          string  `json:"gists_url"`
		GravatarID        string  `json:"gravatar_id"`
		HtmlURL           string  `json:"html_url"`
		ID                float64 `json:"id"`
		Login             string  `json:"login"`
		OrganizationsURL  string  `json:"organizations_url"`
		ReceivedEventsURL string  `json:"received_events_url"`
		ReposURL          string  `json:"repos_url"`
		SiteAdmin         bool    `json:"site_admin"`
		StarredURL        string  `json:"starred_url"`
		SubscriptionsURL  string  `json:"subscriptions_url"`
		Type              string  `json:"type"`
		URL               string  `json:"url"`
	} `json:"owner"`
	Private         bool    `json:"private"`
	PullsURL        string  `json:"pulls_url"`
	PushedAt        string  `json:"pushed_at"`
	Size            float64 `json:"size"`
	SshURL          string  `json:"ssh_url"`
	StargazersURL   string  `json:"stargazers_url"`
	StatusesURL     string  `json:"statuses_url"`
	SubscribersURL  string  `json:"subscribers_url"`
	SubscriptionURL string  `json:"subscription_url"`
	SvnURL          string  `json:"svn_url"`
	TagsURL         string  `json:"tags_url"`
	TeamsURL        string  `json:"teams_url"`
	TreesURL        string  `json:"trees_url"`
	UpdatedAt       string  `json:"updated_at"`
	URL             string  `json:"url"`
	Watchers        float64 `json:"watchers"`
	WatchersCount   float64 `json:"watchers_count"`
}
`)

such code is static and will not change.

@dave
Copy link
Owner

dave commented Nov 8, 2018

There's no specific function for inserting a raw string into the output, but Op will do exactly that. Be careful there's no qualified identifiers in the code you're adding because the packages they import won't be added to the imports block.

@magicsong
Copy link
Author

magicsong commented Nov 8, 2018

It is very useful when creating generated code. Why it doesn‘t have such api?Use Op is a hack or work-round. I suppose such api should appear in doc

@dave
Copy link
Owner

dave commented Nov 8, 2018

This isn't something I want to encourage this because if you slip a qualified identifier in by mistake it won't be added to the imports block.

@magicsong
Copy link
Author

@dave use Id.Dot() will also have such problem. If I don't use the Op workaround, it will become very tedious and such way is deviated from the reason why I use generated code.

@dave
Copy link
Owner

dave commented Nov 8, 2018

To add a qualified identifier, you should be using Qual - see the Identifiers section of the readme.

If I don't use the Op workaround

I don't see any reason not to use the Op work around in this case.

I think it's quite uncommon to be inserting such a large block of code into a generated file. Usually it's best to structure the project so the generated files are pretty dense with dynamic code, and the code that doesn't change is in a separate file.

@magicsong
Copy link
Author

I think Op should do as what is supposed to, not insert a block of code. If it can, from view of programers, it is a bug. If it does have such ability, why not make such function officially. In my case, i don't want to generate a lot of go files(because it is already much enough, naming a new file is becoming harder and harder for me. So just want to let the types and functions stay in a single go file).

@dave
Copy link
Owner

dave commented Nov 8, 2018

I understand that a Raw function would help in this situation, and I've considered adding this in the past. However, this situation is not one that I want to encourage, so I've intentionally omitted this feature. Thanks for the suggestion and I'll keep it in mind for the future.

@magicsong
Copy link
Author

Have you encounter a situation that the struct is dynamically generated from json. I use other tool generating the struct code. In this case, the raw function will useful

Sent with GitHawk

@dave dave changed the title Insert a pile of code Raw function to insert raw string into the output Nov 9, 2018
@matyat matyat mentioned this issue Nov 30, 2021
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