Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Lattice SDK Go library
# Lattice SDK Go Library

![](https://www.anduril.com/lattice-sdk/)

The Lattice SDK Go library provides convenient access to the Lattice API from Go.

The Lattice SDK Go library provides convenient access to the Lattice SDK APIs from Go.

## Documentation

Expand All @@ -26,6 +27,10 @@ go get github.com/anduril/lattice-sdk-go/v2

For support with this library please reach out to your Anduril representative.

## Reference

A full reference for this library is available [here](https://github.com/anduril/lattice-sdk-go/blob/HEAD/./reference.md).

## Usage

Instantiate and use the client with the following:
Expand All @@ -37,7 +42,7 @@ import (
client "github.com/anduril/lattice-sdk-go/v2/client"
option "github.com/anduril/lattice-sdk-go/v2/option"
context "context"
Lattice "github.com/anduril/lattice-sdk-go/v2"
v2 "github.com/anduril/lattice-sdk-go/v2"
)

func do() {
Expand All @@ -48,7 +53,7 @@ func do() {
)
client.Entities.LongPollEntityEvents(
context.TODO(),
&Lattice.EntityEventRequest{
&v2.EntityEventRequest{
SessionToken: "sessionToken",
},
)
Expand Down
11 changes: 5 additions & 6 deletions client/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion client/client_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions core/request_option.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions entities/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions entities/raw_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 60 additions & 10 deletions internal/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,81 @@ type QueryEncoder interface {
EncodeQueryValues(key string, v *url.Values) error
}

// QueryValues encodes url.Values from request objects.
//
// Note: This type is inspired by Google's query encoding library, but
// supports far less customization and is tailored to fit this SDK's use case.
//
// Ref: https://github.com/google/go-querystring
func QueryValues(v interface{}) (url.Values, error) {
// prepareValue handles common validation and unwrapping logic for both functions
func prepareValue(v interface{}) (reflect.Value, url.Values, error) {
values := make(url.Values)
val := reflect.ValueOf(v)
for val.Kind() == reflect.Ptr {
if val.IsNil() {
return values, nil
return reflect.Value{}, values, nil
}
val = val.Elem()
}

if v == nil {
return values, nil
return reflect.Value{}, values, nil
}

if val.Kind() != reflect.Struct {
return nil, fmt.Errorf("query: Values() expects struct input. Got %v", val.Kind())
return reflect.Value{}, nil, fmt.Errorf("query: Values() expects struct input. Got %v", val.Kind())
}

err := reflectValue(values, val, "")
if err != nil {
return reflect.Value{}, nil, err
}

return val, values, nil
}

// QueryValues encodes url.Values from request objects.
//
// Note: This type is inspired by Google's query encoding library, but
// supports far less customization and is tailored to fit this SDK's use case.
//
// Ref: https://github.com/google/go-querystring
func QueryValues(v interface{}) (url.Values, error) {
_, values, err := prepareValue(v)
return values, err
}

// QueryValuesWithDefaults encodes url.Values from request objects
// and default values, merging the defaults into the request.
// It's expected that the values of defaults are wire names.
func QueryValuesWithDefaults(v interface{}, defaults map[string]interface{}) (url.Values, error) {
val, values, err := prepareValue(v)
if err != nil {
return values, err
}

// apply defaults to zero-value fields directly on the original struct
valType := val.Type()
for i := 0; i < val.NumField(); i++ {
field := val.Field(i)
fieldType := valType.Field(i)
fieldName := fieldType.Name

if fieldType.PkgPath != "" && !fieldType.Anonymous {
// Skip unexported fields.
continue
}

// check if field is zero value and we have a default for it
if field.CanSet() && field.IsZero() {
tag := fieldType.Tag.Get("url")
if tag == "" || tag == "-" {
continue
}
wireName, _ := parseTag(tag)
if wireName == "" {
wireName = fieldName
}
if defaultVal, exists := defaults[wireName]; exists {
values.Set(wireName, valueString(reflect.ValueOf(defaultVal), tagOptions{}, reflect.StructField{}))
}
}
}

return values, err
}

Expand Down
Loading