JSdoc trying to document a member of undocumented symbol - javascript

I am trying to use JSDoc in a Backbone Model, so I have the following profile.js file:
/**
A module representing a Profile.
#exports models/profle
#example
// Load module
require(['models/profile'], function(Profile) {
var Myprofile = new Profile();
});
*/
define(['backbone'], function(Backbone) {
/**
#constructor
#requires Backbone
#augments module:Backbone.Model
*/
var Profile = Backbone.Model.extend({
/**
#lends profile.prototype
*/
defaults: {
PIN : null,
is_guest: true,
has_pin: false,
imgSrc: null
},
/**
Validate a Profile
The validate method is passed the model attributes, as well as the options from set or save
#param {attrs} The attributes of the profile
#param {options} The options from set or save
*/
validate: function(attrs, options) {
if (!attrs.name || attrs.name.length == 0)
return 'You must enter a name!';
if (!!attrs.name && attrs.name.length < 1 && attrs.name.length > 24)
return "User name must be between 1 and 24 characters";
if (!attrs.password || attrs.password.length != 4)
return "The pincode has to be four digits";
if (!attrs.password || attrs.password != attrs.confirmPassword)
return "Confirmed pincode does not match";
}
});
return Profile;
});
The problem is that when I do `jsdoc profile.js -d="home/mydocumentation" it generates a warning
>> WARNING: Trying to document validate as a member of undocumented symbol profile.
and it didn't document the validate function, any idea what I am doing wrong?

Related

grpc: protobuf cross-language code generation results in naming inconsistency

I found that using snake_case in protobuf definition will have slightly different generated method/class names across different languages. The difference is in the casing if the protocol field name uses snake_case.
Example
A regular protoc code-generation based on the following protocol
syntax = "proto3";
package myservice;
service Myservice {
rpc MyService(Request) returns (Reply) {}
}
message Request {
bool my_foo = 1;
bool my_bar = 2;
}
message Reply {
bool is_succeeded = 1;
}
yields the following generated naming
Python
python -m grpc_tools.protoc -I="${SRC_DIR}" --python_out="${DST_DIR}" --grpc_python_out=$DST_DIR --proto_path="${SRC_DIR}" my.proto
my_pb2_grpc.py
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
"""Client and server classes corresponding to protobuf-defined services."""
import grpc
import my_pb2 as my__pb2
class MyserviceStub(object):
"""Missing associated documentation comment in .proto file."""
def __init__(self, channel):
"""Constructor.
Args:
channel: A grpc.Channel.
"""
self.MyService = channel.unary_unary(
'/myservice.Myservice/MyService',
request_serializer=my__pb2.Request.SerializeToString,
response_deserializer=my__pb2.Reply.FromString,
)
class MyserviceServicer(object):
"""Missing associated documentation comment in .proto file."""
def MyService(self, request, context):
"""Missing associated documentation comment in .proto file."""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')
def add_MyserviceServicer_to_server(servicer, server):
rpc_method_handlers = {
'MyService': grpc.unary_unary_rpc_method_handler(
servicer.MyService,
request_deserializer=my__pb2.Request.FromString,
response_serializer=my__pb2.Reply.SerializeToString,
),
}
generic_handler = grpc.method_handlers_generic_handler(
'myservice.Myservice', rpc_method_handlers)
server.add_generic_rpc_handlers((generic_handler,))
# This class is part of an EXPERIMENTAL API.
class Myservice(object):
"""Missing associated documentation comment in .proto file."""
#staticmethod
def MyService(request,
target,
options=(),
channel_credentials=None,
call_credentials=None,
insecure=False,
compression=None,
wait_for_ready=None,
timeout=None,
metadata=None):
return grpc.experimental.unary_unary(request, target, '/myservice.Myservice/MyService',
my__pb2.Request.SerializeToString,
my__pb2.Reply.FromString,
options, channel_credentials,
insecure, call_credentials, compression, wait_for_ready, timeout, metadata)
my_pb2.py
# -*- coding: utf-8 -*-
# Generated by the protocol buffer compiler. DO NOT EDIT!
# source: my.proto
"""Generated protocol buffer code."""
from google.protobuf import descriptor as _descriptor
from google.protobuf import message as _message
from google.protobuf import reflection as _reflection
from google.protobuf import symbol_database as _symbol_database
# ##protoc_insertion_point(imports)
_sym_db = _symbol_database.Default()
DESCRIPTOR = _descriptor.FileDescriptor(
name='my.proto',
package='myservice',
syntax='proto3',
serialized_options=None,
create_key=_descriptor._internal_create_key,
serialized_pb=b'\n\x08my.proto\x12\tmyservice\")\n\x07Request\x12\x0e\n\x06my_foo\x18\x01 \x01(\x08\x12\x0e\n\x06my_bar\x18\x02 \x01(\x08\"\x1d\n\x05Reply\x12\x14\n\x0cis_succeeded\x18\x01 \x01(\x08\x32#\n\tMyservice\x12\x33\n\tMyService\x12\x12.myservice.Request\x1a\x10.myservice.Reply\"\x00\x62\x06proto3'
)
_REQUEST = _descriptor.Descriptor(
name='Request',
full_name='myservice.Request',
filename=None,
file=DESCRIPTOR,
containing_type=None,
create_key=_descriptor._internal_create_key,
fields=[
_descriptor.FieldDescriptor(
name='my_foo', full_name='myservice.Request.my_foo', index=0,
number=1, type=8, cpp_type=7, label=1,
has_default_value=False, default_value=False,
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key),
_descriptor.FieldDescriptor(
name='my_bar', full_name='myservice.Request.my_bar', index=1,
number=2, type=8, cpp_type=7, label=1,
has_default_value=False, default_value=False,
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key),
],
extensions=[
],
nested_types=[],
enum_types=[
],
serialized_options=None,
is_extendable=False,
syntax='proto3',
extension_ranges=[],
oneofs=[
],
serialized_start=23,
serialized_end=64,
)
_REPLY = _descriptor.Descriptor(
name='Reply',
full_name='myservice.Reply',
filename=None,
file=DESCRIPTOR,
containing_type=None,
create_key=_descriptor._internal_create_key,
fields=[
_descriptor.FieldDescriptor(
name='is_succeeded', full_name='myservice.Reply.is_succeeded', index=0,
number=1, type=8, cpp_type=7, label=1,
has_default_value=False, default_value=False,
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key),
],
extensions=[
],
nested_types=[],
enum_types=[
],
serialized_options=None,
is_extendable=False,
syntax='proto3',
extension_ranges=[],
oneofs=[
],
serialized_start=66,
serialized_end=95,
)
DESCRIPTOR.message_types_by_name['Request'] = _REQUEST
DESCRIPTOR.message_types_by_name['Reply'] = _REPLY
_sym_db.RegisterFileDescriptor(DESCRIPTOR)
Request = _reflection.GeneratedProtocolMessageType('Request', (_message.Message,), {
'DESCRIPTOR' : _REQUEST,
'__module__' : 'my_pb2'
# ##protoc_insertion_point(class_scope:myservice.Request)
})
_sym_db.RegisterMessage(Request)
Reply = _reflection.GeneratedProtocolMessageType('Reply', (_message.Message,), {
'DESCRIPTOR' : _REPLY,
'__module__' : 'my_pb2'
# ##protoc_insertion_point(class_scope:myservice.Reply)
})
_sym_db.RegisterMessage(Reply)
_MYSERVICE = _descriptor.ServiceDescriptor(
name='Myservice',
full_name='myservice.Myservice',
file=DESCRIPTOR,
index=0,
serialized_options=None,
create_key=_descriptor._internal_create_key,
serialized_start=97,
serialized_end=161,
methods=[
_descriptor.MethodDescriptor(
name='MyService',
full_name='myservice.Myservice.MyService',
index=0,
containing_service=None,
input_type=_REQUEST,
output_type=_REPLY,
serialized_options=None,
create_key=_descriptor._internal_create_key,
),
])
_sym_db.RegisterServiceDescriptor(_MYSERVICE)
DESCRIPTOR.services_by_name['Myservice'] = _MYSERVICE
# ##protoc_insertion_point(module_scope)
Node.js
With grpc-node the result looks like this
grpc_tools_node_protoc --js_out=import_style=commonjs,binary:"${DST_DIR}" --grpc_out=grpc_js:"${DST_DIR}" --proto_path="${SRC_DIR}" my.proto
my_grpc_pb.js
// GENERATED CODE -- DO NOT EDIT!
'use strict';
var grpc = require('#grpc/grpc-js');
var my_pb = require('./my_pb.js');
function serialize_myservice_Reply(arg) {
if (!(arg instanceof my_pb.Reply)) {
throw new Error('Expected argument of type myservice.Reply');
}
return Buffer.from(arg.serializeBinary());
}
function deserialize_myservice_Reply(buffer_arg) {
return my_pb.Reply.deserializeBinary(new Uint8Array(buffer_arg));
}
function serialize_myservice_Request(arg) {
if (!(arg instanceof my_pb.Request)) {
throw new Error('Expected argument of type myservice.Request');
}
return Buffer.from(arg.serializeBinary());
}
function deserialize_myservice_Request(buffer_arg) {
return my_pb.Request.deserializeBinary(new Uint8Array(buffer_arg));
}
var MyserviceService = exports.MyserviceService = {
myService: {
path: '/myservice.Myservice/MyService',
requestStream: false,
responseStream: false,
requestType: my_pb.Request,
responseType: my_pb.Reply,
requestSerialize: serialize_myservice_Request,
requestDeserialize: deserialize_myservice_Request,
responseSerialize: serialize_myservice_Reply,
responseDeserialize: deserialize_myservice_Reply,
},
};
exports.MyserviceClient = grpc.makeGenericClientConstructor(MyserviceService);
my_pb.js
// source: my.proto
/**
* #fileoverview
* #enhanceable
* #suppress {missingRequire} reports error on implicit type usages.
* #suppress {messageConventions} JS Compiler reports an error if a variable or
* field starts with 'MSG_' and isn't a translatable message.
* #public
*/
// GENERATED CODE -- DO NOT EDIT!
/* eslint-disable */
// #ts-nocheck
var jspb = require('google-protobuf');
var goog = jspb;
var global = Function('return this')();
goog.exportSymbol('proto.myservice.Reply', null, global);
goog.exportSymbol('proto.myservice.Request', null, global);
/**
* Generated by JsPbCodeGenerator.
* #param {Array=} opt_data Optional initial data array, typically from a
* server response, or constructed directly in Javascript. The array is used
* in place and becomes part of the constructed object. It is not cloned.
* If no data is provided, the constructed object will be empty, but still
* valid.
* #extends {jspb.Message}
* #constructor
*/
proto.myservice.Request = function(opt_data) {
jspb.Message.initialize(this, opt_data, 0, -1, null, null);
};
goog.inherits(proto.myservice.Request, jspb.Message);
if (goog.DEBUG && !COMPILED) {
/**
* #public
* #override
*/
proto.myservice.Request.displayName = 'proto.myservice.Request';
}
/**
* Generated by JsPbCodeGenerator.
* #param {Array=} opt_data Optional initial data array, typically from a
* server response, or constructed directly in Javascript. The array is used
* in place and becomes part of the constructed object. It is not cloned.
* If no data is provided, the constructed object will be empty, but still
* valid.
* #extends {jspb.Message}
* #constructor
*/
proto.myservice.Reply = function(opt_data) {
jspb.Message.initialize(this, opt_data, 0, -1, null, null);
};
goog.inherits(proto.myservice.Reply, jspb.Message);
if (goog.DEBUG && !COMPILED) {
/**
* #public
* #override
*/
proto.myservice.Reply.displayName = 'proto.myservice.Reply';
}
if (jspb.Message.GENERATE_TO_OBJECT) {
/**
* Creates an object representation of this proto.
* Field names that are reserved in JavaScript and will be renamed to pb_name.
* Optional fields that are not set will be set to undefined.
* To access a reserved field use, foo.pb_<name>, eg, foo.pb_default.
* For the list of reserved names please see:
* net/proto2/compiler/js/internal/generator.cc#kKeyword.
* #param {boolean=} opt_includeInstance Deprecated. whether to include the
* JSPB instance for transitional soy proto support:
* http://goto/soy-param-migration
* #return {!Object}
*/
proto.myservice.Request.prototype.toObject = function(opt_includeInstance) {
return proto.myservice.Request.toObject(opt_includeInstance, this);
};
/**
* Static version of the {#see toObject} method.
* #param {boolean|undefined} includeInstance Deprecated. Whether to include
* the JSPB instance for transitional soy proto support:
* http://goto/soy-param-migration
* #param {!proto.myservice.Request} msg The msg instance to transform.
* #return {!Object}
* #suppress {unusedLocalVariables} f is only used for nested messages
*/
proto.myservice.Request.toObject = function(includeInstance, msg) {
var f, obj = {
myFoo: jspb.Message.getBooleanFieldWithDefault(msg, 1, false),
myBar: jspb.Message.getBooleanFieldWithDefault(msg, 2, false)
};
if (includeInstance) {
obj.$jspbMessageInstance = msg;
}
return obj;
};
}
/**
* Deserializes binary data (in protobuf wire format).
* #param {jspb.ByteSource} bytes The bytes to deserialize.
* #return {!proto.myservice.Request}
*/
proto.myservice.Request.deserializeBinary = function(bytes) {
var reader = new jspb.BinaryReader(bytes);
var msg = new proto.myservice.Request;
return proto.myservice.Request.deserializeBinaryFromReader(msg, reader);
};
/**
* Deserializes binary data (in protobuf wire format) from the
* given reader into the given message object.
* #param {!proto.myservice.Request} msg The message object to deserialize into.
* #param {!jspb.BinaryReader} reader The BinaryReader to use.
* #return {!proto.myservice.Request}
*/
proto.myservice.Request.deserializeBinaryFromReader = function(msg, reader) {
while (reader.nextField()) {
if (reader.isEndGroup()) {
break;
}
var field = reader.getFieldNumber();
switch (field) {
case 1:
var value = /** #type {boolean} */ (reader.readBool());
msg.setMyFoo(value);
break;
case 2:
var value = /** #type {boolean} */ (reader.readBool());
msg.setMyBar(value);
break;
default:
reader.skipField();
break;
}
}
return msg;
};
/**
* Serializes the message to binary data (in protobuf wire format).
* #return {!Uint8Array}
*/
proto.myservice.Request.prototype.serializeBinary = function() {
var writer = new jspb.BinaryWriter();
proto.myservice.Request.serializeBinaryToWriter(this, writer);
return writer.getResultBuffer();
};
/**
* Serializes the given message to binary data (in protobuf wire
* format), writing to the given BinaryWriter.
* #param {!proto.myservice.Request} message
* #param {!jspb.BinaryWriter} writer
* #suppress {unusedLocalVariables} f is only used for nested messages
*/
proto.myservice.Request.serializeBinaryToWriter = function(message, writer) {
var f = undefined;
f = message.getMyFoo();
if (f) {
writer.writeBool(
1,
f
);
}
f = message.getMyBar();
if (f) {
writer.writeBool(
2,
f
);
}
};
/**
* optional bool my_foo = 1;
* #return {boolean}
*/
proto.myservice.Request.prototype.getMyFoo = function() {
return /** #type {boolean} */ (jspb.Message.getBooleanFieldWithDefault(this, 1, false));
};
/**
* #param {boolean} value
* #return {!proto.myservice.Request} returns this
*/
proto.myservice.Request.prototype.setMyFoo = function(value) {
return jspb.Message.setProto3BooleanField(this, 1, value);
};
/**
* optional bool my_bar = 2;
* #return {boolean}
*/
proto.myservice.Request.prototype.getMyBar = function() {
return /** #type {boolean} */ (jspb.Message.getBooleanFieldWithDefault(this, 2, false));
};
/**
* #param {boolean} value
* #return {!proto.myservice.Request} returns this
*/
proto.myservice.Request.prototype.setMyBar = function(value) {
return jspb.Message.setProto3BooleanField(this, 2, value);
};
if (jspb.Message.GENERATE_TO_OBJECT) {
/**
* Creates an object representation of this proto.
* Field names that are reserved in JavaScript and will be renamed to pb_name.
* Optional fields that are not set will be set to undefined.
* To access a reserved field use, foo.pb_<name>, eg, foo.pb_default.
* For the list of reserved names please see:
* net/proto2/compiler/js/internal/generator.cc#kKeyword.
* #param {boolean=} opt_includeInstance Deprecated. whether to include the
* JSPB instance for transitional soy proto support:
* http://goto/soy-param-migration
* #return {!Object}
*/
proto.myservice.Reply.prototype.toObject = function(opt_includeInstance) {
return proto.myservice.Reply.toObject(opt_includeInstance, this);
};
/**
* Static version of the {#see toObject} method.
* #param {boolean|undefined} includeInstance Deprecated. Whether to include
* the JSPB instance for transitional soy proto support:
* http://goto/soy-param-migration
* #param {!proto.myservice.Reply} msg The msg instance to transform.
* #return {!Object}
* #suppress {unusedLocalVariables} f is only used for nested messages
*/
proto.myservice.Reply.toObject = function(includeInstance, msg) {
var f, obj = {
isSucceeded: jspb.Message.getBooleanFieldWithDefault(msg, 1, false)
};
if (includeInstance) {
obj.$jspbMessageInstance = msg;
}
return obj;
};
}
/**
* Deserializes binary data (in protobuf wire format).
* #param {jspb.ByteSource} bytes The bytes to deserialize.
* #return {!proto.myservice.Reply}
*/
proto.myservice.Reply.deserializeBinary = function(bytes) {
var reader = new jspb.BinaryReader(bytes);
var msg = new proto.myservice.Reply;
return proto.myservice.Reply.deserializeBinaryFromReader(msg, reader);
};
/**
* Deserializes binary data (in protobuf wire format) from the
* given reader into the given message object.
* #param {!proto.myservice.Reply} msg The message object to deserialize into.
* #param {!jspb.BinaryReader} reader The BinaryReader to use.
* #return {!proto.myservice.Reply}
*/
proto.myservice.Reply.deserializeBinaryFromReader = function(msg, reader) {
while (reader.nextField()) {
if (reader.isEndGroup()) {
break;
}
var field = reader.getFieldNumber();
switch (field) {
case 1:
var value = /** #type {boolean} */ (reader.readBool());
msg.setIsSucceeded(value);
break;
default:
reader.skipField();
break;
}
}
return msg;
};
/**
* Serializes the message to binary data (in protobuf wire format).
* #return {!Uint8Array}
*/
proto.myservice.Reply.prototype.serializeBinary = function() {
var writer = new jspb.BinaryWriter();
proto.myservice.Reply.serializeBinaryToWriter(this, writer);
return writer.getResultBuffer();
};
/**
* Serializes the given message to binary data (in protobuf wire
* format), writing to the given BinaryWriter.
* #param {!proto.myservice.Reply} message
* #param {!jspb.BinaryWriter} writer
* #suppress {unusedLocalVariables} f is only used for nested messages
*/
proto.myservice.Reply.serializeBinaryToWriter = function(message, writer) {
var f = undefined;
f = message.getIsSucceeded();
if (f) {
writer.writeBool(
1,
f
);
}
};
/**
* optional bool is_succeeded = 1;
* #return {boolean}
*/
proto.myservice.Reply.prototype.getIsSucceeded = function() {
return /** #type {boolean} */ (jspb.Message.getBooleanFieldWithDefault(this, 1, false));
};
/**
* #param {boolean} value
* #return {!proto.myservice.Reply} returns this
*/
proto.myservice.Reply.prototype.setIsSucceeded = function(value) {
return jspb.Message.setProto3BooleanField(this, 1, value);
};
goog.object.extend(exports, proto.myservice);
Dart
With the same treatment as above
protoc --plugin=protoc-gen-dart="$HOME/.pub-cache/bin/protoc-gen-dart" --dart_out=grpc:"${DST_DIR}" -I"${SRC_DIR}" -I"$IncludeDir" my.proto
my.pbgrpc.dart
///
// Generated code. Do not modify.
// source: my.proto
//
// #dart = 2.12
// ignore_for_file: annotate_overrides,camel_case_types,unnecessary_const,non_constant_identifier_names,library_prefixes,unused_import,unused_shown_name,return_of_invalid_type,unnecessary_this,prefer_final_fields
import 'dart:async' as $async;
import 'dart:core' as $core;
import 'package:grpc/service_api.dart' as $grpc;
import 'my.pb.dart' as $0;
export 'my.pb.dart';
class MyserviceClient extends $grpc.Client {
static final _$myService = $grpc.ClientMethod<$0.Request, $0.Reply>(
'/myservice.Myservice/MyService',
($0.Request value) => value.writeToBuffer(),
($core.List<$core.int> value) => $0.Reply.fromBuffer(value));
MyserviceClient($grpc.ClientChannel channel,
{$grpc.CallOptions? options,
$core.Iterable<$grpc.ClientInterceptor>? interceptors})
: super(channel, options: options, interceptors: interceptors);
$grpc.ResponseFuture<$0.Reply> myService($0.Request request,
{$grpc.CallOptions? options}) {
return $createUnaryCall(_$myService, request, options: options);
}
}
abstract class MyserviceServiceBase extends $grpc.Service {
$core.String get $name => 'myservice.Myservice';
MyserviceServiceBase() {
$addMethod($grpc.ServiceMethod<$0.Request, $0.Reply>(
'MyService',
myService_Pre,
false,
false,
($core.List<$core.int> value) => $0.Request.fromBuffer(value),
($0.Reply value) => value.writeToBuffer()));
}
$async.Future<$0.Reply> myService_Pre(
$grpc.ServiceCall call, $async.Future<$0.Request> request) async {
return myService(call, await request);
}
$async.Future<$0.Reply> myService($grpc.ServiceCall call, $0.Request request);
}
my.pb.dart
///
// Generated code. Do not modify.
// source: my.proto
//
// #dart = 2.12
// ignore_for_file: annotate_overrides,camel_case_types,unnecessary_const,non_constant_identifier_names,library_prefixes,unused_import,unused_shown_name,return_of_invalid_type,unnecessary_this,prefer_final_fields
import 'dart:core' as $core;
import 'package:protobuf/protobuf.dart' as $pb;
class Request extends $pb.GeneratedMessage {
static final $pb.BuilderInfo _i = $pb.BuilderInfo(const $core.bool.fromEnvironment('protobuf.omit_message_names') ? '' : 'Request', package: const $pb.PackageName(const $core.bool.fromEnvironment('protobuf.omit_message_names') ? '' : 'myservice'), createEmptyInstance: create)
..aOB(1, const $core.bool.fromEnvironment('protobuf.omit_field_names') ? '' : 'myFoo')
..aOB(2, const $core.bool.fromEnvironment('protobuf.omit_field_names') ? '' : 'myBar')
..hasRequiredFields = false
;
Request._() : super();
factory Request({
$core.bool? myFoo,
$core.bool? myBar,
}) {
final _result = create();
if (myFoo != null) {
_result.myFoo = myFoo;
}
if (myBar != null) {
_result.myBar = myBar;
}
return _result;
}
factory Request.fromBuffer($core.List<$core.int> i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromBuffer(i, r);
factory Request.fromJson($core.String i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromJson(i, r);
#$core.Deprecated(
'Using this can add significant overhead to your binary. '
'Use [GeneratedMessageGenericExtensions.deepCopy] instead. '
'Will be removed in next major version')
Request clone() => Request()..mergeFromMessage(this);
#$core.Deprecated(
'Using this can add significant overhead to your binary. '
'Use [GeneratedMessageGenericExtensions.rebuild] instead. '
'Will be removed in next major version')
Request copyWith(void Function(Request) updates) => super.copyWith((message) => updates(message as Request)) as Request; // ignore: deprecated_member_use
$pb.BuilderInfo get info_ => _i;
#$core.pragma('dart2js:noInline')
static Request create() => Request._();
Request createEmptyInstance() => create();
static $pb.PbList<Request> createRepeated() => $pb.PbList<Request>();
#$core.pragma('dart2js:noInline')
static Request getDefault() => _defaultInstance ??= $pb.GeneratedMessage.$_defaultFor<Request>(create);
static Request? _defaultInstance;
#$pb.TagNumber(1)
$core.bool get myFoo => $_getBF(0);
#$pb.TagNumber(1)
set myFoo($core.bool v) { $_setBool(0, v); }
#$pb.TagNumber(1)
$core.bool hasMyFoo() => $_has(0);
#$pb.TagNumber(1)
void clearMyFoo() => clearField(1);
#$pb.TagNumber(2)
$core.bool get myBar => $_getBF(1);
#$pb.TagNumber(2)
set myBar($core.bool v) { $_setBool(1, v); }
#$pb.TagNumber(2)
$core.bool hasMyBar() => $_has(1);
#$pb.TagNumber(2)
void clearMyBar() => clearField(2);
}
class Reply extends $pb.GeneratedMessage {
static final $pb.BuilderInfo _i = $pb.BuilderInfo(const $core.bool.fromEnvironment('protobuf.omit_message_names') ? '' : 'Reply', package: const $pb.PackageName(const $core.bool.fromEnvironment('protobuf.omit_message_names') ? '' : 'myservice'), createEmptyInstance: create)
..aOB(1, const $core.bool.fromEnvironment('protobuf.omit_field_names') ? '' : 'isSucceeded')
..hasRequiredFields = false
;
Reply._() : super();
factory Reply({
$core.bool? isSucceeded,
}) {
final _result = create();
if (isSucceeded != null) {
_result.isSucceeded = isSucceeded;
}
return _result;
}
factory Reply.fromBuffer($core.List<$core.int> i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromBuffer(i, r);
factory Reply.fromJson($core.String i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromJson(i, r);
#$core.Deprecated(
'Using this can add significant overhead to your binary. '
'Use [GeneratedMessageGenericExtensions.deepCopy] instead. '
'Will be removed in next major version')
Reply clone() => Reply()..mergeFromMessage(this);
#$core.Deprecated(
'Using this can add significant overhead to your binary. '
'Use [GeneratedMessageGenericExtensions.rebuild] instead. '
'Will be removed in next major version')
Reply copyWith(void Function(Reply) updates) => super.copyWith((message) => updates(message as Reply)) as Reply; // ignore: deprecated_member_use
$pb.BuilderInfo get info_ => _i;
#$core.pragma('dart2js:noInline')
static Reply create() => Reply._();
Reply createEmptyInstance() => create();
static $pb.PbList<Reply> createRepeated() => $pb.PbList<Reply>();
#$core.pragma('dart2js:noInline')
static Reply getDefault() => _defaultInstance ??= $pb.GeneratedMessage.$_defaultFor<Reply>(create);
static Reply? _defaultInstance;
#$pb.TagNumber(1)
$core.bool get isSucceeded => $_getBF(0);
#$pb.TagNumber(1)
set isSucceeded($core.bool v) { $_setBool(0, v); }
#$pb.TagNumber(1)
$core.bool hasIsSucceeded() => $_has(0);
#$pb.TagNumber(1)
void clearIsSucceeded() => clearField(1);
}
Comparison
If we compare the following generated entities
Service class name
Service method name
Request field names
Reply field names
Here are the results
Python
Myservice
MyService
my_foo
my_bar
is_succeeded
Node.js
MyserviceService
myService
accessor: getMyFoo()
accessor: getMyBar()
accessor: getIsSucceeded()
Dart
MyserviceServiceBase
myService
myFoo
myBar
isSucceeded
Question
Although these naming schemes seem to cater to the language-specific convention,
the differences between them caused some maintenance issues.
I wonder if there is a way to ensure an identical casing treatment across all these languages, and possibly all supported languages.
Thanks!
Code generation plugins have complete freedom to generate whatever code they want. Usually, they try to follow the language's conventions. You'd need to have controls for each language, and most won't provide it.
What are the actual maintenance issues that you are facing? Perhaps there is some other way of solving them.

Convert IIFE module to something importable by RollupJS

I am using RollupJS as a bundler, and it can read CommonJS (via a plugin) or ES6 modules. But this module seems to be in UMD format, and I am looking for a quick way I can edit it (without replacing a lot of lines) so that it is in commonJS or ES6 format.
What do folks suggest? I show the top and the bottom of a 5,000 line .js file.
#module vrlinkjs
**/
(function (mak) {
mak.MessageKindEnum = {
Any : -1,
Other : 0,
AttributeUpdate : 1,
Interaction : 2,
Connect : 3,
ObjectDeletion : 4
};
/**
Decodes AttributeUpdate messages into an EnvironmentalStateRepository object.
#class EnvironmentalStateDecoder
#constructor
#augments StateDecoder
#param {WebLVCConnection} webLVCConnection Connection to a WebLVC server
**/
mak.EnvironmentalStateDecoder = function(webLVCConnection) {
mak.StateDecoder.apply(this, arguments);
};
mak.EnvironmentalStateDecoder.prototype = Object.create(mak.StateDecoder.prototype, {
constructor : { value : mak.EnvironmentalStateDecoder },
/**
Decodes a AttributeUpdate message into an EntityStateRepository object.
#method decode
#param {Object} attributeUpdate WebLVC AttributeUpdate message
#param {EntityStateRepository} stateRep State repository to be updated
**/
decode : {
value : function( attributeUpdate, stateRep ) {
// if(this.webLVCConnection.timeStampType == mak.TimeStampType.TimeStampAbsolute &&
// attributeUpdate.TimeStampType == mak.TimeStampType.TimeStampAbsolute) {
// } else {
// stateRep.timeStampType = mak.TimeStampType.TimeStampRelative;
// }
stateRep.timeStampType = mak.TimeStampType.TimeStampRelative;
var curTime = 0.0;
// if (stateRep->timeStampType() == DtTimeStampAbsolute)
// {
// // Use timestamp as time of validity
// curTime = pdu.guessTimeValid(myExConn->clock()->simTime());
// }
// else
// {
// // Use receive time as time of validity
// curTime = myExConn->clock()->simTime();
// }
curTime = this.webLVCConnection.clock.simTime;
if(attributeUpdate.ProcessIdentifier != undefined) {
stateRep.entityIdentifier = attributeUpdate.EntityIdentifier;
}
if(attributeUpdate.Type != undefined) {
stateRep.entityType = attributeUpdate.Type;
}
if(attributeUpdate.ObjectName != undefined) {
stateRep.objectName = attributeUpdate.ObjectName;
}
if(attributeUpdate.GeometryRecords != undefined) {
stateRep.GeometryRecords = attributeUpdate.GeometryRecords;
}
if(attributeUpdate.EnvObjData != undefined) {
if(attributeUpdate.EnvObjData.VrfObjName != undefined) {
stateRep.marking = attributeUpdate.EnvObjData.VrfObjName;
}
}
}
}
});
.....
} (this.mak = this.mak || {}));
UPDATE
I used the ES6 module solution from estus (below), which I really like. It solved the rollup bunding issue, but there is still a runtime error.
But there is a little more that needs to be done. I am getting this error with chrome. I have two varients of the HTML main.html file, one uses the bundle and the other just imports my es6 modules. The error occurs even when I am not using rollup and creating and using the bundle.
Uncaught TypeError: Cannot set property objectName of [object Object] which has only a getter
at mak$1.ReflectedEntity.mak$1.ReflectedObject [as constructor] (vrlink.mjs:818)
at new mak$1.ReflectedEntity (vrlink.mjs:903)
at mak$1.ReflectedEntityList.value (vrlink.mjs:1358)
at mak$1.WebLVCMessageCallbackManager.<anonymous> (vrlink.mjs:1155)
at mak$1.WebLVCMessageCallbackManager.processMessage (vrlink.mjs:1745)
at mak$1.WebLVCConnection.drainInput (vrlink.mjs:2139)
at SimLink.tick (SimLink.js:34)
This seems to be the offender when converting from IIFE modules to ES6. It says that there is no setter.
The code is not my creation, but it seemed like it should not have be a major effort to convert IIFE to ES6. The offending snippet is:
mak.VrfBackendStateRepository = function (objectName) {
/**
Unique string identifying entity
#property objectName
#type String
**/
this.objectName = objectName; //error generated on this line!
If you are wondering what this is, it is a object called mak.webLVConnection, which is created by this function in the IIFE code:
/**
Represents a connection to a WebLVC server.
clientName and port are required. webLVCVersion is optional (current version
supported by the WebLVC server will be in effect). serverLocation is optional
( websocket connection will be made to the host servering the javascript )
#class WebLVCConnection
#constructor
#param {String} clientName String representing name of the client federate
#param {Number} port Websocket port number
#param {Number} webLVCVersion WebLVC version number
#param {String} serverLocation Hostname of websocket server
**/
mak.WebLVCConnection = function (clientName, port, webLVCVersion, serverLocation, url) {
var self = this;
if (clientName == undefined) {
throw new Error("clientName not specified");
}
if (!(typeof clientName == "string" && clientName.length > 0)) {
throw new Error("Invalid ClientName specified");
}
if (port == undefined) {
throw new Error("Port not specified");
}
if (url == undefined) {
url = "/ws";
}
var websocket;
if (serverLocation == undefined) {
if (location.hostname) {
websocket = new WebSocket("ws://" + location.hostname + ":" + port + url);
}
else {
websocket = new WebSocket("ws://localhost:" + port + "/ws");
}
}
else {
websocket = new WebSocket("ws://" + serverLocation + ":" + port + url);
}
/**
Websocket connected to a WebLVC server.
#property websocket
#type WebSocket
**/
this.websocket = websocket;
/**
DIS/RPR-style identifier, used to generate new unique IDs for entities simulated
through this connection. Array of 3 numbers [site ID, host ID, entity number].
#property currentId
#type Array
**/
this.currentId = [1, 1, 0];
/**
Manages registration and invoking of message callbacks.
#property webLVCMessageCallbackManager
#type WebLVCMessageCallbackManager
**/
this.webLVCMessageCallbackManager = new mak.WebLVCMessageCallbackManager();
/**
Simulation clock
#property clock
#type Clock
**/
this.clock = new mak.Clock();
/**
Indicates whether timestamping is relative or absolute
(mak.TimeStampType.TimeStampRelative or
mak.TimeStampType.TimeStampAbsolute).
#property {Number} timeStampType
**/
this.timeStampType = mak.TimeStampType.TimeStampRelative;
/**
List of incoming messages. When messages are received, they are placed
in this queue. The drainInput() member function must be called regularly
to remove and process messages in this queue.
#property {Array} messageQueue
**/
this.messageQueue = new Array();
/**
Callback function invoked on receipt of a message. Calls
webLVCMessageCallbackManager.processMessage().
#method processMessage
#private
**/
this.processMessage = this.webLVCMessageCallbackManager.processMessage.bind(this.webLVCMessageCallbackManager);
/**
Callback function invoked when websocket connection is opened. Sends
the initial WebLVC connect message.
#method onopen
#private
**/
this.websocket.onopen = function () {
var connectMessage = {
MessageKind: mak.MessageKindEnum.Connect,
ClientName: clientName
}
if (webLVCVersion != undefined) {
connectMessage.WebLVCVersion = webLVCVersion;
}
if (self.websocket.readyState == 1) {
self.websocket.send(JSON.stringify(connectMessage));
}
};
/**
Callback function invoked when a WebLVC message is received. Parses the
the JSON message data and passes the resulting object to processMessage.
#method onmessage
#event {Object} JSON message
#private
**/
this.websocket.onmessage = function (event) {
//just in case
if (event.data == "ping")
return;
var message = JSON.parse(event.data);
if (message != null) {
self.messageQueue.push(message);
} else {
console.warn("onmessage - null message received");
}
};
/**
Callback function invoked when the websocket is closed.
#method onclose
#private
**/
this.websocket.onclose = function () {
console.debug("In websocket.onclose");
};
/**
Callback function invoked when an error in the websocket is detected.
Sends warning to console.
#method onerror
#private
**/
this.websocket.onerror = function () {
console.log("websocket onerror");
};
this.isOk = function () {
return this.websocket.readyState == 1;
}
};
mak.WebLVCConnection.prototype = {
constructor: mak.WebLVCConnection,
/**
Set the DIS/RPR-style application ID.
#method set applicationId
#param {Array} applicationId Array of 2 integers [site ID, host ID].
**/
set applicationId(applicationId) {
this.currentId[0] = applicationId[0];
this.currentId[1] = applicationId[1];
this.currentId[2] = 0;
},
/**
Returns next available DIS/RPR-style entity ID.
#method nextId
#return {Array} Array of 3 integers [site ID, host ID, entity number].
**/
get nextId() {
this.currentId[2]++;
return this.currentId;
},
/**
Register callback function for a given kind of message.
#method addMessageCallback
#param {Number} messageKind WebLVC MessageKind
#param callback Function to be invoked
**/
addMessageCallback: function (messageKind, callback) {
this.webLVCMessageCallbackManager.addMessageCallback(messageKind, callback);
},
/**
De-register callback function for a given kind of message.
#method removeMessageCallback
#param messageKind WebLVC MessageKind
#param callback Function to be invoked
**/
removeMessageCallback: function (messageKind, callback) {
this.webLVCMessageCallbackManager.removeMessageCallback(messageKind, callback);
},
/**
Send a WebLVC message to the server.
#method send
#param {Object} message
**/
send: function (message) {
try {
if (this.websocket.readyState == 1) {
this.websocket.send(JSON.stringify(message));
}
} catch (exception) {
console.log("Error sending on websocket - exception: " + exception);
}
},
/**
Send a time-stamped WebLVC message to the server.
#method sendStamped
#param {Object} message
**/
sendStamped: function (message) {
// Timestamp is hex string
var timeStamp = this.currentTimeForStamping().toString(16);
//message.TimeStamp = ""; // timeStamp;
this.send(message);
},
/**
Get the current simulation time for a time stamp.
#method currentTimeForStamping
#return {Number} Simulation time in seconds.
**/
currentTimeForStamping: function () {
if (this.timeStampType == mak.TimeStampType.TimeStampAbsolute) {
return this.clock.simTime();
}
else {
return this.clock.absRealTime();
}
},
/**
Iterate through message queue, calling processMessage() and then
removing each message. Should be called regularly from your
application.
#method drainInput
**/
drainInput: function () {
var message;
while (this.messageQueue.length > 0) {
message = this.messageQueue.shift();
this.processMessage(message);
}
},
/**
Closes the websocket connection. Calls the destroy method on its
WebLVCMessageCallbackManager data member.
#method destroy
**/
destroy: function () {
console.debug("In WebLVCConnection.destroy");
this.webLVCMessageCallbackManager.destroy();
this.websocket.close();
}
};
UMD modules, by definition, are CommonJS. The code above is just IIFE and relies on mak global.
IIFE wrapper function can be replaced with default or named ES module export:
const mak = {};
mak.MessageKindEnum = { ... };
...
export default mak;
Or with CommonJS export:
const mak = {};
mak.MessageKindEnum = { ... };
...
module.exports = mak;
did you try:
(function (mak) {
...
}(module.exports));
// instead of
// } (this.mak = this.mak || {}));

0 Results Returned from NetSuite Search

I am attempting to stop a record from being created based on a search result. I can't seem to return any data through my SuiteScript search though, even though I know for a fact the data exists.
I created a Custom Saved Search with the exact filter being used below and return the results I am looking for.
Does anything stand out on why I may not be retrieving any results?
NOTE: The sfdcAccountId Variable does have a value, so I am searching on a valid value.
// 2.0
define(["N/error", "N/log", "N/search"], function (err, log, search) {
/**
* User Event 2.0 example showing usage of the Submit events
*
* #NApiVersion 2.x
* #NModuleScope SameAccount
* #NScriptType UserEventScript
* #appliedtorecord customer
*/
var exports = {};
function beforeSubmit(scriptContext) {
log.debug({
"title": "Before Submit",
"details": "action=" + scriptContext.type
});
if (doesCustomerExist(scriptContext)) {
throw err.create({
"name": "DUPLICATE_SFDC_ACCOUNT_ID",
"message": "Customer Already Contains SFDC Account Id",
"notifyOff": true
});
}
}
function doesCustomerExist(scriptContext) {
var sfdcAccountId = scriptContext.newRecord.getValue('custentitysfdc_account_id');
log.debug({
"title": "Before Submit",
"details": "sfdcAccountId=" + sfdcAccountId
});
if(sfdcAccountId == null || sfdcAccountId == '') return false;
var searchResult = search.create({
type: search.Type.CUSTOMER,
filters: ['custentitysfdc_account_id', search.Operator.IS, sfdcAccountId]
}).run();
return (searchResult != null && searchResult.length > 0);
}
exports.beforeSubmit = beforeSubmit;
return exports;
});
When you call .run() on a search, it returns a search.ResultSet object. If you call getRange() on that object, you'll get the array of results that you're looking for. Here's an updated version of your search that returns search.Result[] on which you can check the length or iterate through as necessary.
var searchResult = search.create({
type: search.Type.CUSTOMER,
filters: ['custentitysfdc_account_id', search.Operator.IS, sfdcAccountId]
}).run().getRange({start: 0, end: 1000});

Json login with multiple user login in a single login page

I am trying to do a login form by using JSON, I have 3 different users to login in a single login page, they are as 1. User 2. Admin 3. Supplier
This is my code. I am doing this as a test case, I will change the authentication details to server side later on.
<html>
<head>
<title> Login Form</title>
<script>
function validate()
{
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
var loginlist = { "User": {
"userlist":[{"username":"usr1", "password":"a123"},{"username":"usr2", "password":"a123"},{"username":"usr3", "password":"a123"}],
"ID":1
},
"Admin": {
"adminlist":[{"username":"admin1", "password":"b123"},{"username":"admin2", "password":"b123"},{"username":"admin3", "password":"b123"}],
"ID":2
},
"Supplier": {
"supplierlist":[{"username":"sup1", "password":"c123"},{"username":"sup2", "password":"c123"},{"username":"sup3", "password":"c123"}],
"ID":1
}
};
var logged = false;
if(loginlist[0].User.ID === 1){
for(var i =0; i < loginlist[0].User.userlist.length; i++){
if(username == loginlist[0].User.userlist[i].username && password == loginlist[0].User.userlist[i].password){
logged= true;
}
}
if(logged)
alert("User login");
else
alert("User login fail");
}
else if(loginlist[0].Admin.ID === 2){
for(var j =0; j < loginlist[0].Admin.adminlist.length; j++){
if(username == loginlist[0].Admin.adminlist[j].username && password == loginlist[0].Admin.adminlist[j].password){
logged= true;
}
}
if(logged)
alert("Admin login");
else
alert("admin login fail");
}
else{
for(var k =0; k < loginlist[0].Supplier.supplierlist.length; k++){
if(username == loginlist[0].Supplier.supplierlist[k].username && password == loginlist[0].Supplier.supplierlist[k].password){
logged= true;
}
}
if(logged)
alert("Supplier login");
else
alert("supplier login fail");
}
}
</script>
</head>
<body>
<div class="container">
<div class="main">
<h2>Login Form</h2>
<form id="form_id" method="post" name="myform">
<label>User Name :</label>
<input type="text" placeholder="Enter username" name="username" id="username"/>
<label>Password :</label>
<input type="password" placeholder="Enter Password" name="password" id="password"/>
<input type="button" value="Login" id="submit" onclick="validate()"/>
</form>
</body>
</html>
which I have tried. My issue is when I enter something into the username and password nothing is displaying, I have done this kind of program but with the single user operation. I am not able to achieve this multiple users in a single login page
I am not quite sure what is the reason for having each individual user type as separate arrays, it might be good for some specific use case, but it will certainly be more straightforward to have all users in a single set.
Having everything in one array and then providing some functions to use to fetch a specific user type would be a better approach, as this way you don't need to worry about the result so much from the beginning. Besides you should always be working with as much raw material as possible; though people might find this arguable.
It is usually better to not trigger JavaScript code in the <head> tag of the HTML unless it is needed, as the code is executed before all of the document is loaded. It is usually safer to put all JavaScript code just before the closing <body> tag.
I also noticed you were triggering the validate function on button click. It is a problem if you are submitting the form via any other method than that (e.g. pressing enter on a focused input field). I added an event listener to the form itself, this should handle all common submit cases.
I made the users array have just one level, which contains all of the users. Each user has a type, which is used to determine the type of the user (regular user, admin user, supplier, etc.)
I have commented each line of the code below.
This script should NOT be used in production of any kind. It is dangerous as all checks are done client-side and client-side only. ALWAYS use server-side checks, preferably in test scripts too!
Click the Run Snippet button below to test the script here.
You can find the usernames and passwords from the array in the very beginning of the JavaScript script.
// store the IDs of all logged in users in here
var loggedusers = [];
// server should handle everything below...
// users array, which contains all users in the system
// on the server-side this could be an array returned by a MySQL database table, for example
var users = [{
// ID of the user
id: 1,
// username of the user
username: 'user1',
// password of the user, note that this should obviously be hashed on the server-side
// for PHP back-end: preferably hashed with 'password_hash' and compared using 'password_verify' if using PHP version 5.5 or newer
password: 'a',
// type of the user, currently using 'user', 'admin' and 'supplier', but technically it could be _anything_
type: 'user'
}, {
id: 2,
username: 'admin1',
password: 'b',
type: 'admin'
}, {
id: 3,
username: 'supplier1',
password: 'c',
type: 'supplier'
}];
// ... up to this point, never store this data on the client-side (especially highly sensitive information like hashes, salts, or even worse like plain text passwords like above).
/**
* null|Object getUserByProperty ( mixed key, mixed value [ , boolean strict = false, boolean multiple = false, boolean case_insensitive = false ] )
*
* Gets a user by a property key, value and various settings.
*
* #param mixed key Property key to look for.
* #param mixed value Property value to look for.
* #param boolean strict (optional) Should the comparison be type strict?
* #param boolean multiple (optional) Should it return all results, rather than the first result?
* #param boolean case_insensitive (optional) Should it ignore character case?
*
* #return null|Object Returns the user object, or null, if not found.
*/
function getUserByProperty(key, value, strict, multiple, case_insensitive) {
// prepare a result array
var result = [];
// loop through all of our users
for (var index in users) {
// get the user we are iterating through now
var user = users[index];
// check if the user has the specified property
if (typeof user[key] != 'undefined') {
// get the property value
var compare = user[key];
// doing something case insensitive
if (case_insensitive) {
// if the property value is a string
if (typeof compare == 'string')
// we want to turn it to lower case
compare = compare.toLowerCase();
// if the specified value is a string
if (typeof value == 'string')
// we want to turn it to lower case
value = value.toLowerCase();
}
// if specified value is not defined, or values match
if (typeof value == 'undefined' || ((strict && compare === value) || (!strict && compare == value))) {
// if we want multiple results
if (multiple) {
// the result will be appended to the result array
result.push(user);
} else {
// otherwise we just return it
return user;
}
}
}
}
// return the results or null, if nothing was found (for single match search)
return multiple ? result : null;
}
/**
* null|Object getUserById ( number id )
*
* Gets a user with the specified ID.
*
* #param number id ID of user to get.
*
* #return null|Object Returns the user object, or null, if not found.
*/
function getUserById(id) {
return getUserByProperty('id', id);
}
/**
* null|Object getUserByUsername ( string username [ , boolean case_insensitive = false ] )
*
* Gets a user with the specified username.
*
* #param string username Username of user to get.
* #param boolean case_insensitive Should character case be ignored?
*
* #return null|Object Returns the user object, or null, if not found.
*/
function getUserByUsername(username, case_insensitive) {
return getUserByProperty('username', username, false, false, case_insensitive);
}
/**
* boolean|array getUsersByType ( string type [ , boolean case_insensitive = false ] )
*
* Gets all users with the specified type.
*
* #param string type Type of user to look for.
* #param boolean case_insensitive Should character case be ignored?
*
* #return array Returns the an array of user objects.
*/
function getUsersByType(type, case_insensitive) {
return getUserByProperty('type', type, false, true, case_insensitive);
}
/**
* boolean|Object login ( string username, string password )
*
* Provides the functionality to be able to log in on a user.
*
* #param string username Username of the user to log in on.
* #param string password Password of the user to log in on.
*
* #return boolean|Object Returns the user object, or false, if login was not successful.
*/
function login(username, password) {
// checks whether username and password have been filled in
if (typeof username == 'string' && typeof password == 'string' && username.length > 0 && password.length > 0) {
// prepare a variable to store the user object, if any is received
var loggeduser;
// server should handle everything below...
// iterate through all users in the 'users' array (or database table perhaps, on server-side)
for (var index in users) {
// grab the property value with the property
var user = users[index];
// check if username and password match
if (username === user.username && password === user.password)
// set value of 'loggeduser' to the property value (user)
loggeduser = user;
}
// ... up to this point, and the user returned from the server should be set in to 'loggeduser'
// make sure highly sensitive information is not returned, such as hash, salt or anything
// check whether the user is set
if (typeof loggeduser != 'undefined') {
// save the ID of the user to the 'loggedusers' array
loggedusers[loggeduser.id] = true;
// update the logged in list
updatelist();
// return the received user object
return loggeduser;
}
}
return false;
}
/**
* boolean logout ( number userid )
*
* Provides the functionality to be able to log out from a user.
*
* #param number userid ID of the user to log out of.
*
* #return boolean Returns a boolean representing whether the log out was successful or not.
*/
function logout(userid) {
// check whether the ID is actually logged in
if (loggedusers[userid]) {
// temporary array, which we will be filling
var temporary = [];
// let's loop through logged users
for (var id in loggedusers)
// ignore our user
if (id != userid)
// let's put this user to the array
temporary[id] = true;
// we replace the 'loggedusers' array with our new array
loggedusers = temporary;
// update the logged in list
updatelist();
// we have successfully logged out
return true;
}
// we have not successfully logged out
return false;
}
/**
* boolean updatelist ( void )
*
* Provides the functionality to update the #logged-in-list element
* with the logged in users names and logout links.
*
* #return boolean Returns a boolean representing whether the update was successful or not.
*/
function updatelist() {
// get the #logged-in-list element
var list_element = document.getElementById('logged-in-list');
// check the element exists
if (list_element) {
// get the #logged-in element
var list_container_element = document.getElementById('logged-in');
// check the element exists and that we should be changing the styles
if (list_container_element)
// if there are no logged in users, "hide" the element, otherwise "show" it
list_container_element.style.visibility = loggedusers.length === 0 ? 'hidden' : 'visible';
// we take the first child with a while loop
while (list_element.firstChild)
// remove the child, and it will repeat doing so until there is no firstChild left for the list_element
list_element.removeChild(list_element.firstChild);
// we loop through every logged in user
for (var id in loggedusers) {
// get the user by ID
var user = getUserById(id);
// check if that user is a user
if (user) {
// we create necessary elements to cover our logout functionality
var p = document.createElement('P');
p.innerText = user.username;
var a = document.createElement('A');
a.userid = id;
a.href = '#';
a.innerHTML = '(logout)';
// we bind an onclick event listener to the link
a.addEventListener('click', function(e) {
e.preventDefault();
// we will now execute the logout function for this user ID
logout(e.srcElement.userid);
});
// we append the link to the paragraph element
p.appendChild(a);
// we append the paragraph to the list element
list_element.appendChild(p);
}
}
return true;
}
return false;
}
// add a new 'onsubmit' event listener to the '#login-form' node
// this will be triggered each time the form is submitted via any method
document.getElementById('login-form').addEventListener('submit', function(e) {
// prevent default browser behavior
e.preventDefault();
// find the username and password nodes
var username_element = e.srcElement.elements.username;
var password_element = e.srcElement.elements.password;
// check whether these elements return right stuff
if (username_element && password_element) {
// get the values of username and password
username = username_element.value;
password = password_element.value;
// execute the 'login' function with the username and password filled in on the client
var user = login(username, password);
// check whether the login was successful
if (user !== false) {
// reset the username input field
username_element.value = '';
// reset the password input field
password_element.value = '';
// alert the client that login was successful
alert('Logged in as ' + user.username + '.');
} else {
// reset the password input field
password_element.value = '';
// alert the client that login was not successful
alert('Invalid username and/or password.');
}
}
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Login Form</title>
</head>
<body>
<div style="visibility: hidden;" id="logged-in">
<p><strong>Logged in as:</strong>
</p>
<div id="logged-in-list"></div>
</div>
<form id="login-form">
<h2>Login Form</h2>
<label for="username">Username:</label>
<input type="text" placeholder="Enter username..." id="username" />
<label for="password">Password:</label>
<input type="password" placeholder="Enter password..." id="password" />
<input type="submit" value="Login" />
</form>
<!-- the JavaScript code should go as contents of this tag -->
<script></script>
</body>
</html>
Please, to anyone using this code, do not use this, under any circumstance, in production. Always perform validations and comparisons server-side, along with client-side checks before the server-side checks to cut out any unnecessary processing on the server.
Hope this helps you!
<?php
function jumlah_like($url){
// Query di FQL
$myfql = "SELECT like_count, share_count, comment_count ";
$myfql .= " FROM link_stat WHERE url = '$url'";
$fqlURL = "https://api.facebook.com/method/fql.query?format=json&query=" . urlencode($myfql);
// Facebook Response dalam JSON
$response = file_get_contents($fqlURL);
return json_decode($response);
}
$page_name = "https://www.facebook.com/dumetschool/?ref=ts&fref=ts";
$fb = jumlah_like(https://www.facebook.com/gegegenogege);
//var_dump($fb);
// jumlah like facebook
echo "URL = ".$page_name;
echo "
<h1><small>Jumlah Like:</small> ". $fb[0]->like_count ."</h1>
";
?>

Use data from session storage web in Polymer

I have a website made with Polymer that when you log in, it returns you in the session storage a key userData with values docID, name, surname and surname2, and then it enters to the platform. Those values are stored in the session storage of the browser.
I want to use those values except the docID and bring it to my code for plot it in the log in view/page, but I don't know how to use the session storage to take those parameters.
I made a fake user but with local storage that works with last time of connection but I don't know how to use it with session and receiving data from a website. This is my script:
Polymer({
date: null,
timeDate: null,
keyStorage: 'lastConnection',
ready: function(){
this.storageDate();
this.timeDate = this.getLastDateConnection();
this.storageUser();
this.fakeUserName = this.getUser();
},
getLastDateConnection: function(){
var date = new Date(parseInt(localStorage.getItem(this.keyStorage)));
return [date.getHours(),('0'+date.getMinutes()).slice(-2)].join(':');
},
storageDate: function(){
localStorage.setItem(this.keyStorage, +new Date);
},
getUser: function(){
var name = [localStorage.getItem("firstname") + " " + localStorage.getItem("lastname")];
return name;
},
storageUser:function(){
localStorage.setItem("lastname", "Vader");
localStorage.setItem("firstname", "Dark");
}
});
I want to do something similar except I have to storage the user data with session storage and from a website (I don't know the info until someone gets logged), so I suppose that I shouldn't do a setItem and just made a getItem receiving the key "userData" from the website. Any help/idea? Thanks!
PS: Maybe should I store the user info in my local storage after I receive the userData from the session storage if I want to keep the username? What I want to do is something equal to what Google do with our gmail accounts (you logg-in and when you want to enter again, it stores your account).
Ok, so I think I got it.
in the ready function is to make the call of the function that storages the session storage with:
ready: function(){
this.setData();
this.myUser = this.storageUser();
},
setData: function() {
sessionStorage.setItem("userData",userData);
},
and then storage the session storage in the local making a parse of the object:
storageUser: function(){
var userData = sessionStorage.getItem("userData");
var myObject = JSON.parse(userData);
var userName = [myObject.name + " " + myObject.surname];
return userName;
},
This is working on principle.
I know this is an old post but for new people who land on this page, this session storage element might be of help. It's behaviour is exactly like local storage.
<!--
#license
Copyright (c) 2015 The PlatinumIndustries.pl. All rights reserved.
This code may only be used under the BSD style license.
-->
<link rel="import" href="../polymer/polymer.html">
<!--
Element access to Web Storage API (window.sessionStorage).
Keeps `value` property in sync with sessionStorage.
Value is saved as json by default.
### Usage:
`Ss-sample` will automatically save changes to its value.
<dom-module id="ls-sample">
<iron-sessionstorage name="my-app-storage"
value="{{cartoon}}"
on-iron-sessionstorage-load-empty="initializeDefaultCartoon"
></iron-sessionstorage>
</dom-module>
<script>
Polymer({
is: 'ls-sample',
properties: {
cartoon: {
type: Object
}
},
// initializes default if nothing has been stored
initializeDefaultCartoon: function() {
this.cartoon = {
name: "Mickey",
hasEars: true
}
},
// use path set api to propagate changes to sessionstorage
makeModifications: function() {
this.set('cartoon.name', "Minions");
this.set('cartoon.hasEars', false);
}
});
</script>
### Tech notes:
* * `value.*` is observed, and saved on modifications. You must use
path change notifification methods such as `set()` to modify value
for changes to be observed.
* * Set `auto-save-disabled` to prevent automatic saving.
* * Value is saved as JSON by default.
* * To delete a key, set value to null
* Element listens to StorageAPI `storage` event, and will reload upon receiving it.
* **Warning**: do not bind value to sub-properties until Polymer
[bug 1550](https://github.com/Polymer/polymer/issues/1550)
is resolved. session storage will be blown away.
`<iron-sessionstorage value="{{foo.bar}}"` will cause **data loss**.
#demo demo/index.html
#hero hero.svg
-->
<dom-module id="iron-sessionstorage"></dom-module>
<script>
Polymer({
is: 'iron-sessionstorage',
properties: {
/**
* SessionStorage item key
*/
name: {
type: String,
value: ''
},
/**
* The data associated with this storage.
* If set to null item will be deleted.
* #type {*}
*/
value: {
type: Object,
notify: true
},
/**
* If true: do not convert value to JSON on save/load
*/
useRaw: {
type: Boolean,
value: false
},
/**
* Value will not be saved automatically if true. You'll have to do it manually with `save()`
*/
autoSaveDisabled: {
type: Boolean,
value: false
},
/**
* Last error encountered while saving/loading items
*/
errorMessage: {
type: String,
notify: true
},
/** True if value has been loaded */
_loaded: {
type: Boolean,
value: false
}
},
observers: [
'_debounceReload(name,useRaw)',
'_trySaveValue(autoSaveDisabled)',
'_trySaveValue(value.*)'
],
ready: function() {
this._boundHandleStorage = this._handleStorage.bind(this);
},
attached: function() {
window.addEventListener('storage', this._boundHandleStorage);
},
detached: function() {
window.removeEventListener('storage', this._boundHandleStorage);
},
_handleStorage: function(ev) {
if (ev.key == this.name) {
this._load(true);
}
},
_trySaveValue: function() {
if (this._doNotSave) {
return;
}
if (this._loaded && !this.autoSaveDisabled) {
this.debounce('save', this.save);
}
},
_debounceReload: function() {
this.debounce('reload', this.reload);
},
/**
* Loads the value again. Use if you modify
* sessionStorage using DOM calls, and want to
* keep this element in sync.
*/
reload: function() {
this._loaded = false;
this._load();
},
/**
* loads value from session storage
* #param {boolean=} externalChange true if loading changes from a different window
*/
_load: function(externalChange) {
var v = window.sessionStorage.getItem(this.name);
if (v === null) {
this._loaded = true;
this._doNotSave = true; // guard for save watchers
this.value = null;
this._doNotSave = false;
this.fire('iron-sessionstorage-load-empty', { externalChange: externalChange});
} else {
if (!this.useRaw) {
try { // parse value as JSON
v = JSON.parse(v);
} catch(x) {
this.errorMessage = "Could not parse session storage value";
console.error("could not parse sessionstorage value", v);
v = null;
}
}
this._loaded = true;
this._doNotSave = true;
this.value = v;
this._doNotSave = false;
this.fire('iron-sessionstorage-load', { externalChange: externalChange});
}
},
/**
* Saves the value to localStorage. Call to save if autoSaveDisabled is set.
* If `value` is null or undefined, deletes localStorage.
*/
save: function() {
var v = this.useRaw ? this.value : JSON.stringify(this.value);
try {
if (this.value === null || this.value === undefined) {
window.sessionStorage.removeItem(this.name);
} else {
window.sessionStorage.setItem(this.name, /** #type {string} */ (v));
}
}
catch(ex) {
// Happens in Safari incognito mode,
this.errorMessage = ex.message;
console.error("sessionStorage could not be saved. Safari incoginito mode?", ex);
}
}
/**
* Fired when value loads from localStorage.
*
* #event iron-localstorage-load
* #param {{externalChange:boolean}} detail -
* externalChange: true if change occured in different window.
*/
/**
* Fired when loaded value does not exist.
* Event handler can be used to initialize default value.
*
* #event iron-localstorage-load-empty
* #param {{externalChange:boolean}} detail -
* externalChange: true if change occured in different window.
*/
});
</script>

Categories

Resources