Skip to content

Commit

Permalink
pw_web: Remove object-path dependency from Device API
Browse files Browse the repository at this point in the history
Change-Id: I64a852f42d7f7be1ea6795de23d6c96641ff827f
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/165013
Reviewed-by: Anthony DiGirolamo <tonymd@google.com>
Commit-Queue: Asad Memon <asadmemon@google.com>
  • Loading branch information
asadm authored and CQ Bot Account committed Aug 14, 2023
1 parent c02cc30 commit 0e3ebc9
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 17 deletions.
14 changes: 0 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
"@protobuf-ts/protoc": "^2.7.0",
"google-protobuf": "^3.17.3",
"long": "^5.2.1",
"object-path": "^0.11.8",
"postcss": "^8.4.24",
"prettier": "^3.0.1",
"rollup-plugin-postcss": "^4.0.2",
Expand Down
4 changes: 2 additions & 2 deletions ts/device/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// License for the specific language governing permissions and limitations under
// the License.

import objectPath from 'object-path';
import { setPathOnObject } from './object_set';
import { Decoder, Encoder } from 'pigweedjs/pw_hdlc';
import {
Client,
Expand Down Expand Up @@ -85,7 +85,7 @@ export class Device {
let channel = this.client.channel();
let servicesKeys = Array.from(channel.services.keys());
servicesKeys.forEach((serviceKey) => {
objectPath.set(
setPathOnObject(
rpcMap,
serviceKey,
this.mapServiceMethods(channel.services.get(serviceKey)),
Expand Down
81 changes: 81 additions & 0 deletions ts/device/object_set.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2023 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

function hasOwnProperty(obj: Object, prop: number | string) {
if (obj == null) {
return false;
}
//to handle objects with null prototypes (too edge case?)
return Object.prototype.hasOwnProperty.call(obj, prop);
}
function hasShallowProperty(obj: Object, prop: number | string) {
return (
(typeof prop === 'number' && Array.isArray(obj)) ||
hasOwnProperty(obj, prop)
);
}

function getShallowProperty(obj: Object, prop: number | string) {
if (hasShallowProperty(obj, prop)) {
return obj[prop];
}
}
function getKey(key) {
var intKey = parseInt(key);
if (intKey.toString() === key) {
return intKey;
}
return key;
}

export function setPathOnObject(
obj: Object,
path: number | string | Array<number | string>,
value: any,
doNotReplace: boolean = false,
) {
if (typeof path === 'number') {
path = [path];
}
if (!path || path.length === 0) {
return obj;
}
if (typeof path === 'string') {
return setPathOnObject(
obj,
path.split('.').map(getKey),
value,
doNotReplace,
);
}
var currentPath = path[0];
var currentValue = getShallowProperty(obj, currentPath);
if (path.length === 1) {
if (currentValue === void 0 || !doNotReplace) {
obj[currentPath] = value;
}
return currentValue;
}

if (currentValue === void 0) {
//check if we assume an array
if (typeof path[1] === 'number') {
obj[currentPath] = [];
} else {
obj[currentPath] = {};
}
}

return setPathOnObject(obj[currentPath], path.slice(1), value, doNotReplace);
}

0 comments on commit 0e3ebc9

Please sign in to comment.