Method: IcAgent::Candid.build_type

Defined in:
lib/ic_agent/candid.rb

.build_type(raw_table, table, entry) ⇒ Object



1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
# File 'lib/ic_agent/candid.rb', line 1396

def self.build_type(raw_table, table, entry)
  ty = entry[0]
  if ty == TypeIds::Vec
    if ty >= raw_table.length
      raise ValueError, 'type index out of range'
    end

    t = get_type(raw_table, table, entry[1])
    if t.nil?
      t = table[t]
    end
    return BaseTypes::vec(t)
  elsif ty == TypeIds::Opt
    if ty >= raw_table.length
      raise ValueError, 'type index out of range'
    end

    t = get_type(raw_table, table, entry[1])
    if t.nil?
      t = table[t]
    end
    return BaseTypes::opt(t)
  elsif ty == TypeIds::Record
    fields = {}
    entry[1].each do |hash, t|
      name = "_#{hash.to_s}_"
      if t >= raw_table.length
        raise ValueError, 'type index out of range'
      end

      temp = get_type(raw_table, table, t)
      fields[name] = temp
    end
    record = BaseTypes::record(fields)
    tup = record.try_as_tuple()
    if tup.is_a?(Array)
      return BaseTypes::tuple(*tup)
    else
      return record
    end
  elsif ty == TypeIds::Variant
    fields = {}
    entry[1].each do |hash, t|
      name = "_#{hash.to_s}_"
      if t >= raw_table.length
        raise ValueError, 'type index out of range'
      end

      temp = get_type(raw_table, table, t)
      fields[name] = temp
    end
    return BaseTypes::variant(fields)
  elsif ty == TypeIds::Func
    return BaseTypes::func([], [], [])
  elsif ty == TypeIds::Service
    return BaseTypes::service({})
  else
    raise ValueError, "Illegal op_code: #{ty}"
  end
end