Metadata-Version: 2.4
Name: json-to-func
Version: 0.1.0
Summary: Convert JSON keys to callable Python methods
Author-email: Mohammad Amin Khara <kharama8798@gmail.com>
License-Expression: MIT
License-File: LICENSE
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.9
Provides-Extra: dev
Requires-Dist: pytest>=7.0; extra == 'dev'
Description-Content-Type: text/markdown

# JSON to Func

A Python library that converts JSON keys to callable methods on a class.

## Installation

```bash
pip install json-to-func
```

## Usage

### Basic Usage

```python
from json_to_func import JsonToFunc, load

# Load from a JSON file
translator = JsonToFunc("messages.json")

# Or use the convenience function
translator = load("messages.json")

# Or load from a dictionary
translator = JsonToFunc({
    "hello": "Hello, World!",
    "greeting": "Hello, %s!"
})
```

### Calling Methods

JSON keys are automatically transformed into valid Python method names:

```python
# Simple key
translator.hello()  # Returns: "Hello, World!"

# With placeholder
translator.greeting("John")  # Returns: "Hello, John!"
```

### Key Transformation Rules

| Original Key | Method Name |
|-------------|-------------|
| `tomans` | `tomans()` |
| `field-required` | `field_required()` |
| `hint:title` | `hint__title()` |
| `hint-bar:text:message` | `hint_bar__text__message()` |

**Rules:**
- Dashes (`-`) become single underscores (`_`)
- Colons (`:`) become double underscores (`__`)
- Other invalid characters become underscores
- Keys starting with numbers get a leading underscore

### Format String Support

The library supports `%s` placeholders in values:

```python
data = {
    "field-required": "%s is required",
    "range-error": "Value must be between %s and %s"
}

translator = JsonToFunc(data)

translator.field_required("Email")  # "Email is required"
translator.range_error("1", "10")   # "Value must be between 1 and 10"
```

### Alternative Access Methods

```python
# Get by original key name
translator.get("field-required", "Email")

# Get by transformed function name
translator.get_by_func_name("field_required", "Email")

# With default value
translator.get("nonexistent", default="fallback")
```

### Utility Methods

```python
# List all original keys
translator.keys()  # ["hello", "field-required", ...]

# List all method names
translator.func_names()  # ["hello", "field_required", ...]

# Check if key exists
"hello" in translator  # True
"field_required" in translator  # True

# Get count
len(translator)  # Number of entries
```

## Example with i18n/Translation Files

```json
{
  "tomans": "تومان",
  "field-required": "%s الزامی است",
  "entity-not-exists": "چنین %s وجود ندارد",
  "hint-bar:title:welcome": "خوش آمدید"
}
```

```python
from json_to_func import JsonToFunc

messages = JsonToFunc("fa.json")

print(messages.tomans())  # تومان
print(messages.field_required("ایمیل"))  # ایمیل الزامی است
print(messages.entity_not_exists("کاربر"))  # چنین کاربر وجود ندارد
print(messages.hint_bar__title__welcome())  # خوش آمدید
```

## License

MIT
