Skip to content

Commit

Permalink
Merge pull request #39 from hyperledger-labs/fix/resolve-history
Browse files Browse the repository at this point in the history
Fix resolve history reference
  • Loading branch information
samuelvenzi authored Oct 25, 2023
2 parents c43acbc + 44e7c5b commit 8853db5
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions assets/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func History(stub *sw.StubWrapper, key string, resolve bool) (*HistoryResponse,
defer resultsIterator.Close()

historyResult := make([]map[string]interface{}, 0)
var subAssets []AssetProp

for resultsIterator.HasNext() {
queryResponse, err := resultsIterator.Next()
Expand All @@ -40,15 +41,18 @@ func History(stub *sw.StubWrapper, key string, resolve bool) (*HistoryResponse,
}

if resolve {
key, err := NewKey(data)
if err != nil {
return nil, errors.WrapError(err, "failed to create key object to resolve result")
if subAssets == nil {
key, err := NewKey(data)
if err != nil {
return nil, errors.WrapError(err, "failed to create key object to resolve result")
}
subAssets = key.Type().SubAssets()
}
asset, err := key.GetRecursive(stub)

err := resolveHistory(stub, data, subAssets)
if err != nil {
return nil, errors.WrapError(err, "failed to resolve result")
}
data = asset
}

historyResult = append(historyResult, data)
Expand All @@ -60,3 +64,26 @@ func History(stub *sw.StubWrapper, key string, resolve bool) (*HistoryResponse,

return &response, nil
}

func resolveHistory(stub *sw.StubWrapper, data map[string]interface{}, subAssets []AssetProp) errors.ICCError {
for _, refProp := range subAssets {
ref, ok := data[refProp.Tag].(map[string]interface{})
if !ok {
continue
}

key, err := NewKey(ref)
if err != nil {
return errors.WrapError(err, "could not make subasset key")
}

resolved, err := key.GetRecursive(stub)
if err != nil {
return errors.WrapError(err, "failed to get subasset recursive")
}

data[refProp.Tag] = resolved
}

return nil
}

0 comments on commit 8853db5

Please sign in to comment.