Skip to content

Commit

Permalink
Chore: publishable go module (#106)
Browse files Browse the repository at this point in the history
* make root a go module for publishing

* go work sync

* add support for go project initialization

* add go mod tidy instructions
  • Loading branch information
joshmossas authored Nov 12, 2024
1 parent 122d0ea commit 3e9341a
Show file tree
Hide file tree
Showing 30 changed files with 167 additions and 146 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"listhen",
"middlewares",
"modii",
"modiimedia",
"nitropack",
"nocheck",
"ofetch",
Expand Down
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
module github.com/modiimedia/arri

go 1.22.5

require github.com/iancoleman/strcase v0.3.0

require (
github.com/tidwall/gjson v1.18.0
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
)
1 change: 0 additions & 1 deletion languages/go/go-server/go.sum → go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY=
github.com/tidwall/gjson v1.18.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
8 changes: 5 additions & 3 deletions go.work
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
go 1.22.5

use ./languages/go/go-server
use ./playground/go
use ./tests/servers/go
use (
.
./playground/go
./tests/servers/go
)
5 changes: 1 addition & 4 deletions go.work.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
15 changes: 15 additions & 0 deletions languages/go/go-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Go implementation of [Arri RPC](/README.md). It uses the `net/http` package from

- [Quickstart](#quickstart)
- [Basic Example](#basic-example)
- [Manual Setup](#manual-setup)
- [Creating HTTP Procedures](#creating-http-procedures)
- [Creating Event Stream Procedures](#creating-event-stream-procedures)
- [Defining Messages](#defining-messages)
Expand All @@ -21,6 +22,8 @@ Go implementation of [Arri RPC](/README.md). It uses the `net/http` package from

## Quickstart

The Arri CLI comes with an initialization script that will scaffold a basic go server for Arri RPC.

```bash
# npm
npx arri init [project-name]
Expand All @@ -35,6 +38,18 @@ pnpm install
pnpm run dev
```

Follow the prompts and be sure to select go as your language of choice:

```terminal
What kind of project do you want to initialize?
-> application
generator plugin
What language do you want to use?
typescript
-> go
```

## Basic Example

```go
Expand Down
2 changes: 1 addition & 1 deletion languages/go/go-server/decode_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"reflect"
"testing"

arri "arri"
arri "github.com/modiimedia/arri/languages/go/go-server"
)

var _objectWithEveryTypeInput, _objectWithEveryTypeInputErr = os.ReadFile("../../../tests/test-files/ObjectWithEveryType.json")
Expand Down
2 changes: 1 addition & 1 deletion languages/go/go-server/decode_url_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"
"time"

arri "arri"
arri "github.com/modiimedia/arri/languages/go/go-server"
)

type queryResultWithEveryType struct {
Expand Down
2 changes: 1 addition & 1 deletion languages/go/go-server/encode_json_new.go___
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package arri
package go_server

import (
"encoding/json"
Expand Down
2 changes: 1 addition & 1 deletion languages/go/go-server/encode_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"testing"
"time"

arri "arri"
arri "github.com/modiimedia/arri/languages/go/go-server"
)

var testDate = time.Date(2001, time.January, 01, 16, 0, 0, 0, time.UTC)
Expand Down
13 changes: 0 additions & 13 deletions languages/go/go-server/go.mod

This file was deleted.

2 changes: 1 addition & 1 deletion languages/go/go-server/testing_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"reflect"
"time"

arri "arri"
arri "github.com/modiimedia/arri/languages/go/go-server"
)

type nestedObject struct {
Expand Down
2 changes: 1 addition & 1 deletion languages/go/go-server/type_def_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"reflect"
"testing"

arri "arri"
arri "github.com/modiimedia/arri/languages/go/go-server"
)

func TestStringToTypeDef(t *testing.T) {
Expand Down
16 changes: 8 additions & 8 deletions languages/rust/rust-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub struct ArriParsedRequestOptions<'a> {
}

#[derive(Debug)]
pub struct ArriServerError {
pub struct arriError {
pub code: u16,
pub message: String,
pub stack: Option<String>,
Expand All @@ -65,7 +65,7 @@ trait ArriRequestErrorMethods {
fn from_response_data(status: u16, body: String) -> Self;
}

impl ArriRequestErrorMethods for ArriServerError {
impl ArriRequestErrorMethods for arriError {
fn from_response_data(status: u16, body: String) -> Self {
let mut err = Self::from_json_string(body.to_owned());
if err.code == 0 {
Expand All @@ -78,7 +78,7 @@ impl ArriRequestErrorMethods for ArriServerError {
}
}

impl ArriModel for ArriServerError {
impl ArriModel for arriError {
fn new() -> Self {
Self {
code: 0,
Expand Down Expand Up @@ -192,7 +192,7 @@ impl ArriModel for ArriServerError {
pub async fn arri_request<'a>(
opts: ArriRequestOptions<'a>,
params: Option<impl ArriModel>,
) -> Result<reqwest::Response, ArriServerError> {
) -> Result<reqwest::Response, arriError> {
let response: Result<reqwest::Response, reqwest::Error>;
let mut headers: HashMap<&str, String> = HashMap::new();
{
Expand Down Expand Up @@ -302,7 +302,7 @@ pub async fn arri_request<'a>(
match response {
Ok(res) => return Ok(res),
Err(err) => {
return Err(ArriServerError {
return Err(arriError {
code: err.status().unwrap_or(StatusCode::default()).as_u16(),
message: format!("Error requesting \"{}\"", opts.url),
stack: None,
Expand Down Expand Up @@ -357,7 +357,7 @@ pub async fn parsed_arri_request<'a, TResponse>(
opts: ArriParsedRequestOptions<'a>,
params: Option<impl ArriModel>,
parser: fn(body: String) -> TResponse,
) -> Result<TResponse, ArriServerError> {
) -> Result<TResponse, arriError> {
let result = arri_request(
ArriRequestOptions {
method: opts.method,
Expand All @@ -376,15 +376,15 @@ pub async fn parsed_arri_request<'a, TResponse>(
let status = response.status().as_u16();
let body: Result<String, reqwest::Error> = response.text().await;
if status >= 300 || status < 200 {
return Err(ArriServerError::from_response_data(
return Err(arriError::from_response_data(
status,
body.unwrap_or_default(),
));
}
match body {
Ok(text) => return Ok(parser(text)),
Err(err) => {
return Err(ArriServerError {
return Err(arriError {
code: status,
message: "Expected server to return plaintext".to_string(),
stack: None,
Expand Down
8 changes: 4 additions & 4 deletions languages/rust/rust-client/src/sse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
time::{Duration, Instant},
};

use crate::{ArriModel, ArriRequestErrorMethods, ArriServerError};
use crate::{ArriModel, ArriRequestErrorMethods, arriError};

pub struct ArriParsedSseRequestOptions<'a> {
pub client: &'a reqwest::Client,
Expand All @@ -21,7 +21,7 @@ pub struct ArriParsedSseRequestOptions<'a> {

pub enum SseEvent<T> {
Message(T),
Error(ArriServerError),
Error(arriError),
Open,
Close,
}
Expand Down Expand Up @@ -204,7 +204,7 @@ impl<'a> EventSource<'a> {
}

if !response.is_ok() {
on_event(SseEvent::Error(ArriServerError::new()), &mut controller);
on_event(SseEvent::Error(arriError::new()), &mut controller);
if controller.is_aborted {
return SseAction::Abort;
}
Expand All @@ -219,7 +219,7 @@ impl<'a> EventSource<'a> {
if status < 200 || status >= 300 {
let body = ok_response.text().await.unwrap_or_default();
on_event(
SseEvent::Error(ArriServerError::from_response_data(status, body)),
SseEvent::Error(arriError::from_response_data(status, body)),
&mut controller,
);
if controller.is_aborted {
Expand Down
9 changes: 5 additions & 4 deletions languages/rust/rust-codegen-reference/src/example_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
deprecated
)]
use arri_client::{
arriError,
chrono::{DateTime, FixedOffset},
parsed_arri_request,
reqwest::{self, Request},
serde_json::{self, Map},
sse::{parsed_arri_sse_request, ArriParsedSseRequestOptions, SseController, SseEvent},
utils::{serialize_date_time, serialize_string},
ArriClientConfig, ArriClientService, ArriEnum, ArriModel, ArriParsedRequestOptions,
ArriServerError, EmptyArriModel, InternalArriClientConfig,
EmptyArriModel, InternalArriClientConfig,
};
use std::collections::{BTreeMap, HashMap};

Expand All @@ -39,7 +40,7 @@ impl ArriClientService for ExampleClient {
}

impl ExampleClient {
pub async fn send_object(&self, params: NestedObject) -> Result<NestedObject, ArriServerError> {
pub async fn send_object(&self, params: NestedObject) -> Result<NestedObject, arriError> {
parsed_arri_request(
ArriParsedRequestOptions {
http_client: &self._config.http_client,
Expand Down Expand Up @@ -75,7 +76,7 @@ impl ArriClientService for ExampleClientBooksService {

impl ExampleClientBooksService {
/// Get a book
pub async fn get_book(&self, params: BookParams) -> Result<Book, ArriServerError> {
pub async fn get_book(&self, params: BookParams) -> Result<Book, arriError> {
parsed_arri_request(
ArriParsedRequestOptions {
http_client: &self._config.http_client,
Expand All @@ -92,7 +93,7 @@ impl ExampleClientBooksService {

/// Create a book
#[deprecated]
pub async fn create_book(&self, params: Book) -> Result<Book, ArriServerError> {
pub async fn create_book(&self, params: Book) -> Result<Book, arriError> {
parsed_arri_request(
ArriParsedRequestOptions {
http_client: &self._config.http_client,
Expand Down
2 changes: 1 addition & 1 deletion languages/rust/rust-codegen/src/_index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ use arri_client::{
sse::{parsed_arri_sse_request, ArriParsedSseRequestOptions, SseController, SseEvent},
utils::{serialize_date_time, serialize_string},
ArriClientConfig, ArriClientService, ArriEnum, ArriModel, ArriParsedRequestOptions,
ArriServerError, EmptyArriModel, InternalArriClientConfig,
arriError, EmptyArriModel, InternalArriClientConfig,
};
use std::collections::{BTreeMap, HashMap};
Expand Down
2 changes: 1 addition & 1 deletion languages/rust/rust-codegen/src/procedures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export function rustHttpRpcFromSchema(
return `${leading}pub async fn ${functionName}(
&self,
${params ? `params: ${context.typeNamePrefix}${params},` : ""}
) -> Result<${context.typeNamePrefix}${response ?? "()"}, ArriServerError> {
) -> Result<${context.typeNamePrefix}${response ?? "()"}, arriError> {
parsed_arri_request(
ArriParsedRequestOptions {
http_client: &self._config.http_client,
Expand Down
4 changes: 2 additions & 2 deletions languages/ts/ts-server/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
} from "h3";

import { RequestHookContext } from "./context";
import { type ArriServerError, defineError, handleH3Error } from "./errors";
import { type arriError, defineError, handleH3Error } from "./errors";
import { isEventStreamRpc, registerEventStreamRpc } from "./eventStreamRpc";
import { type Middleware, MiddlewareEvent } from "./middleware";
import { type ArriRoute, registerRoute } from "./route";
Expand Down Expand Up @@ -284,7 +284,7 @@ export interface ArriOptions {
onAfterResponse?: (event: RequestHookEvent) => void | Promise<void>;
onBeforeResponse?: (event: RequestHookEvent) => void | Promise<void>;
onError?: (
error: ArriServerError,
error: arriError,
event: RequestHookEvent,
) => void | Promise<void>;
}
Expand Down
Loading

0 comments on commit 3e9341a

Please sign in to comment.