Skip to content

Commit

Permalink
Merge pull request #11 from hasura/fix-config-env-cli
Browse files Browse the repository at this point in the history
Fix CLI config directory env var and handle null values in response
  • Loading branch information
gneeri authored Jun 13, 2024
2 parents 4c6fabe + 413a2a4 commit bf14b09
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.1.1]

- Fixed the configuration directory environment variable in the CLI.
- Handled null values for nested fields in the response.

## [0.1.0]

- Initial release of the Hasura connector for Elasticsearch.
2 changes: 1 addition & 1 deletion cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

type Arguments struct {
// The path to the configuration. Defaults to the current directory.
Configuration string `help:"Configuration directory." env:"HASURA_CONFIGURATION_DIRECTORY"`
Configuration string `help:"Configuration directory." env:"HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH"`
}

type CLI struct {
Expand Down
43 changes: 28 additions & 15 deletions connector/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,37 @@ func prepareResponse(ctx context.Context, response map[string]interface{}) *sche
func extractDocument(source map[string]interface{}, selectedFields map[string]types.Field) map[string]interface{} {
document := make(map[string]interface{})
for fieldName, fieldData := range selectedFields {
if fieldData.Fields != nil {
if fieldDataSource, ok := source[fieldData.Name].(map[string]interface{}); ok {
documents := extractDocument(fieldDataSource, selectedFields[fieldName].Fields)
document[fieldName] = []interface{}{documents}
}
if fieldDataSource, ok := source[fieldData.Name].([]interface{}); ok {
docs := make([]interface{}, 0)
for _, data := range fieldDataSource {
doc := extractDocument(data.(map[string]interface{}), selectedFields[fieldName].Fields)
docs = append(docs, doc)
}
document[fieldName] = docs
document[fieldName] = extractSubDocument(source, fieldData)
}
return document
}

// extractSubDocument extracts sub-documents based on the selected fields.
func extractSubDocument(source map[string]interface{}, fieldData types.Field) interface{} {
if fieldData.Fields != nil {
sourceData, ok := source[fieldData.Name]
if !ok {
return []interface{}{extractDocument(make(map[string]interface{}), fieldData.Fields)}
}

if subDocument, ok := sourceData.(map[string]interface{}); ok {
return []interface{}{extractDocument(subDocument, fieldData.Fields)}
}

if subDocuments, ok := sourceData.([]interface{}); ok {
subDocumentsList := make([]interface{}, 0)
for _, subDocument := range subDocuments {
subDocumentsList = append(subDocumentsList, extractDocument(subDocument.(map[string]interface{}), fieldData.Fields))
}
} else {
document[fieldName] = source[fieldData.Name]
return subDocumentsList
}
}
return document

if fieldValue, ok := source[fieldData.Name]; ok {
return fieldValue
}

return nil
}

// extractAggregates extracts aggregate values from the source data and updates the row set aggregates.
Expand Down

0 comments on commit bf14b09

Please sign in to comment.