iban-tools

Tools / code generators for IBAN validation
Log | Files | Refs

commit 447d0aaeb0f655cac66f73c80a7acdc0e6a6fdff
parent 49f40f1e1fc3e9fd4c61a44160af415692326396
Author: Antoine A <>
Date:   Thu, 13 Mar 2025 16:40:00 +0100

Add kt codegen

Diffstat:
MMakefile | 5++++-
Acodegen_kt.py | 66++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 70 insertions(+), 1 deletion(-)

diff --git a/Makefile b/Makefile @@ -1,4 +1,4 @@ -all: rs ts +all: rs kt ts registry: ./parse_registry.py @@ -6,5 +6,8 @@ registry: rs: registry ./codegen_rs.py +kt: registry + ./codegen_kt.py + ts: registry ./codegen_ts.py \ No newline at end of file diff --git a/codegen_kt.py b/codegen_kt.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python3 + +import json + +with open("registry.json", "r") as json_file: + registry = json.load(json_file) + +kt = """/* + * This file is part of LibEuFin. + * Copyright (C) 2025 Taler Systems S.A. + + * LibEuFin is free software; you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation; either version 3, or + * (at your option) any later version. + + * LibEuFin is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General + * Public License for more details. + + * You should have received a copy of the GNU Affero General Public + * License along with LibEuFin; see the file COPYING. If not, see + * <http://www.gnu.org/licenses/> + */ + +package tech.libeufin.common + +/** IBAN ASCII characters rules */ +enum class IbanC { + /** Digits (0-9) */ + n, + /** Uppercase (A-Z) */ + a, + /** Digits or uppercase (0-9 & A-Z) */ + c, +} + +enum class Country(val ibanLen: Int, val rules: List<Pair<Int, IbanC>>, val bbanRegex: Regex) { +""" + +def fmt_rule(rule): + fmt = "listOf(" + for repetition, char in rule: + fmt += f"Pair({repetition}, IbanC.{char})," + return fmt + ")" + + +for r in registry: + kt += f' {r["code"]}({r["iban_len"]}, {fmt_rule(r["bban_rules"])}, Regex("^{r["bban_regex"]}$")),\n' +kt += """; + val bbanLen get() = ibanLen - 4 +} +val VALID_IBAN = listOf( +""" +for r in registry: + bban = r["bban_example"] + if bban is None: + kt += f' Pair("{r["iban_example"]}", null),\n' + else: + kt += f' Pair("{r["iban_example"]}", "{bban}"),\n' +kt += ");" + +with open("registry.kt", "w") as kt_file: + kt_file.write(kt) +\ No newline at end of file