`#system_library "py:module"` names a Python module the way `go:` names a Go package, so a Ziran program built with --target=py calls Python libraries directly: module functions, dotted attributes such as bytes.fromhex, and methods on the first argument written (Type).name. string crosses as str, []u8 as bytes, integers wrap to their declared width, Python objects stay opaque (std/py_types.zi declares Object, with null for None), and a Ziran procedure argument becomes a callable Python can call back. #py_field reads or sets an attribute. #py_results catches a raised exception into the result record's last field, as "Type: message" text or as the exception object, with the result in an optional first field. Python imports survive saved IR (format 53); the C, C++, Go, and Rust targets reject them with a clear diagnostic, and zi2go now reports Python types and imports instead of emitting broken Go.
101 lines
4.3 KiB
Ziran
101 lines
4.3 KiB
Ziran
// Go HTTP requests, response writers and headers retain their native types and behavior.
|
|
url :: #import "url_go";
|
|
io :: #import "io_go";
|
|
ctx :: #import "context_go";
|
|
clock :: #import "time_go";
|
|
#import "go_types"
|
|
go_http :: #system_library "go:net/http";
|
|
builtin :: #system_library "go:builtin";
|
|
Request :: #type #foreign go_http "Request";
|
|
Header :: #type #foreign go_http "Header";
|
|
ResponseWriter :: #type #foreign go_http "ResponseWriter";
|
|
Client :: #type #foreign go_http "Client";
|
|
Response :: #type #foreign go_http "Response";
|
|
RequestResult :: struct {
|
|
value: *Request
|
|
error: Error
|
|
}
|
|
ResponseResult :: struct {
|
|
value: *Response
|
|
error: Error
|
|
}
|
|
|
|
#scope_file
|
|
RemoteAddressRaw :: (request: *Request) -> string #go_field #foreign go_http "(*Request).RemoteAddr";
|
|
HeadersRaw :: (request: *Request) -> Header #go_field #foreign go_http "(*Request).Header";
|
|
MethodRaw :: (request: *Request) -> string #go_field #foreign go_http "(*Request).Method";
|
|
URLRaw :: (request: *Request) -> *url.URL #go_field #foreign go_http "(*Request).URL";
|
|
BodyRaw :: (request: *Request) -> io.ReadCloser #go_field #foreign go_http "(*Request).Body";
|
|
ContextRaw :: (request: *Request) -> ctx.Context #foreign go_http "(*Request).Context";
|
|
MaxBytesReaderRaw :: (writer: ResponseWriter, reader: io.ReadCloser, limit: s64) -> io.ReadCloser #foreign go_http "MaxBytesReader";
|
|
WriteHeaderRaw :: (writer: ResponseWriter, status: isize) #foreign go_http "ResponseWriter.WriteHeader";
|
|
HeaderValueRaw :: (header: Header, name: string) -> string #foreign go_http "Header.Get";
|
|
ResponseHeadersRaw :: (writer: ResponseWriter) -> Header #foreign go_http "ResponseWriter.Header";
|
|
HeaderSetRaw :: (header: Header, name: string, value: string) #foreign go_http "Header.Set";
|
|
AllocateClient :: () -> *Client #foreign builtin "new";
|
|
SetTimeoutRaw :: (client: *Client, timeout: clock.Duration) #go_field #foreign go_http "(*Client).Timeout";
|
|
TimeoutRaw :: (client: *Client) -> clock.Duration #go_field #foreign go_http "(*Client).Timeout";
|
|
NewRequestRaw :: (context: ctx.Context, method: string, target: string, body: io.Reader) -> RequestResult #go_results #foreign go_http "NewRequestWithContext";
|
|
DoRaw :: (client: *Client, request: *Request) -> ResponseResult #go_results #foreign go_http "(*Client).Do";
|
|
ResponseBodyRaw :: (response: *Response) -> io.ReadCloser #go_field #foreign go_http "(*Response).Body";
|
|
StatusRaw :: (response: *Response) -> string #go_field #foreign go_http "(*Response).Status";
|
|
StatusCodeRaw :: (response: *Response) -> isize #go_field #foreign go_http "(*Response).StatusCode";
|
|
|
|
#scope_export
|
|
RemoteAddress :: (request: *Request) -> string {
|
|
return RemoteAddressRaw(request)
|
|
}
|
|
Headers :: (request: *Request) -> Header {
|
|
return HeadersRaw(request)
|
|
}
|
|
Method :: (request: *Request) -> string {
|
|
return MethodRaw(request)
|
|
}
|
|
RequestURL :: (request: *Request) -> *url.URL {
|
|
return URLRaw(request)
|
|
}
|
|
Body :: (request: *Request) -> io.ReadCloser {
|
|
return BodyRaw(request)
|
|
}
|
|
Context :: (request: *Request) -> ctx.Context {
|
|
return ContextRaw(request)
|
|
}
|
|
MaxBytesReader :: (writer: ResponseWriter, reader: io.ReadCloser, limit: s64) -> io.ReadCloser {
|
|
return MaxBytesReaderRaw(writer, reader, limit)
|
|
}
|
|
WriteHeader :: (writer: ResponseWriter, status: isize) {
|
|
WriteHeaderRaw(writer, status)
|
|
}
|
|
HeaderValue :: (header: Header, name: string) -> string {
|
|
return HeaderValueRaw(header, name)
|
|
}
|
|
ResponseHeaders :: (writer: ResponseWriter) -> Header {
|
|
return ResponseHeadersRaw(writer)
|
|
}
|
|
SetHeader :: (header: Header, name: string, value: string) {
|
|
HeaderSetRaw(header, name, value)
|
|
}
|
|
NewClient :: (timeout: clock.Duration) -> *Client {
|
|
client := AllocateClient()
|
|
SetTimeoutRaw(client, timeout)
|
|
return client
|
|
}
|
|
ClientTimeout :: (client: *Client) -> clock.Duration {
|
|
return TimeoutRaw(client)
|
|
}
|
|
NewRequest :: (context: ctx.Context, method: string, target: string, body: io.Reader) -> RequestResult {
|
|
return NewRequestRaw(context, method, target, body)
|
|
}
|
|
Do :: (client: *Client, request: *Request) -> ResponseResult {
|
|
return DoRaw(client, request)
|
|
}
|
|
ResponseBody :: (response: *Response) -> io.ReadCloser {
|
|
return ResponseBodyRaw(response)
|
|
}
|
|
Status :: (response: *Response) -> string {
|
|
return StatusRaw(response)
|
|
}
|
|
StatusCode :: (response: *Response) -> isize {
|
|
return StatusCodeRaw(response)
|
|
}
|