Skip to content

Commit

Permalink
NAS-132316 / 25.04 / remove @accepts from keyvalue (#14888)
Browse files Browse the repository at this point in the history
* remove @accepts from keyvalue

* initialize options
  • Loading branch information
yocalebo authored Nov 6, 2024
1 parent bf06552 commit c2602d8
Showing 1 changed file with 11 additions and 19 deletions.
30 changes: 11 additions & 19 deletions src/middlewared/middlewared/plugins/keyvalue.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Any

from truenas_api_client import json

from middlewared.schema import Any, Str, accepts, Dict
from middlewared.service import Service
import middlewared.sqlalchemy as sa

Expand All @@ -18,16 +19,14 @@ class KeyValueService(Service):
class Config:
private = True

@accepts(Str('key'))
async def has_key(self, key):
async def has_key(self, key: str):
try:
await self.get(key)
return True
except KeyError:
return False

@accepts(Str('key'), Any('default', null=True, default=None))
async def get(self, key, default):
async def get(self, key: str, default: Any | None = None):
try:
return json.loads(
(await self.middleware.call(
Expand All @@ -38,28 +37,21 @@ async def get(self, key, default):

raise KeyError(key)

@accepts(
Str('key'),
Any('value'),
Dict('options', additional_attrs=True),
)
async def set(self, key, value, options):
async def set(self, key: str, value: Any, options: dict | None = None):
opts = options if options is not None else dict()
try:
row = await self.middleware.call("datastore.query", "system.keyvalue", [["key", "=", key]], {"get": True})
except IndexError:
await self.middleware.call(
"datastore.insert", "system.keyvalue", {"key": key, "value": json.dumps(value)}, options
"datastore.insert", "system.keyvalue", {"key": key, "value": json.dumps(value)}, opts
)
else:
await self.middleware.call(
"datastore.update", "system.keyvalue", row["id"], {"value": json.dumps(value)}, options
"datastore.update", "system.keyvalue", row["id"], {"value": json.dumps(value)}, opts
)

return value

@accepts(
Str('key'),
Dict('options', additional_attrs=True),
)
async def delete(self, key, options):
await self.middleware.call("datastore.delete", "system.keyvalue", [["key", "=", key]], options)
async def delete(self, key: str, options: dict | None = None):
opts = options if options is not None else dict()
await self.middleware.call("datastore.delete", "system.keyvalue", [["key", "=", key]], opts)

0 comments on commit c2602d8

Please sign in to comment.