Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

JS: Attributes

Attributes

A gclass declares its attributes in a table. Each attribute has a type, a name, a set of flags, a default value and a description.

Source code: src/gobj.js

import { SDATA, SDATA_END, data_type_t, sdata_flag_t } from "@yuneta/gobj-js";

const attrs_table = [
    SDATA(data_type_t.DTP_STRING,  "url",       sdata_flag_t.SDF_RD,      "", "Server URL"),
    SDATA(data_type_t.DTP_INTEGER, "timeout",   sdata_flag_t.SDF_RD,       0, "Timeout ms"),
    SDATA(data_type_t.DTP_BOOLEAN, "connected", sdata_flag_t.SDF_RD,   false, "Connection state"),
    SDATA(data_type_t.DTP_STRING,  "saved_key", sdata_flag_t.SDF_PERSIST, "", "Persisted value"),
    SDATA_END()
];

Declare

SDATA(type, name, flag, default_value, description)

Declares one attribute. The table finishes with SDATA_END().

data_type_t

The types of an attribute.

TypeDescription
DTP_STRINGA string.
DTP_BOOLEANA boolean.
DTP_INTEGERAn integer.
DTP_REALA real number.
DTP_LISTA list.
DTP_DICTAn object.
DTP_JSONAny JSON value.
DTP_POINTERA value that is not JSON, such as a gobj or a DOM node.

sdata_flag_t

The flags of an attribute. Combine them with the bit-or operator.

FlagDescription
SDF_NOTACCESSNot reachable from outside.
SDF_RDA reader can read it.
SDF_WRA writer can write it.
SDF_REQUIREDThe creation must give a value.
SDF_PERSISTThe framework saves it and loads it again.
SDF_VOLATILgobj_reset_volatil_attrs() clears it.
SDF_RESOURCEA resource.
SDF_PKEYThe primary key of a record.
SDF_DEPRECATEDKept for the old callers.
SDF_WILD_CMDA command that accepts a free form.
SDF_STATSA statistic.
SDF_FKEYA foreign key of a record.
SDF_RSTATSA statistic that resets when a reader reads it.
SDF_PSTATSA statistic that persists.
SDF_AUTHZ_RThe read needs an authorization.
SDF_AUTHZ_WThe write needs an authorization.
SDF_AUTHZ_XThe execution needs an authorization.
SDF_AUTHZ_PThe subscription needs an authorization.
SDF_AUTHZ_SThe statistics need an authorization.
SDF_AUTHZ_RSThe reset of the statistics needs an authorization.

Read

gobj_has_attr(gobj, name)

Tells if a gobj has an attribute with that name.

gobj_read_attr(gobj, name, src)

Reads an attribute of any type. src names the gobj that asks, for the authorization and for the log.

gobj_read_attrs(gobj, include_flag, src)

Reads every attribute whose flags match include_flag, as one object.

gobj_read_bool_attr(gobj, name)

Reads a boolean attribute.

gobj_read_integer_attr(gobj, name)

Reads an integer attribute.

gobj_read_str_attr(gobj, name)

Reads a string attribute.

gobj_read_pointer_attr(gobj, name)

Reads an attribute that holds a value which is not JSON. The subscriber attribute of the subscription model uses it.

gobj_hsdata(gobj)

Gives the whole store of the attributes of a gobj. It is an internal handle, and the functions above are the way to read an attribute.


Write

gobj_write_attr(gobj, path, value, src)

Writes an attribute of any type.

gobj_write_attrs(gobj, kw, include_flag, src)

Writes every attribute of kw whose flags match include_flag.

gobj_write_bool_attr(gobj, name, value)

Writes a boolean attribute.

gobj_write_integer_attr(gobj, name, value)

Writes an integer attribute.

gobj_write_str_attr(gobj, name, value)

Writes a string attribute.

gobj_reset_volatil_attrs(gobj)

Writes the default value in every attribute that carries SDF_VOLATIL.


An attribute with SDF_PERSIST goes to the store through the callbacks that gobj_start_up() receives. See Persistence.