How come I cannot access the public enum of another class as a static field? - google-closure-library

I have an enum in a file:
goog.provide('animals.Fish');
animals.Fish = function(obj) {
this.name_ = obj[animals.Fish.Properties.NAME];
this.awesomenessLevel_ = obj[animals.Fish.Properties.AWESOMENESS];
}
/**
* Public list of properties.
* #enum {string}
*/
animals.Fish.Properties = {
NAME: 'name',
AWESOMENESS: 'awesomenessLevel',
}
How come I can't access this enum as a static field of Fish in another class like this?
goog.require('animals.Fish');
...
var tuna = new animals.Fish(
animals.Fish.NAME: 'tuna',
animals.Fish.AWESOMENESS: '100'
)
...

Closure enumeration types are based on the concept of Enum Types from languages such as Java and C++. In Java, enum types are defined as follows:
An enum type is a type whose fields consist of a fixed set of constants. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.
In your example above, animals.Fish.Properties should probably be represented as a record type, since the assigned values are not constants. In the example below, animals.Fish.Properties has been renamed animals.Properties so that it could be applied to any type of animal (not just fish).
fish.js
goog.provide('animals.Fish');
goog.provide('animals.Properties');
/** #typedef {{name: string, awesomeness: string}} */
animals.Properties;
/**
* #param {animals.Properties} properties Animal properties.
* #constructor
*/
animals.Fish = function(properties) {
/** #type {string} */
this.name_ = properties.name;
/** #type {string} */
this.awesomenessLevel_ = properties.awesomeness;
};
/**
* #return {string} The name of the fish.
*/
animals.Fish.prototype.getName = function() {
return this.name_;
};
animals_app.js
goog.provide('animals.app');
goog.require('animals.Fish');
animals.app.tuna = new animals.Fish({name: 'tuna', awesomeness: '100'});
alert(animals.app.tuna.getName()); // alerts 'tuna'
On a side note, in the original example there should not be a comma after: AWESOMENESS: 'awesomenessLevel' in the definition of animals.Fish.Properties. In addition, in your second file you need to use the fully qualified enum name. So instead of animals.Fish.NAME it would be animals.Fish.Properties.NAME.

Related

Hapi Fhir DomainResource, What URL do you use?

