A Go package that provides a simple way of accessing nested properties in maps and slices.

Related tags

Command Line ask
Overview

ASK
PkgGoDev Go Report Card GoCover.io Mentioned in Awesome Go
and you shall receive.

Ask provides a simple way of accessing nested properties in maps and slices. Works great in combination with encoding/json and other packages that "Unmarshal" arbitrary data into Go data-types. Inspired by the get function in the lodash javascript library.

Use

package main

import "json"
import "github.com/simonnilsson/ask"

func main() {

	// Use parsed JSON as source data
	var object map[string]interface{}
	json.Unmarshal([]byte(`{ "a": [{ "b": { "c": 3 } }] }`), &object)

	// Extract the 3
	res, ok := ask.For(object, "a[0].b.c").Int(0)

	fmt.Println(res, ok)
	// Output: 3 true

	// Attempt extracting a string at path .d that does not exist
	res2, ok := ask.For(object, "a[0].b.d").String("nothing")

	fmt.Println(res2, ok)
	// Output: nothing false

}

API

Internally ask uses type assertions to traverse down the path supplied. Each invocation starts by calling For() with your data structure source and the path in this structure to extract.

For(source interface{}, path string) *Answer

Additional paths can be traversed by calling Path() on the resulting answer.

(a *Answer) Path(path string) *Answer

Type assertion

After receiving an *Answer from a call to For() it can be asserted to a type. The methods for this is seen below. Each function takes a default value as a parameter that will be returned in case the value can not be asserted from the answer. A second return value is used to indicate if the assertion was successful.

  • String(d string) (string, bool)
  • Bool(d bool) (bool, bool)
  • Int(d int64) (int64, bool)
  • Uint(d uint64) (uint64, bool)
  • Float(d float64) (float64, bool)
  • Slice(d []interface{}) ([]interface{}, bool)
  • Map(d map[string]interface{}) (map[string]interface{}, bool)

If a number is found but it is of different type than requested it will be casted to desired type and return success. If the value would not fit within the valid range of requested type the operation will fail however and the default parameter will be returned instead.

Two additional methods are available, one to check if the answer has a value (not nil) and one to return the raw value as a interface{}.

  • Exists() bool
  • Value() interface{}

For full documentation see pkg.go.dev.

License

MIT

Owner
Simon Nilsson
Simon Nilsson
Go library for accessing the Reddit API.

Overview Featured in issues 327 and 347 of Golang Weekly ?? go-reddit is a Go client library for accessing the Reddit API. You can view Reddit's offic

Vartan Benohanian 236 Jan 12, 2022
Fonts is a package that provides helpers to access font details and easily retrive font bytes with ZERO dependencies

Fonts Fonts is a package that provides helpers to access font details and easily retrieve font bytes. This package has ZERO 3rd-party dependencies. Fo

Go Swiss 11 Apr 28, 2021
A simple way of sending messages from the CLI output to your Discord channel with webhook.

discat A simple way of sending messages from the CLI output to your Discord channel with webhook. Actually, this is a fork version of slackcat that I

dw1 25 Dec 28, 2021
A simple way for CLI command to have many subcommands

subcommands This is a modified fork of google/subcommands that uses lucasepe/pflag Subcommands is a Go package that implements a simple way for a sing

Luca Sepe 0 Oct 12, 2021
Package osargs provides functions to parse command line arguments

osargs About Package osargs provides functions to parse command line arguments. It is published on https://github.com/vbsw/osargs and https://gitlab.c

Vitali Baumtrok 0 Dec 14, 2021
A better way to clone, organize and manage multiple git repositories

git-get git-get is a better way to clone, organize and manage multiple git repositories. git-get Description Installation macOS Linux Windows Usage gi

Greg Dlugoszewski 53 Jan 9, 2022
🚀 goprobe is a promising command line tool for inspecting URLs with modern and user-friendly way.

goprobe Build go build -o ./bin/goprobe Example > goprobe https://github.com/gaitr/goprobe > cat links.txt | goprobe > echo "https://github.com/gaitr/

null 3 Oct 24, 2021
A command tool to help user install oh-my-zsh plugins fast in a comfortable way

zshx A command tool to help user install oh-my-zsh plugins fast in a comfortable way. in other way, it is a zsh plugin package manager. How to use the

xylon 5 Aug 2, 2021
Small CLI Tool to store test artifacts in a tamperproof way

Small CLI Tool to store test artifacts in a tamperproof way

Ulises Fierro 3 Sep 29, 2021
go-editor is the clean go module that refractors from Kubernetes to help you edit resources in a command-line way.

go-editor The source code of go-editor comes from Kubernetes and refractor as the clean Go module. You can embed go-editor in your command-line tool l

Yong 0 Dec 5, 2021
V2 because I wasn't happy with the way things were going in V1

Pokerchips V2 A client/server app that simulates poker chips so you and your friends can play poke without physical chips! Client The client is writte

Anders Olson 0 Jan 11, 2022
this service provides an API for publishing and managing your articles

REST API Для Публикации и Работы с Articles на Go В работе применены следующие концепции: Разработка Веб-Приложений на Go, следуя дизайну REST API. Ра

astduman 0 Dec 15, 2021
Got: Simple golang package and CLI tool to download large files faster 🏃 than cURL and Wget!

Got. Simple and fast concurrent downloader. Installation ❘ CLI Usage ❘ Module Usage ❘ License Comparison Comparison in cloud server: [[email protected]

Mohamed El Bahja 525 Jan 6, 2022
PingMe is a CLI tool which provides the ability to send messages or alerts to multiple messaging platforms & email.

PingMe is a personal project to satisfy my needs of having alerts, most major platforms have integration to send alerts but its not always useful, either you are stuck with one particular platform, or you have to do alot of integrations. I needed a small app which i can just call from my backup scripts, cron jobs, CI/CD pipelines or from anywhere to send a message with particular information. And i can ship it everywhere with ease. Hence, the birth of PingMe.

Khaliq 495 Jan 13, 2022
Envopts - Provides a code generator for turning env structs into functional options

envopts Provides a code generator to turn structs annotated for the popular env

null 3 Jan 10, 2022
A simple, fast, and fun package for building command line apps in Go

cli cli is a simple, fast, and fun package for building command line apps in Go. The goal is to enable developers to write fast and distributable comm

null 17.2k Jan 22, 2022
a lightweight and simple cli package

▄████████ ▄█ ▄█ ███ ███ ███ ███ ███ █▀ ███ ███▌ ███ ███ ███▌ ███ ███ ███▌ ███ █▄ ███

seo.do 12 Oct 14, 2021
Simple, useful and opinionated CLI package in Go.

acmd TODO Rationale TODO. Features Simple API. Easy to integrate. Builtin help and version commands. Clean and tested code. Dependency-free. Install G

cristaltech 32 Jan 9, 2022
cli is a simple, fast, and fun package for building command line apps in Go.

cli cli is a simple, fast, and fun package for building command line apps in Go. The goal is to enable developers to write fast and distributable comm

凹语言 1 Jan 8, 2022