|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright 2018-2021 The Feast Authors |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package feast.ingestion.utils |
| 18 | + |
| 19 | +import java.util.Locale.ENGLISH |
| 20 | + |
| 21 | +import org.json4s.{JArray, JField, JObject, JValue} |
| 22 | + |
| 23 | +object JsonUtils { |
| 24 | + def mapFieldWithParent(jv: JValue)(f: (String, JField) => JField): JValue = { |
| 25 | + def rec(v: JValue, parent: String = ""): JValue = v match { |
| 26 | + case JObject(l) => JObject(l.map { case (key, va) => f(parent, key -> rec(va, key)) }) |
| 27 | + case JArray(l) => JArray(l.map(rec(_, parent))) |
| 28 | + case x => x |
| 29 | + } |
| 30 | + rec(jv) |
| 31 | + } |
| 32 | + |
| 33 | + def camelize(word: String): String = { |
| 34 | + if (word.nonEmpty) { |
| 35 | + val w = pascalize(word) |
| 36 | + w.substring(0, 1).toLowerCase(ENGLISH) + w.substring(1) |
| 37 | + } else { |
| 38 | + word |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + def pascalize(word: String): String = { |
| 43 | + val lst = word.split("_").toList |
| 44 | + (lst.headOption.map(s => s.substring(0, 1).toUpperCase(ENGLISH) + s.substring(1)).get :: |
| 45 | + lst.tail.map(s => s.substring(0, 1).toUpperCase + s.substring(1))).mkString("") |
| 46 | + } |
| 47 | +} |
0 commit comments