ScottPJones / LibBSON.jl

libbson bindings for the Julia programming language
Other
9 stars 16 forks source link

ERROR: MethodError: no method matching done(::BSONObject, ::Bool) #34

Open shivchawla opened 5 years ago

shivchawla commented 5 years ago

I am updating my code from Julia v0.6 to v0.7.

LibBSON.dict() is throwing an error

BSONObject wrap: Ptr{Nothing} @0x00007fdfed51f5a0 owner: Core.TypeofBottom Union{} k = 0, v = 250 ERROR: MethodError: no method matching done(::BSONObject, ::Bool) Closest candidates are: done(::I, ::Base.LegacyIterationCompat{I,T,S}) where {I, T, S} at essentials.jl:896 done(::AbstractString, ::Integer) at deprecated.jl:53

securitydoc = BSONObject({ "exchange" : "NSE", "ticker" : "NIFTY_50", "name" : "National Stock Exchange of India (NSE) Stock Index - Nifty 50", "_id" : { "$oid" : "59647a141078ddab805d5cf8" }, "securitytype" : "EQ", "datasources" : [ { "refreshed_at" : "2018-03-13T15:01:17.527Z", "premium" : false, "name" : "National Stock Exchange of India (NSE) Stock Index - Nifty 50", "dataset_code" : "NIFTY_50", "sourcename" : "quandl_NSE", "oldest_available_date" : "1990-07-03", "id" : 30022566, "description" : "Historical indexes for Nifty 50 (NIFTY_50), National Stock Exchange of India (NSE).", "newest_available_date" : "2018-03-13", "database_code" : "NSE", "database_id" : 33, "column_names" : [ "Date", "Open", "High", "Low", "Close", "Shares Traded", "Turnover (Rs. Cr)" ], "frequency" : "daily", "type" : "Time Series" } ], "securityid" : 54312, "country" : "IN", "detail" : { } })

get(LibBSON.dict(securitydoc), "detail", Dict{Any, Any}())

Thanks!

shivchawla commented 5 years ago
           iter_result = iterate(bsonObject) 
           while !iter_result.done

               k = key(iter_result)
               v = value(iter_result)

               if isa(v, BSONObject)
                   d[k] = dict(v)
               elseif isa(v, BSONArray)
                   d[k] = vector(v)
               else
                   d[k] = v
               end

               iter_result = next!(iter_result)
           end
           d

I couldn't figure out the error but changing the implementation works. But the code looks ugly as it uses a lot of private functions.

shivchawla commented 5 years ago

Figured out the issue. The default iterate method returned incorrect output. Fixed in the code snippet below

Base.iterate(bsonObject::BSONObject) = Base.iterate(bsonObject, BSONIter(bsonObject))
Base.iterate(bsonArray::BSONArray)   = Base.iterate(bsonArray, BSONIter(bsonArray))
Base.iterate(bsonObject::BSONObject, iter::BSONIter) = !iter.done ? ((key(iter), value(iter)), next!(iter)) : nothing
Base.iterate(bsonArray::BSONArray,   iter::BSONIter) = !iter.done ? (value(iter), next!(iter)) : nothing

Does it look right?