grokify/mogo

View on GitHub
encoding/jsonpointer/parser.go

Summary

Maintainability
A
35 mins
Test Coverage
package jsonpointer

import (
    "errors"
    "strings"
)

var (
    ErrJSONPointerInvalidSyntaxNoAnchorSlash     = errors.New("invalid JSON Pointer format - no `#/`")
    ErrJSONPointerInvalidSyntaxNonOneAnchorSlash = errors.New("invalid JSON Pointer format - non-1 `#/`")
)

type JSONPointer struct {
    Document   string
    String     string
    PathString string
    Path       []string
}

func ParseJSONPointer(s string) (JSONPointer, error) {
    anchorSlash := "#/"
    slash := "/"
    ptr := JSONPointer{String: s}
    if strings.Index(s, anchorSlash) == 0 {
        ptr.PathString = s
        pathTrimmed := strings.TrimLeft(s, anchorSlash)
        ptr.Path = strings.Split(pathTrimmed, "/")
        return ptr, nil
    } else if strings.Index(s, slash) == 0 {
        ptr.PathString = s
        pathTrimmed := strings.TrimLeft(s, slash)
        ptr.Path = strings.Split(pathTrimmed, "/")
        return ptr, nil
    }
    if !strings.Contains(s, anchorSlash) {
        return ptr, ErrJSONPointerInvalidSyntaxNoAnchorSlash
    }
    parts := []string{}
    if strings.Contains(s, anchorSlash) {
        parts = strings.Split(s, anchorSlash)
    }
    if len(parts) != 2 {
        return ptr, ErrJSONPointerInvalidSyntaxNonOneAnchorSlash
    }
    ptr.Document = parts[0]
    ptr.PathString = parts[1]
    ptr.Path = strings.Split(ptr.PathString, "/")
    return ptr, nil
}