Skip to content

Commit

Permalink
Create go.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
SoMuchForSubtlety committed Jan 11, 2020
1 parent 132db4b commit 5890493
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 57 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
on: [push, pull_request]
name: Test
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v1
with:
go-version: 1.13.x
- name: Checkout code
uses: actions/checkout@v1
- name: Test
run: go test ./...
build:
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v1
with:
go-version: 1.13.x
- name: Checkout code
uses: actions/checkout@v1
- name: Build
run: go build -v .
2 changes: 1 addition & 1 deletion client_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package golark

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion exaple_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package golark

import "log"

Expand Down
44 changes: 22 additions & 22 deletions field.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package golark

import (
"fmt"
Expand All @@ -7,35 +7,35 @@ import (

// Field represents a Skylark request field
type Field struct {
Name string
IsIncluded bool
IsExpanded bool
SubFields map[string]*Field
name string
isIncluded bool
isExpanded bool
subFields map[string]*Field
filters []*Filter
}

// NewField creates a new field
func NewField(name string) *Field {
return &Field{Name: name, SubFields: make(map[string]*Field), IsIncluded: true}
return &Field{name: name, subFields: make(map[string]*Field), isIncluded: true}
}

func (f *Field) apply(v url.Values) url.Values {
if f.IsIncluded {
v = addValue(v, "fields", f.Name)
if f.isIncluded {
v = addValue(v, "fields", f.name)
}
if f.IsExpanded {
v = addValue(v, "fields_to_expand", f.Name)
if f.isExpanded {
v = addValue(v, "fields_to_expand", f.name)
}
for _, filter := range f.filters {
var key string
if filter.c == "" {
key = f.Name
key = f.name
} else {
key = fmt.Sprintf("%s__%s", f.Name, filter.c)
key = fmt.Sprintf("%s__%s", f.name, filter.c)
}
v.Add(key, filter.value)
}
for _, field := range f.SubFields {
for _, field := range f.subFields {
v = field.apply(v)
}
return v
Expand All @@ -44,15 +44,15 @@ func (f *Field) apply(v url.Values) url.Values {
// WithSubField expands a field and adds the given field to the list of filds to be returned.
// Only use this if the field is a reference to a different object!
func (f *Field) WithSubField(subField *Field) *Field {
f.IsExpanded = true
subField.adjustName(f.Name)
f.SubFields[subField.Name] = subField
f.isExpanded = true
subField.adjustName(f.name)
f.subFields[subField.name] = subField
return f
}

func (f *Field) adjustName(parentName string) {
f.Name = fmt.Sprintf("%s__%s", parentName, f.Name)
for _, field := range f.SubFields {
f.name = fmt.Sprintf("%s__%s", parentName, f.name)
for _, field := range f.subFields {
field.adjustName(parentName)
}
}
Expand All @@ -66,9 +66,9 @@ func (f *Field) WithFilter(filter *Filter) *Field {
// Expand expands a field without explicitly listing it as a field to return.
// This is usefult if you want to return all fields.
func (f *Field) Expand(subField *Field) *Field {
subField.IsExpanded = true
subField.IsIncluded = false
subField.adjustName(f.Name)
f.SubFields[subField.Name] = subField
subField.isExpanded = true
subField.isIncluded = false
subField.adjustName(f.name)
f.subFields[subField.name] = subField
return f
}
2 changes: 1 addition & 1 deletion filter.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package golark

// Filter represents a Skylark request filter
// It is used to constrain a request by a field's value
Expand Down
62 changes: 31 additions & 31 deletions request.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package golark

import (
"context"
Expand All @@ -15,28 +15,28 @@ type Request struct {
Endpoint string
Collection string
ID string
Fields map[string]*Field
fields map[string]*Field
ctx context.Context
additionalFields map[string]string
}

// NewRequest returns a simple request with the given
func NewRequest(endpoint, collection, id string) *Request {
return &Request{
Collection: collection, Endpoint: endpoint, Fields: make(map[string]*Field), additionalFields: make(map[string]string), ID: id, ctx: context.Background()}
Collection: collection, Endpoint: endpoint, fields: make(map[string]*Field), additionalFields: make(map[string]string), ID: id, ctx: context.Background()}
}

// AddField adds a field to the request.
// If a request has fields specified it will only return those fields.
func (r *Request) AddField(f *Field) *Request {
r.Fields[f.Name] = f
r.fields[f.name] = f
return r
}

// QueryParams calculates and returns the request's query parameters.
func (r *Request) QueryParams() url.Values {
v := url.Values{}
for _, field := range r.Fields {
for _, field := range r.fields {
v = field.apply(v)
}
for key, value := range r.additionalFields {
Expand All @@ -45,32 +45,9 @@ func (r *Request) QueryParams() url.Values {
return v
}

// ToURL converts the request into a url.URL
func (r *Request) ToURL() (*url.URL, error) {
temp := r.Endpoint + r.Collection + "/"
if r.ID != "" {
temp += r.ID + "/"
}
queryParams := r.QueryParams().Encode()
if queryParams != "" {
temp += "?" + queryParams
}
return url.Parse(temp)
}

// WithContext set's the context the request will be executed with.
// Panics on nil context
func (r *Request) WithContext(ctx context.Context) *Request {
if ctx == nil {
panic("nil context")
}
r.ctx = ctx
return r
}

// OrderBy sorts the response by the given field
func (r *Request) OrderBy(f *Field) *Request {
r.additionalFields["order"] = f.Name
r.additionalFields["order"] = f.name
return r
}

Expand All @@ -86,12 +63,22 @@ func (r *Request) WithFilter(fieldName string, filter *Filter) *Request {
// Expand expands a field without explicitly listing it as a field to return.
// This is usefult if you want to return all fields.
func (r *Request) Expand(f *Field) *Request {
f.IsExpanded = true
f.IsIncluded = false
f.isExpanded = true
f.isIncluded = false
r.AddField(f)
return r
}

// WithContext set's the context the request will be executed with.
// Panics on nil context
func (r *Request) WithContext(ctx context.Context) *Request {
if ctx == nil {
panic("nil context")
}
r.ctx = ctx
return r
}

// Execute executes the request and writes it's results to the value pointed to by v.
func (r *Request) Execute(v interface{}) error {
url, err := r.ToURL()
Expand All @@ -118,3 +105,16 @@ func (r *Request) Execute(v interface{}) error {

return json.NewDecoder(res.Body).Decode(v)
}

// ToURL converts the request into a url.URL
func (r *Request) ToURL() (*url.URL, error) {
temp := r.Endpoint + r.Collection + "/"
if r.ID != "" {
temp += r.ID + "/"
}
queryParams := r.QueryParams().Encode()
if queryParams != "" {
temp += "?" + queryParams
}
return url.Parse(temp)
}
2 changes: 1 addition & 1 deletion util.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package golark

import "net/url"

Expand Down

0 comments on commit 5890493

Please sign in to comment.