{"id":13411647,"url":"https://github.com/gurukami/typ","last_synced_at":"2025-03-14T17:31:01.987Z","repository":{"id":57490001,"uuid":"173535197","full_name":"gurukami/typ","owner":"gurukami","description":"Null Types, Safe primitive type conversion and fetching value from complex structures.","archived":false,"fork":false,"pushed_at":"2021-10-15T16:11:56.000Z","size":101,"stargazers_count":46,"open_issues_count":0,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-07-31T20:48:19.667Z","etag":null,"topics":["cast","go","golang","golang-library","typecast","types"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gurukami.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-03-03T05:34:23.000Z","updated_at":"2024-07-05T11:17:59.000Z","dependencies_parsed_at":"2022-09-02T12:01:02.822Z","dependency_job_id":null,"html_url":"https://github.com/gurukami/typ","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gurukami%2Ftyp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gurukami%2Ftyp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gurukami%2Ftyp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gurukami%2Ftyp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gurukami","download_url":"https://codeload.github.com/gurukami/typ/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243618654,"owners_count":20320271,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cast","go","golang","golang-library","typecast","types"],"created_at":"2024-07-30T20:01:15.360Z","updated_at":"2025-03-14T17:31:01.642Z","avatar_url":"https://github.com/gurukami.png","language":"Go","readme":"# Typ\n\n[![GoDoc](https://godoc.org/github.com/gurukami/typ?status.svg)](https://godoc.org/github.com/gurukami/typ)\n[![Build Status](https://travis-ci.org/gurukami/typ.svg \"Travis CI status\")](https://travis-ci.org/gurukami/typ)\n[![Coverage Status](https://img.shields.io/codecov/c/github/gurukami/typ/master.svg)](https://codecov.io/github/gurukami/typ?branch=master)\n[![Go Report Card](https://goreportcard.com/badge/github.com/gurukami/typ?style=flat)](https://goreportcard.com/report/github.com/gurukami/typ)\n[![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go)\n\nTyp is a library providing a powerful interface to impressive user experience with conversion and fetching data from built-in types in Golang\n\n## Features\n\n* Safe conversion along built-in types like as `bool`, `int`, `int8`, `int16`, `int32`, `int64`, `uint`, `uint8`, `uint16`, `uint32`, `uint64`, `float32`, `float64`, `complex64`, `complex128`, `string`\n* Null types for all primitive types with supported interfaces: ```json.Unmarshaler```, ```json.Marshaler```, ```sql.Scanner```, ```driver.Valuer```\n* Value retriever for multidimensional unstructured data from interface\n* Conversion functions present via interface (reflection) and native types for better performance\n* Some humanize string conversion functions\n\n## Installation\n\nUse ```go get``` to install the latest version of the library. \n\n```go\n    go get -u github.com/gurukami/typ\n```\n\nThen include package in your application and enjoy.\n\n```go\n    import \"github.com/gurukami/typ/v2\"\n```\n## Usage\n\n**Of(interface{})** conversion from interface value to built-in type\n\n```go\n// typ.Of(v interface{}, options ...Option).{Type}(defaultValue ...{Type})\n//\n// Where {Type} any of \n//      Bool, \n//      Int, Int8, Int16, Int32, Int64, \n//      Uint, Uint8, Uint16, Uint32, Uint64, \n//      Float32, Float, \n//      Complex64, Complex, \n//      String\n//\n// All methods for conversion returns {Type}Accessor with helpful methods\n//\n//      V() - value of type\n//      Present() - determines whether a value has been set\n//      Valid() - determines whether a value has been valid (without error)\n//      Err() error - returns underlying error  \n//      Set(value {Type}) - saves value into current struct  \n//      Clone() {Type}Accessor - returns new instance of current struct with preserved value \u0026 error  \n//      \n//      Scan(value interface{})         | sql.Scanner\n//      Value() (driver.Value, error)   | driver.Valuer\n//\n//      UnmarshalJSON(b []byte) error   | json.Unmarshaler\n//      MarshalJSON() ([]byte, error)   | json.Marshaler\n\n// Valid\nnv := typ.Of(3.1415926535, typ.FmtByte('g'), typ.Precision(4)).String()\nfmt.Printf(\"Value: %v, Valid: %v, Present: %v, Error: %v\\n\", nv.V(), nv.Valid(), nv.Present(), nv.Error)\n// Output: Value: 3.142, Valid: true, Present: true, Error: \u003cnil\u003e\n\n// Not valid\nnv = typ.Of(3.1415926535).Int()\nfmt.Printf(\"Value: %v, Valid: %v, Present: %v, Error: %v\\n\", nv.V(), nv.Valid(), nv.Present(), nv.Error)\n// Output: Value: 3, Valid: false, Present: true, Error: value can't safely convert\n```\n\n**Native conversion without `reflection` when type is know** \n\n```go\n// For the best performance always use this way if you know about exact type\n//\n// typ.{FromType}{ToType}(value , [options ...{FromType}{ToType}Option]).V()\n// \n// Where {FromType}, {ToType} any of \n//      Bool, \n//      Int, Int8, Int16, Int32, Int64, \n//      Uint, Uint8, Uint16, Uint32, Uint64, \n//      Float32, Float, \n//      Complex64, Complex, \n//      String\n//\n// All methods for conversion returns {Type}Accessor interface with helpful methods \u0026 fields, additional info you can read in example above\n\n// Valid\nnv := typ.FloatString(3.1415926535, typ.FloatStringFmtByte('g'), typ.FloatStringPrecision(4))\nfmt.Printf(\"Value: %v, Valid: %v, Present: %v, Error: %v\\n\", nv.V(), nv.Valid(), nv.Present(), nv.Error)\n// Output: Value: 3.142, Valid: true, Present: true, Error: \u003cnil\u003e\n\n// Not valid\nnv = typ.FloatInt(3.1415926535)\nfmt.Printf(\"Value: %v, Valid: %v, Present: %v, Error: %v\\n\", nv.V(), nv.Valid(), nv.Present(), nv.Error)\n// Output: Value: 3, Valid: false, Present: true, Error: value can't safely convert\n```\n\n**Retrieve multidimensional unstructured data from interface** \n\n```go\ndata := map[int]interface{}{\n   0: []interface{}{\n      0: map[string]int{\n         \"0\": 42,\n      },\n   },\n}\n\n// Instead of do something like this \n//  data[0].([]interface{})[0].(map[string]int)[\"0”]\n//      and not caught a panic\n// use this\n\n// Value exists\nnv := typ.Of(data).Get(0, 0, \"0\").Interface()\nfmt.Printf(\"Value: %v, Valid: %v, Present: %v, Error: %v\\n\", nv.V(), nv.Valid(), nv.Present(), nv.Error)\n// Output: Value: 42, Valid: true, Present: true, Error: \u003cnil\u003e\n\n// Value not exists\nnv = typ.Of(data).Get(3, 7, \"5\").Interface()\nfmt.Printf(\"Value: %v, Valid: %v, Present: %v, Error: %v\\n\", nv.V(), nv.Valid(), nv.Present(), nv.Error)\n// Output: Value: \u003cnil\u003e, Valid: false, Present: false, Error: out of bounds on given data\n```\n\n**Rules of safely type conversion along types**\n\n| From / to   | Bool | Int* |  String |  Uint*  |  Float* | Complex*  |\n|----------|:-------------:|:------:|:---:|:---:|:---:|:---:|\n| Bool      | + | + | + | +  | +  |  + |\n| Int* |   +    | +   | `formatting`  | `\u003e= 0`  | `24bit or 53bit`  | `real`, `24bit or 53bit`  |\n| String | `parsing` | `parsing`  | +  | `parsing`  | `parsing`  | `parsing`  |\n| Uint*  | + | `63bit` | `formatting`  | +  | `24bit or 53bit`  |  `24bit or 53bit`  |\n| Float*   |       +        |   `24bit or 53bit`    |  `formatting` | `\u003e= 0`, `24bit or 53bit`  |  + | +  |\n| Complex*   |       +        |   `real`, `24bit or 53bit`    | +  | `\u003e= 0`, `real`, `24bit or 53bit`  | `real` | +  |\n\n\\* based on bit size capacity, `8,16,32,64` for `Int`,`Uint`; `32,64` for `Float`,`Complex`\n\n## Donation for amazing goal\n\nI like airplanes and i want to get private pilot licence, and i believe you can help me to make my dream come true :)  \n\n[ \u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e **Make a dream come true** \u003c\u003c\u003c\u003c\u003c\u003c\u003c\u003c\u003c\u003c ](https://gist.github.com/Nerufa/0d868899d628b1b105f74b6da501bc1f)\n\n\n## License\n\nThe MIT license  \nCopyright (c) 2019 Gurukami","funding_links":[],"categories":["数据结构","Data Structures and Algorithms","数据结构与算法","Uncategorized","Data Integration Frameworks","Data Structures","Generators","数据结构`go语言实现的数据结构与算法`"],"sub_categories":["标准 CLI","Nullable Types","可空类型","Advanced Console UIs"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgurukami%2Ftyp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgurukami%2Ftyp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgurukami%2Ftyp/lists"}