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

Proposal: hz supports generating handy RPC-style client code based on IDL #381

Closed
FGYFFFF opened this issue Nov 17, 2022 · 4 comments
Closed
Labels
enhancement New feature or request proposal

Comments

@FGYFFFF
Copy link
Contributor

FGYFFFF commented Nov 17, 2022

Proposal

hz supports generating hertz client code. All opinions on the user side are worth considering❤️.

Current Template(Opinions are all welcome)🤗

  • The initial client template that has been designed so far: client template.
  • Generate client code based on IDL and auto-populate http request parameters based on IDL's annotations.
  • If you have any idea or suggestion, you may reply below or mention pr to the branch example/hz_client_template.

Quickly experience🤓

  • You may have a try on this feature with this example.
  • You can try this feature with pr

Description🧐

  1. Writing an IDL, use hz for the code generation(coming soon):
namespace go cloudwego.hertz.template

struct Req {
  1: optional string QueryString(api.query="q1");
  2: optional string PathString(api.path="p1");
  3: optional string HeaderString(api.header="h1");
  4: optional string MixString(api.query="q2", api.header="h2", api.path="p2");
  5: optional string BodyString(api.body="sad");
}

struct Resp {
  1: optional string RespString(api.header="h1");
}

service Hertz {
    Resp BizMethod2(1: Req req)(api.post = '/life/client1');
    Resp BizMethod3(1: Req req)(api.post = '/life/client2');
    Resp BizMethod4(1: Req req)(api.post = '/life/client3');
}
  1. Call with generated client:
	str := "hello, hertz"
	req := template.Req{
		QueryString:  &str,
		PathString:   &str,
		HeaderString: &str,
	}

        // Case1 
	// Initiate interface calls directly, domain can be specified by IDL
	respStruct, rawResp, _ = hertz.BizMethod2(context.Background(), &req)


        // Also provide 3 ways of customization bellow:

	// Case2 
        // Pass the configuration parameters to create the client and initiate the http call
	cli, _ := hertz.NewClient("http://127.0.0.1:8899", hertz.WithHertzClientOption(client.WithKeepAlive(true)))
	respStruct, rawResp, _ := cli.BizMethod2(context.Background(), &req, config.WithSD(false))

	// Case3
	// Create a client by passing a hertz client that has already been initialized
	customizedClient, _ := client.NewClient()
	cli2, _ := hertz.NewClient("http://127.0.0.1:8899", hertz.WithHertzClient(customizedClient))
	respStruct, rawResp, _ = cli2.BizMethod2(context.Background(), &req)
	
        // Case4
	// Injecting client middleware
	cli3, _ := hertz.NewClient("http://127.0.0.1:8899", hertz.WithHertzClientMiddleware())
	respStruct, rawResp, _ = cli3.BizMethod2(context.Background(), &req)
@welkeyever welkeyever changed the title Proposal: Call for comments for template of 'hertz client' generated by 'hz' based on IDL Proposal: hz supports generating handy RPC-style client code based on IDL Nov 18, 2022
@welkeyever welkeyever added the enhancement New feature or request label Nov 18, 2022
@welkeyever welkeyever pinned this issue Nov 22, 2022
@li-jin-gou
Copy link
Member

I think we should also support not using the Go-Moudle‘s case.

@a631807682
Copy link
Member

Is it possible to parse it into an AST that meets the needs of the http client, and let the community or users themselves use different generators to support code generation in different languages or different client frameworks?
It is difficult for us to decide whether users should use the hertz client or even the go language.

@welkeyever
Copy link
Member

Is it possible to parse it into an AST that meets the needs of the http client, and let the community or users themselves use different generators to support code generation in different languages or different client frameworks? It is difficult for us to decide whether users should use the hertz client or even the go language.

IDL is already implementation-neutral, what hz does here, is generate the code according to IDL.

@FGYFFFF
Copy link
Contributor Author

FGYFFFF commented Dec 7, 2022

possible to parse it into an AST that meets the needs of the http client, and let the community or users themselves use different generators to support code generation in different languages or different client frameworks?
It is difficult for us to decide whether users should use the hertz client or even the go lang

Currently, this solution is used to generate the hertz client. If you need an http client for another language or another framework, you can parse the AST and generate the code yourself

@li-jin-gou li-jin-gou unpinned this issue Dec 7, 2022
@li-jin-gou li-jin-gou pinned this issue Jan 15, 2023
@li-jin-gou li-jin-gou unpinned this issue Jan 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request proposal
Development

No branches or pull requests

4 participants