Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add map typed flags #15

Merged
merged 11 commits into from
Feb 4, 2018
Merged

Add map typed flags #15

merged 11 commits into from
Feb 4, 2018

Conversation

furdarius
Copy link
Contributor

@furdarius furdarius commented Jan 22, 2018

Issue #7
This PR add availability of map typed flags usage. Map keys can be string or kind of integer, and values can be any types available as flags by this library.

Example:

package main

import (
	"fmt"
	"log"
	"github.com/davecgh/go-spew/spew"
	"github.com/octago/sflags/gen/gflag"
	"flag"
)

type config struct {
	Field1 map[string]int64
	Field2 map[int64]int32
	Field3 map[int8]bool
	Field4 map[string]string
}

func main() {
	var cfg config

	err := gflag.ParseToDef(&cfg)
	if err != nil {
		log.Fatalf("err: %v", err)
	}

	flag.Parse()

	fmt.Printf("cfg: %s\n", spew.Sdump(cfg))

	/*
	INPUT:
	./flag -field1=test:123 -field2=8:2 -field2=3:5 -field3=5:true -field4=m:v
	*/

	/*
	OUTPUT:
	cfg: (main.config) {
	Field1: (map[string]int64) (len=1) {
		(string) (len=4) "test": (int64) 123
		},
	Field2: (map[int64]int32) (len=2) {
		(int64) 8: (int32) 2,
		(int64) 3: (int32) 5
		},
	Field3: (map[int8]bool) (len=1) {
		(int8) 5: (bool) true
		},
	Field4: (map[string]string) (len=1) {
		(string) (len=1) "m": (string) (len=1) "v"
		}
	}
	*/
}

TODO:

  • Add tests for generated maps types
  • Extend docs

Signed-off-by: Artemiy Ryabinkov <[email protected]>
Signed-off-by: Artemiy Ryabinkov <[email protected]>
Signed-off-by: Artemiy Ryabinkov <[email protected]>
Signed-off-by: Artemiy Ryabinkov <[email protected]>
@codecov-io
Copy link

codecov-io commented Jan 22, 2018

Codecov Report

Merging #15 into master will increase coverage by 0.65%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #15      +/-   ##
==========================================
+ Coverage   99.29%   99.95%   +0.65%     
==========================================
  Files          10       10              
  Lines        1424     8042    +6618     
==========================================
+ Hits         1414     8038    +6624     
+ Misses          7        2       -5     
+ Partials        3        2       -1
Impacted Files Coverage Δ
values_generated.go 100% <ø> (ø) ⬆️
parser.go 100% <100%> (+4.25%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 45e390e...9c35ef5. Read the comment docs.

func parseGeneratedMap(value interface{}) Value {
switch value.(type) {
{{range .Values}}{{ if not .NoMap }}\nn
{{ $value := . }}{{range $mapKeyTypes}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add \nn here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

@furdarius furdarius changed the title [WIP] Add map typed flags Add map typed flags Jan 24, 2018
@m0sth8
Copy link
Contributor

m0sth8 commented Feb 4, 2018

Thank you!

@m0sth8 m0sth8 merged commit 69e028c into urfave:master Feb 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants