]> git.ipfire.org Git - people/ms/libloc.git/commitdiff
lua: database: Implementing fetching AS objects
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 22 Feb 2024 15:39:25 +0000 (15:39 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 22 Feb 2024 15:39:25 +0000 (15:39 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/lua/database.c
tests/lua/main.lua

index efed6aaf9ce9d08b70d70cb4c0cc7f13621050bf..65e7aa6e3f0257047ac74684bd0249c95ce19a1d 100644 (file)
@@ -23,6 +23,7 @@
 #include <libloc/database.h>
 
 #include "location.h"
+#include "as.h"
 #include "database.h"
 #include "network.h"
 
@@ -83,6 +84,29 @@ static int Database_gc(lua_State* L) {
        return 0;
 }
 
+static int Database_get_as(lua_State* L) {
+       struct loc_as* as = NULL;
+       int r;
+
+       Database* self = luaL_checkdatabase(L, 1);
+
+       // Fetch number
+       uint32_t asn = luaL_checknumber(L, 2);
+
+       // Fetch the AS
+       r = loc_database_get_as(self->db, &as, asn);
+       if (r) {
+               lua_pushnil(L);
+               return 1;
+       }
+
+       // Create a new AS object
+       r = create_as(L, as);
+       loc_as_unref(as);
+
+       return r;
+}
+
 static int Database_lookup(lua_State* L) {
        struct loc_network* network = NULL;
        int r;
@@ -105,6 +129,7 @@ static int Database_lookup(lua_State* L) {
 }
 
 static const struct luaL_Reg database_functions[] = {
+       { "get_as", Database_get_as },
        { "open", Database_open },
        { "lookup", Database_lookup },
        { "__gc", Database_gc },
index ff904d46d1f1f9fcf92508a8454ec7581c1cad96..2076202115e9854094aeb9a08572e32ca69aadb1 100755 (executable)
@@ -77,6 +77,24 @@ function test_as()
        as = nil
 end
 
+function test_fetch_as()
+       location = require("location")
+
+       -- Open the database
+       db = location.Database.open(ENV_TEST_DATABASE)
+
+       -- Fetch an AS
+       as = db:get_as(0)
+
+       -- This should not exist
+       luaunit.assertNil(as)
+
+       -- Fetch something that exists
+       as = db:get_as(204867)
+       luaunit.assertEquals(as:get_number(), 204867)
+       luaunit.assertEquals(as:get_name(), "Lightning Wire Labs GmbH")
+end
+
 function test_country()
        location = require("location")