Skip to content

Commit

Permalink
Handle null values for nested fields in response
Browse files Browse the repository at this point in the history
  • Loading branch information
darshan-lukhi committed Jun 13, 2024
1 parent eee0bb3 commit 413a2a4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 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.
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 413a2a4

Please sign in to comment.