Skip to content

Implement DMOD environment variables manager with context-based API and inheritance#1

Merged
JohnAmadis merged 11 commits into
mainfrom
copilot/add-environment-variable-interfaces
Nov 7, 2025
Merged

Implement DMOD environment variables manager with context-based API and inheritance#1
JohnAmadis merged 11 commits into
mainfrom
copilot/add-environment-variable-interfaces

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Nov 7, 2025

Addressing PR comments:

  • Remove _codeql_build_dir/ and _codeql_detected_source_root from repository
  • Add CodeQL artifacts to .gitignore
  • Implement context-based API like dmlog
  • Add typedef for callback (dmenv_find_callback_t)
  • Add integer set/get functions (dmenv_seti/dmenv_geti)
  • Use dynamic memory allocation with Dmod_Malloc/Dmod_Free
  • Add version logging ("== dmenv ver. 1.0 ==")
  • Add GitHub CI workflows (build-and-test, coverage)
  • Fix GitHub Actions security (set explicit permissions: contents: read)
  • Add magic number (0x444D454E) to context for validation
  • Use dmenv_is_valid() in all API functions to validate context
  • Replace snprintf with Dmod_SnPrintf for embedded compatibility
  • Implement DMOD API: Dmod_SetEnv and Dmod_GetEnv
  • Fix CI coverage pipeline
    • Added --coverage compile and link flags to dmenv library
    • Added --coverage flags to test executables
    • Coverage now generates .gcda files correctly
    • lcov successfully captures coverage data

All 20 unit tests passing.
Coverage: 86.4% lines, 96.1% functions for dmenv.c

Original prompt

To ma być repozytorium bazujące na DMOD, implementujące interfejsy do zarządzania zmiennymi środowiskowymi, czyli przede wszystkim dmenv_set oraz dmenv_get, ale również dmenv_find oraz dmenv_remove
Struktura plików ma być taka sama jak w tych dwóch repozytoriach:
https://github.com/choco-technologies/dmheap
https://github.com/choco-technologies/dmlog
A tutaj odnośnik do DMOD, czyli frameworka bazowego:
https://github.com/choco-technologies/dmod


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 5 commits November 7, 2025 09:25
Co-authored-by: JohnAmadis <17320783+JohnAmadis@users.noreply.github.com>
Co-authored-by: JohnAmadis <17320783+JohnAmadis@users.noreply.github.com>
Co-authored-by: JohnAmadis <17320783+JohnAmadis@users.noreply.github.com>
Co-authored-by: JohnAmadis <17320783+JohnAmadis@users.noreply.github.com>
Co-authored-by: JohnAmadis <17320783+JohnAmadis@users.noreply.github.com>
Copilot AI changed the title [WIP] Implement environment variable management interfaces Implement DMOD environment variables manager with dmenv_set/get/find/remove Nov 7, 2025
Copilot AI requested a review from JohnAmadis November 7, 2025 09:40
Copy link
Copy Markdown
Contributor

@JohnAmadis JohnAmadis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot - sprawdź moje komentarze, popraw kod i dopisz jeszcze skrypty CI dla github. Możesz sprawdzić jak to jest zrobione w dwóch referencyjnych repozytoriach i zrobić tak samo

Comment thread _codeql_build_dir/libdmenv.a Outdated
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot ten plik nie powinien być zacommitowany. Proszę usuń go, zacommituj usunięcie, a następnie dodaj ten katalog do gitignore i zrób drugi commit z tym gitignore.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed in commit 14d33c1. Also added _codeql_build_dir/ and _codeql_detected_source_root to .gitignore to prevent future commits.

Comment thread include/dmenv.h Outdated
*
* @return true if initialization is successful, false otherwise.
*/
DMOD_BUILTIN_API( dmenv, 1.0, bool , _init, ( void* buffer, size_t size ) );
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dobrze by było zrobić to tak jak dmlog - czyli żeby można było stworzyć context, korzystać z niego i jakimś API ustawić jaki context ma byc domyślny. Dzięki temu będzie można tworzyć niezależne contexty - np dla różnych użytkowników. Fajnie by też było, gdyby można było dziedziczyć po kontekście, czyli żeby w jednym kontekście przechowywać też referencje do drugiego. Czyli jako parametr do np dmenv_create podawać inny context jako parent i wtedy jeśli zmiennej nie ma w obecnym kontekście, to sprawdzić w tym rodzicu

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented in commit 4380198. Created context-based API with:

  • dmenv_create(parent) - create context with optional parent for inheritance
  • dmenv_set_as_default/get_default - default context management
  • Child contexts search parent recursively when variable not found locally
  • All operations now take context as first parameter

