Thursday, July 30, 2015

Go: a list of structure field names

package main

import (
 "fmt"
 "reflect"
)

type User struct {
 Name     string
 Age      int
 Password string
}

func GetFieldNames(s interface{}) []string {
 t := reflect.TypeOf(s)
 if t.Kind() == reflect.Ptr {
  t = t.Elem()
 }
 if kind := t.Kind(); kind != reflect.Struct {
  fmt.Printf("not a struct kind: %s", kind)
  return nil
 }
 var ret []string
 for i := 0; i < t.NumField(); i++ {
  field := t.Field(i)
  ret = append(ret, field.Name)
 }
 return ret
}

func main() {
 u := User{}
 fmt.Println(GetFieldNames(u))
 fmt.Println(GetFieldNames(&u))
}
At Go Playground: http://play.golang.org/p/6LDoCIKFeH
The function returns a slice of strings. Each is a name of a field. If a pointer is an argument the underlying type's field names are returned. The function ended up in: github.com/orian/utils/reflect. A documentation is browsable at: https://gowalker.org/github.com/orian/utils/reflect.
A short explanation:
  • First get a reflect.Type of a given argument s
  • If s' kind is a pointer, get a descriptor of underlying type.
  • If underlying type is not a struct, return error..
  • Iterate over fields, add field names and return the result slice.

Wednesday, July 1, 2015

TIL: clicked element reference in JavaScript console

I had a break from WebDev for around 2 years. I'm rediscovering things now. Today I've learnt: $0
  • Right click on the element of interest in your browser and select 'inspect element'. You should see the browser debugger with the element you clicked on highlighted.
  • The debugger allows you to access the currently selected element in the console as $0 variable.

Thursday, June 11, 2015

TIL: never depend on the order of evaluation of sub-expressions in a stream

The code below may result in a 6 different execution orders. It will surprise and hurt you, so don't do that ;-)
std::cout << wizard.CastSpell("abracadabra") << wizard.DrinkPotion("Rosanke") << wizard.Mana();
More info can be found:

Monday, June 1, 2015

Endless extracting of tar.bz2 archive

Funny story. Over a year ago I've backed up a project directory to tar.bz2 archive. Today I've decided to check what's inside. I've encountered a frozen window of 'extract here' option from file context menu.

I've killed the extract process and rerun it from a terminal:
tar -xvvf archive.tar.bz2
After few normal lines i've seen smth (in fact thousands of such lines):
hrwxr-x--- user/group       0 2013-02-06 23:20 workspace/a/b/a/b/f.go link to workspace/a/b/f.go
hrwxr-x--- user/group       0 2013-02-06 23:20 workspace/a/b/a/b/a/b/f.go link to workspace/a/b/a/b/f.go
This clued me, that probably the archive kept the symbolic links. After Googling and reading tar manual I had this idea: let's remove a symlink from an archive:
tar --delete -f archive.tar workspace/a/b/a
This worked just well :-)

Thursday, May 28, 2015

pep8

I've got very comfortable with clang-format for C++ and gofmt for Go. Every time I have to use some other language i'm missing them very much. Today I've found a solution for Python. It makes life so much easier. The two awesome tools are:

pep8

pep8 is a tool to check your Python code against some of the style conventions in PEP 8.
https://pypi.python.org/pypi/pep8

autopep8

autopep8 automatically formats Python code to conform to the PEP 8 style guide. It uses the pep8 utility to determine what parts of the code needs to be formatted. autopep8 is capable of fixing most of the formatting issues that can be reported by pep8.
https://github.com/hhatto/autopep8