http://hapifhir.io/doc_custom_structures.html
this article discusses a DomainResource.
There are situations however when you might want to create an entirely
custom resource type. This feature should be used only if there is no
other option, since it means you are creating a resource type that
will not be interoperable with other FHIR implementations.
I've implemented the code verbatum. (I show the classes below (with no "guts" just for brevity) (full code at the url))
/**
* This is an example of a custom resource that also uses a custom
* datatype.
*
* Note that we are extensing DomainResource for an STU3
* resource. For DSTU2 it would be BaseResource.
*/
#ResourceDef(name = "CustomResource", profile = "http://hl7.org/fhir/profiles/custom-resource")
public class CustomResource extends DomainResource {
}
and
/**
* This is an example of a custom datatype.
*
* This is an STU3 example so it extends Type and implements ICompositeType. For
* DSTU2 it would extend BaseIdentifiableElement and implement ICompositeDatatype.
*/
#DatatypeDef(name="CustomDatatype")
public class CustomDatatype extends Type implements ICompositeType {
}
And I've "registered it" in my code base:
if (null != this.fhirContext)
{
this.fhirContext.registerCustomType(CustomResource.class);
this.fhirContext.registerCustomType(CustomDatatype.class);
}
(~trying to follow the instructions from the URL above)
// Create a context. Note that we declare the custom types we'll be using
// on the context before actually using them
FhirContext ctx = FhirContext.forDstu3();
ctx.registerCustomType(CustomResource.class);
ctx.registerCustomType(CustomDatatype.class);
// Now let's create an instance of our custom resource type
// and populate it with some data
CustomResource res = new CustomResource();
// Add some values, including our custom datatype
DateType value0 = new DateType("2015-01-01");
res.getTelevision().add(value0);
CustomDatatype value1 = new CustomDatatype();
value1.setDate(new DateTimeType(new Date()));
value1.setKittens(new StringType("FOO"));
res.getTelevision().add(value1);
res.setDogs(new StringType("Some Dogs"));
// Now let's serialize our instance
String output = ctx.newXmlParser().setPrettyPrint(true).encodeResourceToString(res);
System.out.println(output);
But that looks like a console-app usage of two objects...not how to wire it into the fhir-server.
I've been trying for 3 hours now to figure out what URL to use.
some things I've tried:
http://127.0.0.1:8080/fhir/CustomResource
http://127.0.0.1:8080/fhir/profiles/custom-resource
http://127.0.0.1:8080/fhir/custom-resource
to no avail...
What is the URL?
And how do I populate the values for it?
Ok.
So the CustomResource still needs its own IResourceProvider. (thanks to daniels in the comments of the original question)
Here is a basic working example.
You'll do everything I listed in the original question AND you'll make and register an IResourceProvider for the new customresource.
new IResourceProvider
package mystuff.resourceproviders;
import org.hl7.fhir.dstu3.model.DateType;
import org.hl7.fhir.dstu3.model.IdType;
import ca.uhn.fhir.rest.annotation.IdParam;
import ca.uhn.fhir.rest.annotation.Read;
import ca.uhn.fhir.rest.server.IResourceProvider;
import mystuff.CustomDatatype;
import mystuff.CustomResource;
import org.hl7.fhir.dstu3.model.DateTimeType;
import org.hl7.fhir.dstu3.model.StringType;
import org.hl7.fhir.instance.model.api.IBaseResource;
import java.util.Date;
public class CustomResourceProvider implements IResourceProvider {
#Override
public Class<? extends IBaseResource> getResourceType() {
return CustomResource.class;
}
/* the IdType (datatype) will be different based on STU2 or STU3. STU3 version below */
#Read()
public CustomResource getResourceById(#IdParam IdType theId) {
// Now let's create an instance of our custom resource type
// and populate it with some data
CustomResource res = new CustomResource();
res.setId(theId);
// Add some values, including our custom datatype
DateType value0 = new DateType("2015-01-01");
res.getTelevision().add(value0);
CustomDatatype value1 = new CustomDatatype();
value1.setDate(new DateTimeType(new Date()));
value1.setKittens(new StringType("FOO"));
res.getTelevision().add(value1);
res.setDogs(new StringType("Some Dogs"));
return res;
}
}
then you'll register this (as documented here):
http://hapifhir.io/doc_rest_server.html#_toc_create_a_server
instead of this:
#Override
protected void initialize() throws ServletException {
/*
* The servlet defines any number of resource providers, and
* configures itself to use them by calling
* setResourceProviders()
*/
List<IResourceProvider> resourceProviders = new ArrayList<IResourceProvider>();
resourceProviders.add(new RestfulPatientResourceProvider());
resourceProviders.add(new RestfulObservationResourceProvider());
setResourceProviders(resourceProviders);
}
you'll have something like this
#Override
protected void initialize() throws ServletException {
/*
* The servlet defines any number of resource providers, and
* configures itself to use them by calling
* setResourceProviders()
*/
List<IResourceProvider> resourceProviders = new ArrayList<IResourceProvider>();
resourceProviders.add(new CustomResourceProvider());
setResourceProviders(resourceProviders);
}
URL for testing this (most probable local development url that is)
http://127.0.0.1:8080/fhir/CustomResource/12345
and you'll get back this JSON response.
{
"resourceType": "CustomResource",
"id": "12345",
"meta": {
"profile": [
"http://hl7.org/fhir/profiles/custom-resource"
]
},
"televisionDate": [
"2015-01-01"
],
"televisionCustomDatatype": [
{
"date": "2019-01-14T11:49:44-05:00",
"kittens": "FOO"
}
],
"dogs": "Some Dogs"
}

Symfony custom filename for uploaded image files

