A package is nothing but a directory inside your catalog containing one or more CoginitiScript files or other CoginitiScript packages.
Every CoginitiScript file belong to the package which is determined by name of its parent folder. Exception is a files located in the catalog root. They belong to the package named “main”.
Public / Private blocks
Developer is able to choose whether he want a block to be called from other packages or keep them private so it could be called only within given package. Other packages can import and reuse only public blocks from the package. This gives users ability to incapsulate certain logic inside the package and export only public API.
CoginitiScript follows go-like approach for exporting identifiers. An identifier is public if the first character of the identifier’s name is a upper case letter. All other identifiers are private.
In the following example foo
block is private, while Bar
block is public:
#+src sql foo() #+begin ... #+end #+src sql Bar() #+begin ... #+end
Importing packages
To import a package, use #+import
directive.
#+import "sales/fact_sales" #+import "sales/customer/dim_customer"
By convention - the package name is the same as the last element of the import path. In above example, “fact_sales” and “dim_customer” are package names.
However, you can always import package under the different alias:
#+import "sales/fact_sales" as sales
When you import a package, you can only reference its public blocks by using “package_name.BlockName” or “alias_name.BlockName” syntax.
#+import "sales/customer" #+import "sales/fact_sales" as sales
SELECT * FROM {{ customer.CustomerDimension() }}; SELECT * FROM {{ sales.SalesInteraction() }};
Please take into account that last component in the path is used as an identifier of the package, so from syntax perspective it should represent valid identifier. Since catalog naming rules are less strict, folder names there could be named as non-valid identifiers. For example, they could contains spaces in the name. In this case user is forced to specify an alias to the imported package.
Example:
#+import "Users/username/Sales Interaction" as sales SELECT * FROM {{ sales.BlockName() }};
Default blocks are unnamed and private.