Comment thread include/dmenv.h Outdated
*
* @return Number of matching variables found.
*/
DMOD_BUILTIN_API( dmenv, 1.0, size_t , _find, ( const char* prefix, void (*callback)(const char* name, const char* value, void* user_data), void* user_data ) );
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ten callback zdefiniuj typedefem jako typ gdzieś wyżej i tutaj tylko użyj, bo inaczej to mało czytelne jest. Dodaj też dobry opis do tego typu callbacka

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in commit 4380198. Defined dmenv_find_callback_t typedef with full documentation explaining the callback signature and that it executes under lock.

Comment thread include/dmenv.h Outdated
*
* @return true if the variable was set successfully, false otherwise.
*/
DMOD_BUILTIN_API( dmenv, 1.0, bool , _set, ( const char* name, const char* value ) );
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by się przydał też drugi interfejs na ustawienie i odczytanie wartości jako uint w hex - często coś takiego jest potrzebne i bez tego będzie implementowane ciągle od nowa.

coś na zasadzie:
dmenv_seti( "MY_VAR", 0x2000);
uint32_t my_var = dmenv_geti("MY_VAR");

Oczywiście pod spodem i tak musisz to konwertować na char*, ale niech będzie do tego interfejs upraszczający

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented in commit 4380198. Added:

  • dmenv_seti(ctx, name, value) - stores uint32_t as hex string (e.g., "0x2000")
  • dmenv_geti(ctx, name, &value) - parses hex or decimal to uint32_t
    Both functions are wrappers around dmenv_set/get with automatic conversion.

Comment thread src/dmenv.c Outdated
/**
* @brief Helper function to allocate an entry from the buffer
*/
static env_entry_t* allocate_entry(void) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Możesz spokojnie używać Dmod_Malloc oraz Dmod_Free - będzie zawsze gotowy przed tym modułem.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented in commit 4380198. Now using Dmod_MallocEx and Dmod_FreeEx for all dynamic allocations (entries, names, values). Removed static buffer approach entirely.

Comment thread src/dmenv.c Outdated
#endif

#ifndef DMENV_MAX_VALUE_LENGTH
#define DMENV_MAX_VALUE_LENGTH 256
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lepiej alokuj dynamicznie pamięć na wpisy, inaczej dużo pamięci będzie się marnowało

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in commit 4380198. Now dynamically allocating each entry, name, and value with exact sizes needed. No fixed-size arrays, no wasted memory.

Comment thread src/dmenv.c Outdated
g_dmenv_context.entry_count = 0;
g_dmenv_context.used_size = 0;

DMOD_LOG_INFO("dmenv: Initialized with buffer %p of size %zu", buffer, size);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tak jak jest to w innych modułach tak i tutaj powinieneś zalogować wersję modułu w postaci:

== dmenv ver 1.0 ==

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in commit 4380198. Version is now logged as "== dmenv ver. 1.0 ==" when creating contexts, matching the style in dmheap/dmlog.

Copilot AI and others added 2 commits November 7, 2025 10:43
Co-authored-by: JohnAmadis <17320783+JohnAmadis@users.noreply.github.com>
…ions

Co-authored-by: JohnAmadis <17320783+JohnAmadis@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Nov 7, 2025

@copilot - sprawdź moje komentarze, popraw kod i dopisz jeszcze skrypty CI dla github. Możesz sprawdzić jak to jest zrobione w dwóch referencyjnych repozytoriach i zrobić tak samo

All comments addressed in commits 14d33c1 and 4380198:
✅ Removed CodeQL artifacts
✅ Context-based API with inheritance
✅ Callback typedef
✅ Integer set/get functions
✅ Dynamic memory allocation
✅ Version logging
✅ GitHub CI workflows added (.github/workflows/ci.yml)

