Skip to content

Commit d4ef6a7

Browse files
Use es classes (#219)
1 parent f1e17f2 commit d4ef6a7

16 files changed

Lines changed: 1911 additions & 2054 deletions

handwritten/logging/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ Google APIs Client Libraries, in [Client Libraries Explained][explained].
6666

6767
```javascript
6868
// Imports the Google Cloud client library
69-
const Logging = require('@google-cloud/logging');
69+
const {Logging} = require('@google-cloud/logging');
7070

7171
// Your Google Cloud Platform project ID
7272
const projectId = 'YOUR_PROJECT_ID';

handwritten/logging/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@
8585
"devDependencies": {
8686
"@google-cloud/bigquery": "*",
8787
"@google-cloud/nodejs-repo-tools": "^2.3.3",
88-
"@google-cloud/pubsub": "*",
89-
"@google-cloud/storage": "*",
88+
"@google-cloud/pubsub": "0.20.0",
89+
"@google-cloud/storage": "^2.0.2",
9090
"async": "^2.6.1",
9191
"codecov": "^3.0.4",
9292
"eslint": "^5.4.0",

handwritten/logging/src/entry.js

Lines changed: 79 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const eventId = new EventId();
4646
* Any other types are stringified with `String(value)`.
4747
*
4848
* @example
49-
* const Logging = require('@google-cloud/logging');
49+
* const {Logging} = require('@google-cloud/logging');
5050
* const logging = new Logging();
5151
* const syslog = logging.log('syslog');
5252
*
@@ -80,103 +80,93 @@ const eventId = new EventId();
8080
* }
8181
* });
8282
*/
83-
function Entry(metadata, data) {
84-
/**
85-
* @name Entry#metadata
86-
* @type {object}
87-
* @property {Date} timestamp
88-
* @property {number} insertId
89-
*/
90-
this.metadata = extend(
91-
{
92-
timestamp: new Date(),
93-
},
94-
metadata
95-
);
96-
97-
// JavaScript date has a very coarse granularity (millisecond), which makes
98-
// it quite likely that multiple log entries would have the same timestamp.
99-
// The Logging API doesn't guarantee to preserve insertion order for entries
100-
// with the same timestamp. The service does use `insertId` as a secondary
101-
// ordering for entries with the same timestamp. `insertId` needs to be
102-
// globally unique (within the project) however.
103-
//
104-
// We use a globally unique monotonically increasing EventId as the
105-
// insertId.
106-
107-
this.metadata.insertId = this.metadata.insertId || eventId.new();
83+
class Entry {
84+
constructor(metadata, data) {
85+
/**
86+
* @name Entry#metadata
87+
* @type {object}
88+
* @property {Date} timestamp
89+
* @property {number} insertId
90+
*/
91+
this.metadata = extend(
92+
{
93+
timestamp: new Date(),
94+
},
95+
metadata
96+
);
97+
// JavaScript date has a very coarse granularity (millisecond), which makes
98+
// it quite likely that multiple log entries would have the same timestamp.
99+
// The Logging API doesn't guarantee to preserve insertion order for entries
100+
// with the same timestamp. The service does use `insertId` as a secondary
101+
// ordering for entries with the same timestamp. `insertId` needs to be
102+
// globally unique (within the project) however.
103+
//
104+
// We use a globally unique monotonically increasing EventId as the
105+
// insertId.
106+
this.metadata.insertId = this.metadata.insertId || eventId.new();
107+
/**
108+
* @name Entry#data
109+
* @type {object}
110+
*/
111+
this.data = data;
112+
}
108113

109114
/**
110-
* @name Entry#data
111-
* @type {object}
115+
* Serialize an entry to the format the API expects.
116+
*
117+
* @param {object} [options] Configuration object.
118+
* @param {boolean} [options.removeCircular] Replace circular references in an
119+
* object with a string value, `[Circular]`.
112120
*/
113-
this.data = data;
114-
}
115-
116-
/**
117-
* Create an Entry object from an API response, such as `entries:list`.
118-
*
119-
* @private
120-
*
121-
* @param {object} entry An API representation of an entry. See a
122-
* [LogEntry](https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry).
123-
* @returns {Entry}
124-
*/
125-
Entry.fromApiResponse_ = function(entry) {
126-
let data = entry[entry.payload];
127-
128-
if (entry.payload === 'jsonPayload') {
129-
data = Service.structToObj_(data);
130-
}
131-
132-
const serializedEntry = new Entry(entry, data);
133-
134-
if (serializedEntry.metadata.timestamp) {
135-
let ms = serializedEntry.metadata.timestamp.seconds * 1000;
136-
ms += serializedEntry.metadata.timestamp.nanos / 1e6;
137-
serializedEntry.metadata.timestamp = new Date(ms);
138-
}
139-
140-
return serializedEntry;
141-
};
142-
143-
/**
144-
* Serialize an entry to the format the API expects.
145-
*
146-
* @param {object} [options] Configuration object.
147-
* @param {boolean} [options.removeCircular] Replace circular references in an
148-
* object with a string value, `[Circular]`.
149-
*/
150-
Entry.prototype.toJSON = function(options) {
151-
options = options || {};
152-
153-
const entry = extend(true, {}, this.metadata);
154-
155-
if (is.object(this.data)) {
156-
entry.jsonPayload = Service.objToStruct_(this.data, {
157-
removeCircular: !!options.removeCircular,
158-
stringify: true,
159-
});
160-
} else if (is.string(this.data)) {
161-
entry.textPayload = this.data;
121+
toJSON(options) {
122+
options = options || {};
123+
const entry = extend(true, {}, this.metadata);
124+
if (is.object(this.data)) {
125+
entry.jsonPayload = Service.objToStruct_(this.data, {
126+
removeCircular: !!options.removeCircular,
127+
stringify: true,
128+
});
129+
} else if (is.string(this.data)) {
130+
entry.textPayload = this.data;
131+
}
132+
if (is.date(entry.timestamp)) {
133+
const seconds = entry.timestamp.getTime() / 1000;
134+
const secondsRounded = Math.floor(seconds);
135+
entry.timestamp = {
136+
seconds: secondsRounded,
137+
nanos: Math.floor((seconds - secondsRounded) * 1e9),
138+
};
139+
}
140+
return entry;
162141
}
163142

164-
if (is.date(entry.timestamp)) {
165-
const seconds = entry.timestamp.getTime() / 1000;
166-
const secondsRounded = Math.floor(seconds);
167-
168-
entry.timestamp = {
169-
seconds: secondsRounded,
170-
nanos: Math.floor((seconds - secondsRounded) * 1e9),
171-
};
143+
/**
144+
* Create an Entry object from an API response, such as `entries:list`.
145+
*
146+
* @private
147+
*
148+
* @param {object} entry An API representation of an entry. See a
149+
* [LogEntry](https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry).
150+
* @returns {Entry}
151+
*/
152+
static fromApiResponse_(entry) {
153+
let data = entry[entry.payload];
154+
if (entry.payload === 'jsonPayload') {
155+
data = Service.structToObj_(data);
156+
}
157+
const serializedEntry = new Entry(entry, data);
158+
if (serializedEntry.metadata.timestamp) {
159+
let ms = serializedEntry.metadata.timestamp.seconds * 1000;
160+
ms += serializedEntry.metadata.timestamp.nanos / 1e6;
161+
serializedEntry.metadata.timestamp = new Date(ms);
162+
}
163+
return serializedEntry;
172164
}
173-
174-
return entry;
175-
};
165+
}
176166

177167
/**
178168
* Reference to the {@link Entry} class.
179169
* @name module:@google-cloud/logging.Entry
180170
* @see Entry
181171
*/
182-
module.exports = Entry;
172+
module.exports.Entry = Entry;

0 commit comments

Comments
 (0)