Skip to content

Commit

Permalink
refactor(v2): add style guide, remove unused common package (#166)
Browse files Browse the repository at this point in the history
* refactor(v2): add style guide, remove unused common package

* revert(v2): remove default search body declarations
  • Loading branch information
iljaSL authored Nov 21, 2024
1 parent a5b96aa commit 5c542f6
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 40 deletions.
10 changes: 0 additions & 10 deletions api/connectionmanager/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package connectionmanager

import (
"net/url"
"time"

"github.com/SSHcom/privx-sdk-go/api/filters"
"github.com/SSHcom/privx-sdk-go/api/response"
Expand Down Expand Up @@ -67,15 +66,6 @@ func (c *ConnectionManager) SearchConnections(search *ConnectionSearch, opts ...
connections := &response.ResultSet[Connection]{}
params := url.Values{}

// Define default connected start time if empty (similar on how UI is handling start time).
if search != nil && search.Connected == nil {
search = &ConnectionSearch{
Connected: &TimestampSearch{
Start: time.Now().UTC().AddDate(0, 0, -8).Format("2006-01-02T15:04:05.000Z"),
},
}
}

// Set default options, which will be overwritten by opts if defined.
options := append([]filters.Option{
filters.Paging(0, 25),
Expand Down
8 changes: 0 additions & 8 deletions api/monitor/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package monitor
import (
"encoding/json"
"net/url"
"time"

"github.com/SSHcom/privx-sdk-go/api/filters"
"github.com/SSHcom/privx-sdk-go/api/response"
Expand Down Expand Up @@ -44,13 +43,6 @@ func (c *Monitor) SearchAuditEvents(search *AuditEventSearch, opts ...filters.Op
events := &response.ResultSet[AuditEvent]{}
params := url.Values{}

// Define default start time if empty (similar on how UI is handling start time).
if search != nil && search.StartTime == "" {
search = &AuditEventSearch{
StartTime: time.Now().UTC().AddDate(0, 0, -8).Format("2006-01-02T15:04:05.000Z"),
}
}

// Set default options, which will be overwritten by opts if defined.
options := append([]filters.Option{
filters.Paging(0, 25),
Expand Down
22 changes: 0 additions & 22 deletions common/model.go

This file was deleted.

79 changes: 79 additions & 0 deletions styleguide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# PrivX SDK Style Guide

The are a few guidelines to follow when developing or contributing to the PrivX Go SDK.
This documentation should help to point out those guidelines and read before opening a pull request in order to keep the SDK consistent.

# Table of content
- [Creating And Initializing New Client](#creating-and-initializing-new-client)
- [Adding New Handlers And Models](#adding-new-handlers-and-models)
- [Naming Conventions for Handler Functions](#naming-conventions-for-handler-functions)
- [Parameter Ordering in Handler Functions](#parameter-ordering-in-handler-functions)
- [Return Types for Handler Functions](#return-types-for-handler-functions)

## Creating And Initializing New Client

When adding a new client (service) to the SDK, this should be done in the `api` directory.
The package name should be the service name that is new and is not yet defined in the `api` directory.
Create a new directory inside `api` with the name of the service and the corresponding files `model.go` which should contain any struct definitions and a `client.go` file containing the API methods and the client constructor.

The client should be initialized as follow:
```go
type Client struct {
api restapi.Connector
}

func New(api restapi.Connector) *Client {
return &Client{api: api}
}
```

There is no need to add the service name to any struct or constructor.
It will be verbose enough when the client is eventually imported and initialized.

```go
// initializing new client
client := rolestore.New(api)
```

## Adding New Handlers And Models

### Naming Conventions for Handler Functions

Handler function names should follow a consistent structure:
* Start with a verb describing the action
* Follow the verb with the subject (the source of the endpoint)

Example:
```
func (c *HostStore) GetHosts() {...} // (get multiple entries)
func (c *HostStore) GetHost() {...} // (get one entry)
func (c *HostStore) CreateHost() {...}
func (c *HostStore) DeleteHost() {...}
func (c *HostStore) UpdateHost() {...}
func (c *HostStore) SearchHosts() {...}
```

### Parameter Ordering in Handler Functions

When a function requires parameters, they should be ordered as follows:
* The ID (or multiple IDs, if applicable) of the source must always come first
* Any additional parameters with basic types (e.g., string, int) should follow the IDs
* If required, a structured request body should come after basic-type parameters
* End with variadic options, e.g., opts ...filters.Option

Example:
```
func (c *HostStore) UpdateHost(hostID string, host *Host, opts ...filters.Option) {...}
```

### Return Types for Handler Functions

For handlers returning multiple entries, always use the generic `ResultSet` type from the `response` package which has the Count and Item fields defined.

Example:
```
func (c *HostStore) GetHosts(...) (*response.ResultSet[Host], error)
```
This standardizes response behavior across all handlers, addressing inconsistencies in earlier SDK versions. Avoid returning raw slices like `[]Host`.

For handlers that return an ID (e.g., when creating entries), use the `Identifier` type from the `response` package.

0 comments on commit 5c542f6

Please sign in to comment.