I'm aware of VichUpload and namers, but I have to face two different file uploads, and I need special naming conventions that I'm unable to get with the current VichUpload documentation.
First file upload is for an entity called "School", which manages every single school, and its logo. So, taking as web root '../web/files/schools/', then I'd take the school_id and then the 'logo' folder with the uploaded file name, so it could be '../web/files/schools/000001/logo.png'.
Then, the second entity is 'students' to store the photo, with a school_id foreign key from School entity. So, the resulting file name would depend on the school id, and the student id, being the root for student '../web/files/schools/<school_id>/students/<student_id>.[jpg|png]', having student_id a six zero padding on the left.
This is the section in config.yml about this (updated info):
school_image:
upload_destination: "../web/files/images/schools"
uri_prefix: "/files/images/schools"
directory_namer:
service: vich_uploader.directory_namer_subdir
options: { property: 'schoolDirectory', chars_per_dir: 6, dirs: 1 }
namer:
service: vich_uploader.namer_property
options: { property: 'idSlug' }
inject_on_load: true
delete_on_update: true
delete_on_remove: true
student_image:
upload_destination: "../web/files/images/schools"
uri_prefix: "/files/images/schools"
directory_namer:
service: vich_uploader.directory_namer_subdir
options: { property: 'userDirectory', chars_per_dir: 6, dirs: 1 }
namer:
service: vich_uploader.namer_property
options: { property: 'userImage'}
inject_on_load: true
delete_on_update: true
delete_on_remove: true
in school entity (might work with a dirty workaround):
/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
*
* #Vich\UploadableField(mapping="school_image", fileNameProperty="imageName")
*
* #Assert\Image()
*
* #var File
*/
private $imageFile;
public function getIdSlug()
{
$slug = sprintf("%06d", $this->getId());
return $slug;
}
public function getSchoolDirectory()
{
$slug = sprintf("%06d", $this->getId());
return $slug;
}
in student entity (not working, as explained below):
/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
*
* #Vich\UploadableField(mapping="student_image", fileNameProperty="imageName")
*
* #Assert\Image()
*
* #var File
*/
private $imageFile;
public function getUserDirectory()
{
$schoolDir = $this->getSchool()->getSchoolDirectory();
$dir = $schoolDir.'/students/'.sprintf("%06d", $this->getId());
return $dir;
}
public function getUserImage()
{
return $this->getUsername() . $this->getImageFile()->getExtension();
}
This setup with both "namer" and "directory_namer" seems to ignore the paths in directory_namer and use a path "<namer>/<namer>.ext" instead of "<directory_namer>/<namer>.ext". If I change the getUserImage() function and prepend the result of getUserDirectory() (i.e., the db stores "<directory_namer>/<namer>.ext" instead of just "<namer>.ext"), the "<directory_namer>" path is ignored and just "<namer>.ext" is created.
Since upload_prefix and uri_destination don't seem to handle variable data, how can I setup a namer or whatever to get this path for both cases?
BTW, I'm using Symfony 3.1 and composer hasn't allowed to update vich beyond "1.7.x-dev", according to bundled composer.json in vendor folder. If you find that it should work with this setup and the possible solution is to upgrade, I would thank to be pointed to the specific files which fix the problem, so I can manually paste whatever is wrong.
SOLUTION:
The problem was that, due to the "old" version, there was a missing directory_namer class, PropertyDirectoryNamer, with vich_uploader.namer_directory_property servicename, instead of vich_uploader.directory_namer_subdir, which was a wrong class for this purpose. I copied this class and registered it in the namer.xml file, and then I got the expected results. Now I'm going to try to mark this as solved.
You need to create custom directory namer class that implements
Vich\UploaderBundle\Naming\DirectoryNamerInterface
For example:
namespace App\Services;
use Vich\UploaderBundle\Mapping\PropertyMapping;
class SchoolDirectoryNamer implements DirectoryNamerInterface
{
public function directoryName($object, PropertyMapping $mapping): string
{
// do what you want with $object and $mapping
$dir = '';
return $dir;
}
}
Then, make it as a service.
services:
# ...
app.directory_namer.school:
class: App\Services\SchoolDirectoryNamer
Last step is config vich_uploader
vich_uploader:
# ...
mappings:
school:
upload_destination: school_image
directory_namer: app.directory_namer.school
student:
upload_destination: student_image
directory_namer: app.directory_namer.student
source : VichUploaderBundle - How To Create a Custom Directory Namer

Cannot create async method which return ElementArrayFinder for E2E tests

