From b44550ebe15cf26ddf6e44dd09ca288f9363dc4c Mon Sep 17 00:00:00 2001 From: mikaellafs Date: Wed, 10 Jan 2024 14:26:26 -0300 Subject: [PATCH] Fix resolve history reference Signed-off-by: mikaellafs --- assets/history.go | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/assets/history.go b/assets/history.go index 380c79d..8c93999 100644 --- a/assets/history.go +++ b/assets/history.go @@ -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() @@ -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) @@ -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 +}