I run the following in the Chrome console without problem:
// Usage:
// console.log([...scope]); // => ['user','script','document']
// console.log(`${scope.user`}); // => 'user'
// console.log(scope.user.properties); // 'user.properties' (not real code)
class scope {
static user = new scope();
static script = new scope();
static document = new scope();
constructor(propertyーscope) {
if(propertyーscope in scope) return scope[propertyーscope];
}
get properties() {
return `${this.toString()}.properties`; // just for the example
// in reality, I'd return:
// PropertiesService[`get${this.toString().toSentenceCase()}Properties`]();
}
static *[Symbol.iterator]() { for (const key of Object.keys(scope)) yield key; }
toString() { return Object.entries(scope).filter(([key, value])=>value===this)[0][0]; }
};
However, Google Apps script refuses to save this code snippet and complains about the static declarations (= sign after static user.
Isn't GAS supposed to support static fields?
More importantly, how can I achieve the same?
(note: the dash in propertyーscope is not a dash but a unicode letter looking like it; I use that as a more readable alternative to underscores).
There's a way to simulate static fields in Apps Script. It involves using properties instead of a field. We can create a lazily initiated property that replaces itself with the following code:
class MyClass {
static get c() {
// Delete this property. We have to delete it first otherwise we cannot set it (due to it being a get-only property)
delete MyClass.c;
// Replace it with a static value.
return MyClass.c = new StaticObject();
}
}
To confirm this works, we can use the following:
SpreadsheetApp.getUi().alert(MyClass.c === MyClass.c)
This will only evaluate to true if the object was generated once and stored. If the field remains a property, it will return false.
Related
Is it possible to write a TypeScript Function which programmatically creates copies of an object, without hardcoding references to the object properties being copied?
I'm trying to enable a user workflow where the user is empowered to create copies of a specified object, i.e. create and partially complete one object using a Form, create 10x duplicates of the new object, fill-in data for new objects.
I've succeeded in creating a Function-Backed Action which duplicates a designated object, but all references to the properties being copied are hardcoded, which is less than ideal for maintenance and is a relatively common request for our org.
Code example for desired Function:
#OntologyEditFunction()
public GenerticBatchCopyObjects(mySelectedObject: ObjectAPIName, numberNewObjects: Integer): void {
/** Set UUID primary key */
let object_pk = Uuid.random()
for (let i = 1; i <= numberNewObjects; i++) {
/** initialize new object */
const newObject = Objects.create().ObjectAPIName(object_pk + "_" + String(i))
/** copy all properties from selected object record to new object */
for property in mySelectedObject.properties:
if property != "name_of_primary_key_column":
newObject.property = mySelectedObject.property
}
}
There’s not really a nice way to achieve this currently and it’s maybe not advised. This is primarily because the list of properties that will be copied from an object are fixed at publish-/compile-time in any (nice) method I can see.
Partial type-safe solution for copying properties only
I've included the most generic version of this function I can construct below, but it has some limitations. More concretely, the copy function
does not copy links that aren't represented by FK properties (i.e. it only copies properties);
does not adapt and might break when there are new or removed properties; and
is not easily maintained as functionality can change depending on when it is compiled.
private isPrimaryKey(x: string): x is MyObjectType["primaryKey"]["apiName"] {
return x === MyObjectType.primaryKey.apiName;
}
#OntologyEditFunction()
public copy(obj: MyObjectType): void {
const new_obj = Objects.create().myObjectType(Uuid.random());
var prop : keyof typeof MyObjectType.properties;
for (prop in MyObjectType.properties) {
if (!this.isPrimaryKey(prop)) {
new_obj[prop] = obj[prop];
}
}
}
Explicit type-safe solution for copying everything
The approach below requires more manual adjustment if the ontology changes, but makes it explicit that the code must be changed in line with ontology changes. This approach also copies links.
It is also worth noting that this behaviour may not be desired behaviour because it doesn't recursively copy the linked objects and instead attempts to copy the links. Please test that this gives the desired results.
static PROPERTIES = ["myProperty"] as const;
static MULTILINKS = ["myMultiLink"] as const;
static SINGLELINKS = ["mySingleLink", "myOtherSingleLink"] as const;
#OntologyEditFunction()
public copy2(obj: MyObjectType): void {
const new_obj = Objects.create().myObjectType(Uuid.random());
MyFunctions.PROPERTIES.forEach(p => {
new_obj[p] = obj[p];
});
MyFunctions.MULTILINKS.forEach(p => {
obj[p].all().forEach(v => new_obj[p].add(v));
});
MyFunctions.SINGLELINKS.forEach(p => {
const v = obj[p].get();
if (v !== undefined) {
new_obj[p].set(v);
}
});
}
You may need to exclude some of the code if your object type does not have properties/multi-links/single-links to make the compiler happy.
I've implemented an IMvxNavigationFacade for deep linking in my MvvmCross 5.6.x sample app. I've added logic in BuildViewModelRequest() to construct a MvxViewModelRequest with parameters passed in as MvxBundle.
if (url.StartsWith("http://www.rseg.net/rewards/"))
{
var parametersBundle = new MvxBundle();
var id = url.Substring(url.LastIndexOf('/') + 1);
parametersBundle.Data.Add("id", id);
return Task.FromResult(
new MvxViewModelRequest(typeof(RewardDetailViewModel),
parametersBundle, null));
}
However, this approach causes the old style Init() method to be called in the target ViewModel rather than the new typesafe Prepare() method.
public class RewardDetailViewModel :
MvxViewModel<RewardDetailViewModel.Parameteres>
{
...
public new void Init(string id)
{
if (!string.IsNullOrWhiteSpace(id))
{
if (int.TryParse(id, out _rewardId))
RaiseAllPropertiesChanged();
}
}
public override void Prepare(Parameteres parameter)
{
if (parameter != null)
{
_rewardId = parameter.RewardId;
RaiseAllPropertiesChanged();
}
}
}
Is there a way to construct a MvxViewModelRequest so that you pass in an instance of the parameter class for the target ViewModel causing the Prepare() method to be called?
The entire solution can be viewed on GitHub https://github.com/rsegtx/So.MvvmNav2
Thanks in advance!
After doing some research I found at lease one way to accomplish this.
Create a ViewModelInstanceRequest rather than a ViewModelRequest so that you can call ViewModelLoader.LoadViewModel passing in a parameters object; the ViewModelRequest only allows parameters to be passed using a MvxBundle. Make the following change to BuildViewModelRequest() on the NavigationFacade:
var request = new
MvxViewModelInstanceRequest(typeof(RewardDetailViewModel));
var parameters = new RewardDetailViewModel.Parameteres();
.... parse parameters and fill in parameters object
request.ViewModelInstance = ViewModelLoader.LoadViewModel(
request, parameters, null);
return Task.FromResult((MvxViewModelRequest)request);
Create your own IMvxNavigationService and add logic to inspect the object returned from the NavigationFacde and if it is a ViewModelInstanceRequest then use it as is rather than one previously creating.
var facadeRequest = await facade.BuildViewModelRequest(path,
paramDict).ConfigureAwait(false);
...
if (facadeRequest is MvxViewModelInstanceRequest)
request = facadeRequest as MvxViewModelInstanceRequest;
else
{
facadeRequest.ViewModelType = facadeRequest.ViewModelType;
if (facadeRequest.ParameterValues != null)
{
request.ParameterValues = facadeRequest.ParameterValues;
}
request.ViewModelInstance = ViewModelLoader.LoadViewModel(
request, null);
}
I've updated the original example on GitHub https://github.com/rsegtx/So.MvvmNav2.
I have the following inputs:
A CSV File
An array of grammar rules. The grammar rules are
basically metadata that tells me what which each column datatype
should be.
The output would return back to me a list of records that had any errors. So if column should be a date but I'm given the wrong format. I would return those rows.
The csv file would be something like this:
first_name,last_name,dob,age,
john,doe,2001/05/02
mary,jane,1968/04/01
Metadata:
column:first_name
type:string
column:dob
type:date
I was wondering if the strategy pattern would be the right choice. I was thinking of injecting the proper grammar (metadata) depending upon the file. I have multiple files I want to validate.
This problem needs the Validation Handlers (for your grammar rule). Looking at lower complexity level and expected extensions, I do not feel the need of any specific design pattern. I would suggest following simple OO approach. Alternatively depending upon expected dynamic behavior, COR can be incorporated by putting each Concrete Handler in a chain (COR). Pass each token in a chain so as to give opportunity to handlers in a chain till it gets handled.
public class Extractor {
public static void main(String[] args) {
// PREPARE TEMP_MAP_HANDLERS<Type,Handler>
Map<String, Handler> handlers = new HashMap<>();
handlers.put("FIRST_NAME",new NAMEHandler());
handlers.put("LAST_NAME",new NAMEHandler());
handlers.put("DOB",new DOBHandler());
handlers.put("AGE",new AGEHandler());
// READ THE HEADER
String header = "first_name,last_name,dob,age";// SAMPLE READ HEADER
// PREPARE LOOKUP<COL_INDEX, TYPE_HANDLER>
Map<Integer, Handler> metaHandlers = new HashMap<>();
String[] headerTokens = header.split(",");
for (int i = 0; i < headerTokens.length; i++) {
metaHandlers.put(i, handlers.get(headerTokens[i].toUpperCase()));
}
// DONE WITH TEMP HANDLER LOOKUP
// READ ACTUAL ROWS
// FOR EACH ROW IN FILE
String row = "joh*n,doe,2001/05/02";
String[] rowTokens = row.split(",");
for (int i = 0; i < rowTokens.length;i++) {
System.out.println(rowTokens[i]);
Handler handler = metaHandlers.get(i);
if (!handler.validate(rowTokens[i])){
// REPORT WRONG DATA
System.out.println("Wrong Token" + rowTokens[i]);
}
}
}
}
abstract class Handler {
abstract boolean validate (String field);
}
class NAMEHandler extends Handler{
#Override
boolean validate(String field) {
// Arbitrary rule - name should not contain *
return !field.contains("*");
}
}
class DOBHandler extends Handler{
#Override
boolean validate(String field) {
// Arbitrary rule - contains /
return field.contains("/");
}
}
class AGEHandler extends Handler{
#Override
boolean validate(String field) {
// TODO validate AGE
return true;
}
}
I have a ASP.NET WebApi project that I am working on. The boss would like the returns to support "partial response", meaning that though the data model might contain 50 fields, the client should be able to request specific fields for the response. The reason being that if they are implementing for example a list they simply don't need the overhead of all 50 fields, they might just want the First Name, Last Name and Id to generate the list. Thus far I have implemented a solution by using a custom Contract Resolver (DynamicContractResolver) such that when a request comes in I am peeking into it through a filter (FieldListFilter) in the OnActionExecuting method and determining if a field named "FieldList" is present and then if it is I am replacing the current ContractResolver with a new instance of my DynamicContractResolver and I pass the fieldlist to the constructor.
Some sample code
DynamicContractResolver.cs
protected override IList<JsonProperty> CreateProperties(Type type, Newtonsoft.Json.MemberSerialization memberSerialization)
{
List<String> fieldList = ConvertFieldStringToList();
IList<JsonProperty> properties = base.CreateProperties(type, memberSerialization);
if (fieldList.Count == 0)
{
return properties;
}
// If we have fields, check that FieldList is one of them.
if (!fieldList.Contains("FieldList"))
// If not then add it, FieldList must ALWAYS be a part of any non null field list.
fieldList.Add("FieldList");
if (!fieldList.Contains("Data"))
fieldList.Add("Data");
if (!fieldList.Contains("FilterText"))
fieldList.Add("FilterText");
if (!fieldList.Contains("PageNumber"))
fieldList.Add("PageNumber");
if (!fieldList.Contains("RecordsReturned"))
fieldList.Add("RecordsReturned");
if (!fieldList.Contains("RecordsFound"))
fieldList.Add("RecordsFound");
for (int ctr = properties.Count-1; ctr >= 0; ctr--)
{
foreach (string field in fieldList)
{
if (field.Trim() == properties[ctr].PropertyName)
{
goto Found;
}
}
System.Diagnostics.Debug.WriteLine("Remove Property at Index " + ctr + " Named: " + properties[ctr].PropertyName);
properties.RemoveAt(ctr);
// Exit point for the inner foreach. Nothing to do here.
Found: { }
}
return properties;
}
FieldListFilter.cs
public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
{
if (!actionContext.ModelState.IsValid)
{
throw new HttpResponseException(HttpStatusCode.BadRequest);
}
// We need to determine if there is a FieldList property of the model that is being used.
// First get a reference to the model.
var modelObject = actionContext.ActionArguments.FirstOrDefault().Value;
string fieldList = string.Empty;
try
{
// Using reflection, attempt to get the value of the FieldList property
var fieldListTemp = modelObject.GetType().GetProperty("FieldList").GetValue(modelObject);
// If it is null then use an empty string
if (fieldListTemp != null)
{
fieldList = fieldListTemp.ToString();
}
}
catch (Exception)
{
fieldList = string.Empty;
}
// Update the global ContractResolver with the fieldList value but for efficiency only do it if they are not the same as the current ContractResolver.
if (((DynamicContractResolver)GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ContractResolver).FieldList != fieldList)
{
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new DynamicContractResolver(fieldList);
}
}
I can then send a request with the json content payload looking as such:
{
"FieldList":"NameFirst,NameLast,Id",
"Data":[
{
"Id":1234
},
{
"Id":1235
}
]
}
and I will receive a response like so:
{
"FieldList":"NameFirst,NameLast,Id",
"Data":[
{
"NameFirst":"Brian",
"NameLast":"Mueller",
"Id":1234
},
{
"NameFirst":"Brian",
"NameLast":"Mueller",
"Id":1235
}
]
}
I believe that using the ContractResolver might run into threading issues. If I change it for one request is it going to be valid for all requests thereafter until someone changes it on another request (seems so through testing) If that is the case, then I don't see the usefulness for my purpose.
In summary, I am looking for a way to have dynamic data models such that the output from a request is configurable by the client on a request by request basis. Google implements this in their web api and they call it "partial response" and it works great. My implementation works, to a point but I fear that it will be broken for multiple simultaneous requests.
Suggestions? Tips?
A simpler solution that may work.
Create a model class with all 50 members with nullable types.
Assign values to the requested members.
Just return the result in the normal way.
In your WebApiConfig.Register() you must set the null value handling.
config.Formatters.JsonFormatter.SerializerSettings =
new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
You must not touch the configuration. You need the contract resolver on per-request basis. You can use it in your action method like this.
public class MyController : ApiController
{
public HttpResponseMessage Get()
{
var formatter = new JsonMediaTypeFormatter();
formatter.SerializerSettings.ContractResolver =
new DynamicContractResolver(new List<string>()
{"Id", "LastName"}); // you will get this from your filter
var dto = new MyDto()
{ FirstName = "Captain", LastName = "Cool", Id = 8 };
return new HttpResponseMessage()
{
Content = new ObjectContent<MyDto>(dto, formatter)
};
// What goes out is {"LastName":"Cool","Id":8}
}
}
By doing this, you are locking yourself into JSON content type for response messages but you have already made that decision by using a Json.NET specific feature. Also, note you are creating a new JsonMediaTypeFormatter. So, anything you configure to the one in the configuration such as media type mapping is not going to be available with this approach though.
I know this question is from many years ago, but if you're looking to do this with modern releases of the framework, I'd recommend nowadays to use OData services (http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/using-select-expand-and-value).
How would I write a simple LINQ to SQL extension method called "IsActive" which would contain a few basic criteria checks of a few different fields, so that I could reuse this "IsActive" logic all over the place without duplicating the logic.
For example, I would like to be able to do something like this:
return db.Listings.Where(x => x.IsActive())
And IsActive would be something like:
public bool IsActive(Listing SomeListing)
{
if(SomeListing.Approved==true && SomeListing.Deleted==false)
return true;
else
return false;
}
Otherwise, I am going to have to duplicate the same old where criteria in a million different queries right throughout my system.
Note: method must render in SQL..
Good question, there is a clear need to be able to define a re-useable filtering expression to avoid redundantly specifying logic in disparate queries.
This method will generate a filter you can pass to the Where method.
public Expression<Func<Listing, bool>> GetActiveFilter()
{
return someListing => someListing.Approved && !someListing.Deleted;
}
Then later, call it by:
Expression<Func<Filter, bool>> filter = GetActiveFilter()
return db.Listings.Where(filter);
Since an Expression<Func<T, bool>> is used, there will be no problem translating to sql.
Here's an extra way to do this:
public static IQueryable<Filter> FilterToActive(this IQueryable<Filter> source)
{
var filter = GetActiveFilter()
return source.Where(filter);
}
Then later,
return db.Listings.FilterToActive();
You can use a partial class to achieve this.
In a new file place the following:
namespace Namespace.Of.Your.Linq.Classes
{
public partial class Listing
{
public bool IsActive()
{
if(this.Approved==true && this.Deleted==false)
return true;
else
return false;
}
}
}
Since the Listing object (x in your lambda) is just an object, and Linq to SQL defines the generated classes as partial, you can add functionality (properties, methods, etc) to the generated classes using partial classes.
I don't believe the above will be rendered into the SQL query. If you want to do all the logic in the SQL Query, I would recommend making a method that calls the where method and just calling that when necessary.
EDIT
Example:
public static class DataManager
{
public static IEnumerable<Listing> GetActiveListings()
{
using (MyLinqToSqlDataContext ctx = new MyLinqToSqlDataContext())
{
return ctx.Listings.Where(x => x.Approved && !x.Deleted);
}
}
}
Now, whenever you want to get all the Active Listings, just call DataManager.GetActiveListings()
public static class ExtensionMethods
{
public static bool IsActive( this Listing SomeListing)
{
if(SomeListing.Approved==true && SomeListing.Deleted==false)
return true;
else
return false;
}
}
Late to the party here, but yet another way to do it that I use is:
public static IQueryable<Listing> GetActiveListings(IQueryable<Listing> listings)
{
return listings.Where(x => x.Approved && !x.Deleted);
}
and then
var activeListings = GetActiveListings(ctx.Listings);