I have a class where I want to make a async method which will return ElementArrayFinder. I need exactly this type, not ElementFinder[] because my waiters based on it and I can wait element which isn't present now in DOM. Below you can find a simple example of the structure. At row 'return this.collection;' i have an error:
[ts]
Type 'any[]' is not assignable to type 'ElementArrayFinder'.
Property 'browser_' is missing in type 'any[]'.
I tried to cast result in a different way and tried to use Promise.resolve, but no success. Could somebody help me with this case?
export class Test {
private grid1: ElementFinder;
private grid2: ElementFinder;
get collection1(): ElementArrayFinder {
return this.grid1
.element(by.css('tbody'))
.all(by.tagName('tr'));
}
get collection2(): ElementArrayFinder {
return this.grid2
.element(by.css('tbody'))
.all(by.tagName('tr'));
}
public async getCollection(): Promise<ElementArrayFinder> {
if (await this.collection.count() === 0) {
return this.collection1;
}
return this.collection2;
}
}
Bug report
Node Version: v8.2.1
Protractor Version: 5.2.0
I found the answer in file element.d.ts file.
/**
* Retrieve the elements represented by the ElementArrayFinder. The input
* function is passed to the resulting promise, which resolves to an
* array of ElementFinders.
*
* #alias element.all(locator).then(thenFunction)
* #view
* <ul class="items">
* <li>First</li>
* <li>Second</li>
* <li>Third</li>
* </ul>
*
* #example
* element.all(by.css('.items li')).then(function(arr) {
* expect(arr.length).toEqual(3);
* });
*
* // Or using the shortcut $$() notation instead of element.all(by.css()):
*
* $$('.items li').then(function(arr) {
* expect(arr.length).toEqual(3);
* });
*
* #param {function(Array.<ElementFinder>)} fn
* #param {function(Error)} errorFn
*
* #returns {!webdriver.promise.Promise} A promise which will resolve to
* an array of ElementFinders represented by the ElementArrayFinder.
*/
then<T>(fn?: (value: ElementFinder[] | any[]) => T | wdpromise.IThenable<T>, errorFn?: (error: any) => any): wdpromise.Promise<T>;
Based on this documentation ElementArrayFinder type is resolved by .then to the ElementFinder[]. That's why I cannot create method which return ElementArrayFinder type in promise, but can create method which simply return ElementArrayFinder type.

thrift constant structure using enum

I've got following enum and want to create a constant map using elements of this enum:
enum UploadFileType {
POPULATION,
PROBABILITY,
REACH,
CMOBILE,
CMOBAPP
}
/**
* Defines which files are uploadable in which country
*/
const map<string,list<UploadFileType>> uploadable_files = {
'hu': [POPULATION, PROBABILITY, REACH, CMOBILE, CMOBAPP],
'sk': [POPULATION, PROBABILITY, REACH]
}
I'm getting following error here:
[FAILURE:/home/abc/internal.thrift:29] error: identifier POPULATION is unqualified!
[FAILURE:/home/abc/internal.thrift:29] error: identifier POPULATION is unqualified!
and I don't know how this should look properly.
Enums use dot notation:
const map<string,list<UploadFileType>> uploadable_files = {
'hu': [UploadFileType.POPULATION, UploadFileType.PROBABILITY, UploadFileType.REACH, UploadFileType.CMOBILE, UploadFileType.CMOBAPP],
'sk': [UploadFileType.POPULATION, UploadFileType.PROBABILITY, UploadFileType.REACH]
}
enum UploadFileType {
POPULATION,
PROBABILITY,
REACH,
CMOBILE,
CMOBAPP
}
/**
* Defines which files are uploadable in which country
*/
const map<string,list<UploadFileType>> uploadable_files = {
'hu': [UploadFileType.POPULATION, UploadFileType.PROBABILITY, UploadFileType.REACH, UploadFileType.CMOBILE, UploadFileType.CMOBAPP],
'sk': [UploadFileType.POPULATION, UploadFileType.PROBABILITY, UploadFileType.REACH]
}

Is there a function like _compile_select or get_compiled_select()?

Looks like _compile_select is deprecated and get_compiled_select is not added to 2.1.0. Are there any other functions like those two? And also I am curious. Is there any particular reason to not adding get_compiled_select() to Active Record and removing _compile_select?
I've added get_compiled_select() to DB_active_rec.php and it seems to work without problem, but i wouldn't remove _compile_select() since it's used in many other methods.
The pull request for adding this method is here, with some other useful methods like:
get_compiled_select()
get_compiled_insert()
get_compiled_update()
get_compiled_delete()
https://github.com/EllisLab/CodeIgniter/pull/307
if you want just the method, it's just this:
/**
* Get SELECT query string
*
* Compiles a SELECT query string and returns the sql.
*
* #access public
* #param string the table name to select from (optional)
* #param boolean TRUE: resets AR values; FALSE: leave AR vaules alone
* #return string
*/
public function get_compiled_select($table = '', $reset = TRUE)
{
if ($table != '')
{
$this->_track_aliases($table);
$this->from($table);
}
$select = $this->_compile_select();
if ($reset === TRUE)
{
$this->_reset_select();
}
return $select;
}

Resources