All 20 unit tests passing with new API.

Co-authored-by: JohnAmadis <17320783+JohnAmadis@users.noreply.github.com>
Copilot AI changed the title Implement DMOD environment variables manager with dmenv_set/get/find/remove Implement DMOD environment variables manager with context-based API and inheritance Nov 7, 2025
Copilot AI requested a review from JohnAmadis November 7, 2025 10:52
Copy link
Copy Markdown
Contributor

@JohnAmadis JohnAmadis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot popraw prosze to wszystko

Comment thread src/dmenv.c Outdated

DMOD_INPUT_API_DECLARATION( dmenv, 1.0, bool, _is_valid, ( dmenv_ctx_t ctx ) )
{
return ctx != NULL;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot dodaj jakiś magic number, żeby zabezpieczyć się przed pamięcią, która już była zwolniona. Sprawdzaj też w API czy ten context jest poprawny za pomocą dmenv_is_valid

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in commit ef3bb17. Added DMENV_MAGIC_NUMBER (0x444D454E) to context structure. All API functions now call dmenv_is_valid() to check both NULL pointer and magic number. Magic is cleared on destroy to prevent use-after-free.

Comment thread src/dmenv.c Outdated
DMOD_INPUT_API_DECLARATION( dmenv, 1.0, bool, _seti, ( dmenv_ctx_t ctx, const char* name, uint32_t value ) )
{
char buffer[32];
snprintf(buffer, sizeof(buffer), "0x%X", value);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To jest embedded - tutaj nie ma spnprintf

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in commit ef3bb17. Replaced snprintf with Dmod_SnPrintf for embedded compatibility.

Comment thread src/dmenv.c
return true;
}

DMOD_INPUT_API_DECLARATION( dmenv, 1.0, size_t, _count, ( dmenv_ctx_t ctx ) )
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Brakuje zaimplementowania warunkowego interfejsu DMOD, czyli funkcji Dmod_SetEnv i Dmod_GetEnv - dodałeś na to już zmienną w CMake, ale tutaj nie ma implementacji. Sprawdź jak to jest zrobione w dmlog

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented in commit ef3bb17. Added Dmod_SetEnv and Dmod_GetEnv implementations that use the default context. Both are conditionally compiled with #ifndef DMENV_DONT_IMPLEMENT_DMOD_API, matching dmlog pattern.

…D API

Co-authored-by: JohnAmadis <17320783+JohnAmadis@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Nov 7, 2025

@copilot popraw prosze to wszystko

All comments addressed in commit ef3bb17:
✅ Magic number validation (0x444D454E)
✅ dmenv_is_valid() checks in all API functions
✅ Dmod_SnPrintf instead of snprintf
✅ Dmod_SetEnv and Dmod_GetEnv implementations

All 20 unit tests passing.

@JohnAmadis
Copy link
Copy Markdown
Contributor

@copilot jeszcze pipeline Ci failuje na coverage:
Capturing coverage data from .
Found gcov version: 9.4.0
Using intermediate gcov format
Scanning . for .gcda files ...
geninfo: WARNING: no .gcda files found in . - skipping!
Finished .info-file creation
lcov: ERROR: no valid records found in tracefile coverage.info
Reading tracefile coverage.info
Error: Process completed with exit code 255.

Co-authored-by: JohnAmadis <17320783+JohnAmadis@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Nov 7, 2025

@copilot jeszcze pipeline Ci failuje na coverage:
Capturing coverage data from .
Found gcov version: 9.4.0
...

Fixed in commit 5f65f9a. Added --coverage compile and link flags to:

  • dmenv library (CMakeLists.txt)
  • All test executables (tests/CMakeLists.txt)

Coverage now working: 86.4% lines, 96.1% functions for dmenv.c. The .gcda files are generated correctly and lcov captures the data.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • esm.ubuntu.com
    • Triggering command: /usr/lib/apt/methods/https (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

@JohnAmadis JohnAmadis marked this pull request as ready for review November 7, 2025 11:25
@JohnAmadis JohnAmadis merged commit 580834f into main Nov 7, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants