Code Monkey home page Code Monkey logo

pbtxt's Introduction

Pbtxt Swift

Schemaless protobuf text parsers.

Pbtxt.parse(pbtxt: String) throws -> [String: Any]:

Parse a protobuf text-format file into a dictionary. Since the parser is unaware of the schema, there are 2 keys being decoded for every field:

  • key: non-repeated field version.
  • Pbtxt.repeatedField(key): repeated field version (where the associated value is an array).

Example Given the pbtxt below:

layer {
  dim: 42
  [some.proto.ext]: "__"
}
model { id: "id_1" }
model { id: "id_2" }

The parsed dictionary yields:

let dict = try Pbtxt.parse(pbtxt: src)
dict["layer"] // {dim: 42, ..}
dict["model"] // {id: "node_1"}
dict[Pbtxt.repeatedField("model")] // [{id: "id_1"}, {id: "id_2"}]

Pbtxt.decode<T: Decodable>(type: T.Type,pbtxt: String) throws -> T:

Returns a value of the type you specify, decoded from a pbtxt file. Since the parser is unaware of the schema, the same consideration about repeated/non-repeated fields described above applies and must be reflected in your object CodingKeys.

An example codable object for the above described pbxt would be the following;

 struct Layer: Codable {
   enum CodingKeys: String, CodingKey {
      case dimension = "dim"
      case ext = "[some.proto.ext]"
   }
   let dimension: UInt
   let ext: String
 }

 struct Model: Codable {
   let id: String
 }

 struct Obj: Codable {
   enum CodingKeys: String, CodingKey {
     case layer = "layer"
     case models = Pbtxt.repeatedField("model")
   }
   let layer: Layer
   let models: [Model]
 }

Pbtxt.write(dictionary: [String: Any]) -> String :

Write a dictionary into protobuf text-format.

Pbtxt.encode<T: Encodable>(object: T) -> T:

Encodes an object into its protobuf text-format representation.

pbtxt's People

Contributors

alexdrone avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.